<?php

namespace Rvsitebuilder\Wysiwyg\Process\SaveSite;

use Illuminate\Support\Facades\DB;
use Lib\Util\RVLog;
use Rvsitebuilder\Core\Models\CoreSidebar;
use Rvsitebuilder\Core\Models\CoreSlug;
use SRIO\ChainOfResponsibility\ChainContext;
use SRIO\ChainOfResponsibility\ChainProcessInterface;

class SaveSidebarProcess implements ChainProcessInterface
{
    /**
     * TODO:: savetype == 'saveall' or 'sidebar'.
     *
     * @param ChainContext $context
     */
    public function execute(ChainContext $context)
    {
        if ((0 == $context['isHomepage'] && 0 == $context['sidebar']['isCustom_sidebar']) || ('saveall' !== $context['savetype'] && 'sidebar' !== $context['savetype'])) {
            RVLog::debug('skip process');

            return false;
        }

        $saveSidebar = $this->saveSidebar($context['sidebar']);

        $context['response']['sidebar']['status'] = $saveSidebar['status'];
        $context['response']['sidebar']['response_msg'] = $saveSidebar['msg'];
    }

    public function saveSidebar($request)
    {
        try {
            $slug_id = $request['slug_id'];
            $use_sidebar = $request['use_sidebar'];
            $left_content_view = $request['left_content_view'];
            $left_content_edit = $request['left_content_edit'];
            $right_content_view = $request['right_content_view'];
            $right_content_edit = $request['right_content_edit'];

            DB::beginTransaction();
            $slug = CoreSlug::find($slug_id);

            if ((0 == $slug->slugble->isCustomSidebar() && !$slug->isHomepage()) || (1 == $slug->slugble->isCustomSidebar() && 2 == $slug->slugble->sidebar_id)) {
                $newSidebar = new CoreSidebar();
                $newSidebar->use_sidebar = $use_sidebar;
                $newSidebar->left_content_view = $left_content_view;
                $newSidebar->left_content_edit = $left_content_edit;
                $newSidebar->right_content_view = $right_content_view;
                $newSidebar->right_content_edit = $right_content_edit;
                $newSidebar->save();

                $slug->slugble->update([
                      'sidebar_id' => $newSidebar->id,
              ]);
            } else {
                $slug->slugble->getSidebar()->update([
                      'use_sidebar' => $use_sidebar,
                      'left_content_view' => $left_content_view,
                      'left_content_edit' => $left_content_edit,
                      'right_content_view' => $right_content_view,
                      'right_content_edit' => $right_content_edit,
              ]);
            }

            DB::commit();

            $response['status'] = 'success';
            $response['msg'] = 'Saved Sidebar success';
        } catch (\Exception $e) {
            DB::rollBack();
            $errorMsg = 'false:'.$e->getMessage().' at line:'.$e->getLine();
            RVLog::error(sprintf('Error Msg %s at %s :: LINE %s ::  %s', $errorMsg, __FILE__, __LINE__, __METHOD__));
            $response['status'] = 'error';
            $response['msg'] = $errorMsg;
        }

        return $response;
    }
}
