<?php

namespace Rvsitebuilder\Wysiwyg\Http\Controllers\Backend;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Schema;
use Rvsitebuilder\Core\Models\CoreGlobalWidget;

class SocialWidgetController extends Controller
{
    public function getsocialwidget(Request $request)
    {
        $res['widget_html'] = '';
        $widgetdefconfig = $this->getWidgetDefConf();
        $setting = $widgetdefconfig['socialWidget']['setting'];

        $setting = $this->getSocialGlobalSetting($setting);

        $view = view('core::layouts.widget_wys_master_header')->render().
        view($widgetdefconfig['socialWidget']['blade'], compact('setting'))->render().
        view('core::layouts.widget_wys_master_footer')->render();

        return $view;

        /*         return response()->json([
                    'view' => $view,
                    'setting' => $setting,]); */
    }

    public function getSettingwidget()
    {
        $widgetdefconfig = $this->getWidgetDefConf();
        $setting = $widgetdefconfig['socialWidget']['setting'];
        if (Schema::hasTable('core_global_widget')) {
            $setting = $this->getSocialGlobalSetting($setting);
        }

        return $setting;
    }

    public function getsocialwidgetFooter(Request $request)
    {
        $res['widget_html'] = '';
        $widgetdefconfig = $this->getWidgetDefConf();
        $setting = $widgetdefconfig['socialWidget']['setting'];

        $setting = $this->getSocialGlobalSetting($setting);
        $view = view($widgetdefconfig['socialWidget']['blade'], compact('setting'))->render();

        return  $view;
    }

    public function applysocialbutton(Request $request)
    {
        $defsettting = $this->getWidgetDefConf();
        $regsetting = $request->setting;
        $regsetting = $this->editIndex($regsetting);
        $setting = $this->overwriteConfig($defsettting['socialWidget']['setting'], $regsetting);

        $this->setGlobalSocialWidget($setting);

        $widget_html = view($defsettting['socialWidget']['blade'], compact('setting'))->render();

        return $widget_html;
    }

    protected function getSocialGlobalSetting($setting)
    {
        $globalwidget = CoreGlobalWidget::where('widget_name', '=', 'social_widget')->first();
        if (!$globalwidget) {
            $globalwidget = CoreGlobalWidget::firstOrCreate(
                ['widget_name' => 'social_widget'],
                ['widget_module' => 'core']
            );

            $globalwidget->setMeta('globalwidget', $setting);
        }

        $setting = $globalwidget->getMeta('globalwidget');

        return $setting;
    }

    protected function getWidgetDefConf()
    {
        $widgetconf = (array) config('systempagewidget.widget');

        return $widgetconf;
    }

    protected function setGlobalSocialWidget($setting)
    {
        $globalwidget = CoreGlobalWidget::where('widget_name', '=', 'social_widget')->first();
        if (!$globalwidget) {
            $globalwidget = CoreGlobalWidget::firstOrCreate(
                ['widget_name' => 'social_widget'],
                ['widget_module' => 'core']
                );
        }
        $globalwidget->setMeta('globalwidget', $setting);
    }

    protected function overwriteConfig($defsettting, $regsetting)
    {
        foreach ($defsettting as $key => $value) {
            $defsettting[$key] = (null == $regsetting[$key] || '' == $regsetting[$key]) ? '' : $regsetting[$key];
        }

        return $defsettting;
    }

    protected function editIndex($setting)
    {
        foreach ($setting as $key => $value) {
            $newkey = explode('setting_', $key);
            $setting[$newkey[1]] = $value;
        }

        return $setting;
    }
}
