<?php

namespace Rvsitebuilder\Wysiwyg\Http\Controllers\Backend;

use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Rvsitebuilder\Wysiwyg\Events\AfterSave;
use Rvsitebuilder\Wysiwyg\Http\Requests\SaveSiteRequest;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\CreateBlogThumbProcess;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\RemovePHPTagProcess;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\RemoveScriptTagProcess;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\SaveContentProcess;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\SaveFooterProcess;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\SaveHeaderProcess;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\SaveMetaAutoProcess;
use Rvsitebuilder\Wysiwyg\Process\SaveSite\SaveSidebarProcess;
use SRIO\ChainOfResponsibility\ArrayChainContext;
use SRIO\ChainOfResponsibility\ChainRunner;

class SaveController extends Controller
{
    public function saveSite(SaveSiteRequest $request)
    {
        $param = [
          'savetype' => $request->savetype,
          'isHomepage' => $request->isHomepage,
          'header' => [
            'slug_id' => $request->slug_id,
            'content_edit' => $request->header_content_edit,
            'content_view' => $request->header_content_view,
          ],
          'isMasterHeader' => $request->isMasterHeader,
          'isBanner' => $request->isBanner,
          'footer' => [
            'slug_id' => $request->slug_id,
            'content_edit' => $request->footer_content_edit,
            'content_view' => $request->footer_content_view,
            'style' => $request->footerStyle,
          ],
          'isMasterFooter' => $request->isMasterFooter,
          'isCustomFooter' => $request->isCustomFooter,
          'page' => [ // Page / Post
            'slug_id' => $request->slug_id,
            'content_edit' => $request->page_content_edit,
            'content_view' => $request->page_content_view,
          ],
          'meta_auto' => [
            'slug_id' => $request->slug_id,
            'meta_title_auto' => $request->meta_title_auto,
            'meta_des_auto' => $request->meta_des_auto,
          ],
          'sidebar' => [
            'slug_id' => $request->slug_id,
            'use_sidebar' => $request->use_sidebar,
            'isCustom_sidebar' => $request->isCustom_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,
          ],
          'global_post' => $request->global_post,
      ];

        // Save Site Chain
        $SaveRunner = new ChainRunner([
          //** Clean widget data - Backend
          new RemoveScriptTagProcess(),
          new RemovePHPTagProcess(),
          new SaveHeaderProcess(),
          new SaveFooterProcess(),
          new SaveContentProcess(),
          new SaveMetaAutoProcess(),
          new SaveSidebarProcess(),
          new CreateBlogThumbProcess(),
          // CreateFeaturedImageProcess(),
      ]);

        $save_result = (array) $SaveRunner->run(new ArrayChainContext($param));

        $aftersave_result = event(new AfterSave($save_result));

        foreach ($aftersave_result as $v) {
            if ($v) {
                $k = array_keys($v);
                $save_result['response'][$k[0]] =
                            [
                                'status' => $v[$k[0]]['status'],
                                'msg' => $v[$k[0]]['msg'],
                            ];
            }
        }

        return response()->json($save_result['response'], 200);
    }
}
