<?php

namespace Themes\kbgolf\pro\app\Http\Controllers;

use App\Facades\ProApiCacheFacade;
use App\Helpers\ResponseConverter;
use App\Helpers\Utils;
use App\Http\Controllers\Controller;
use App\Services\CallApiService;
use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Request;

class IndexController extends Controller
{
    private $callApiService;

    public function __construct(CallApiService $callApiService)
    {
        $this->callApiService = $callApiService;
    }

    public function index()
    {
        $connectionDevice = session()->get('ConnectionDevice');
        $response = ProApiCacheFacade::getCachedResponse('list-type1-book', 'list-type1/home-book');

        if ($this->callApiService->verifyApiError($response)) {
            notify()->error($response['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        $widgetTaxo = $response['Book'][0]['Page'];
        $maMainSlider = ProApiCacheFacade::filterWidgetTaxo($widgetTaxo, 'ma-main-slider', $connectionDevice);

        $seoHtml = Utils::getSeoHtml('main');
        $query = "post_type_id = 59";
        $limit = (int)request('limit', 36);
        $page = (int)request('page', 1);

        // 시세
        $mainSiseList = $this->callApiService->callApi([
            'url' => 'post-page',
            'data' => [
                'PageVars' => [
                    'Query' => $query,
                    'Desc' => 'created_on',
                    'Limit' => $limit,
                    'Offset' => ($page - 1) * $limit
                ]
            ]
        ]);

        // dd($mainSiseList);
        // 유튜브
        $query = "(post_type_id = 40 or post_type_id = 43 or post_type_id = 46)  and status ='1'";
        // $query = "post_type_id = 40 or post_type_id = 43 or post_type_id = 46 and status ='1'";
        $limit = (int)request('limit', 12);
        $page = (int)request('page', 1);
        $mainYoutubeList = $this->callApiService->callApi([
            'url' => 'post-page',
            'data' => [
                'PageVars' => [
                    'Query' => $query,
                    'Desc' => 'created_on',
                    'Limit' => $limit,
                    'Offset' => ($page - 1) * $limit
                ]
            ]
        ]);

        // dd($mainYoutubeList);

        // 블로그
        $query = "(post_type_id = 41 or post_type_id = 44 or post_type_id = 50) and status ='1'";
        $limit = (int)request('limit', 12);
        $page = (int)request('page', 1);
        $mainBlogList = $this->callApiService->callApi([
            'url' => 'post-page',
            'data' => [
                'PageVars' => [
                    'Query' => $query,
                    'Desc' => 'created_on',
                    'Limit' => $limit,
                    'Offset' => ($page - 1) * $limit
                ]
            ]
        ]);

        // dd($mainBlogList);

        // 시세 등락 계산
        if (isset($mainSiseList['Page']) && !empty($mainSiseList['Page'])) {
            foreach ($mainSiseList['Page'] as &$sise) {
                if (isset($sise['Pc3'], $sise['Pc4'])) {
                    $sise['fluctuation'] = (float)$sise['Pc3'] - (float)$sise['Pc4'];
                } else {
                    $sise['fluctuation'] = 0;
                }
            }
        }

        // 블로그 post_code 만들기
        if (isset($mainBlogList['Page']) && !empty($mainBlogList['Page'])) {
            foreach ($mainBlogList['Page'] as &$mainBlog) {
                if (isset($mainBlog['PostTypeId'])) {
                    switch ($mainBlog['PostTypeId']) {
                        case 41:
                            $mainBlog['PostCode'] = 'golf-blog';
                            break;
                        case 44:
                            $mainBlog['PostCode'] = 'condo-blog';
                            break;
                        default:
                            $mainBlog['PostCode'] = 'fitness-blog';
                    }
                }
            }
        }

        if ($this->callApiService->verifyApiError($mainSiseList)) {
            notify()->error($mainSiseList['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        if ($this->callApiService->verifyApiError($mainYoutubeList)) {
            notify()->error($mainYoutubeList['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        if ($this->callApiService->verifyApiError($mainBlogList)) {
            notify()->error($mainBlogList['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        return view('views.index', compact('seoHtml', 'maMainSlider', 'mainSiseList', 'mainYoutubeList' ,'mainBlogList'));
    }
    // 시세 더보기
    public function loadMoreSise(Request $request)
    {
        $query = "post_type_id = 59";
        $offset = (int) $request->query('offset', 0);
        $limit = (int) $request->query('limit', 5);
        $siseType = $request->query('siseType', 'golf');
        if($siseType){
            $query .= " and pc1='$siseType'";
        }

        $moreSiseList=$this->callApiService->callApi([
            'url' => 'post-page',
            'data' => [
                'PageVars' => [
                    'Query' => $query,
                    'Desc' => 'created_on',
                    'Limit' => $limit,
                    'Offset' => $offset
                ]
            ]
        ]);

        // 등락 계산
        if (isset($moreSiseList['Page']) && !empty($moreSiseList['Page'])) {
            foreach ($moreSiseList['Page'] as &$sise) {
                if (isset($sise['Pc3'], $sise['Pc4'])) {
                    $sise['fluctuation'] = (float)$sise['Pc3'] - (float)$sise['Pc4'];
                } else {
                    $sise['fluctuation'] = 0;
                }
            }
        }

        if ($this->callApiService->verifyApiError($moreSiseList)) {
            notify()->error($moreSiseList['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        return $moreSiseList;
    }

    // 유튜브 더보기
    public function loadMoreYoutube(Request $request)
    {
        $query = "post_type_id = 40 or post_type_id = 43 or post_type_id = 46 and status ='2'";
        $offset = (int) $request->query('offset', 0);
        $limit = (int) $request->query('limit', 5);

        $mainYoutubeList=$this->callApiService->callApi([
            'url' => 'post-page',
            'data' => [
                'PageVars' => [
                    'Query' => $query,
                    'Desc' => 'created_on',
                    'Limit' => $limit,
                    'Offset' => $offset
                ]
            ]
        ]);

        if ($this->callApiService->verifyApiError($mainYoutubeList)) {
            notify()->error($mainYoutubeList['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }
        return $mainYoutubeList;
    }

    // 블로그 더보기
    public function loadMoreBlog(Request $request)
    {
        $query = "post_type_id = 41 or post_type_id = 44 or post_type_id = 50  and status ='2'";
        $offset = (int) $request->query('offset', 0);
        $limit = (int) $request->query('limit', 5);

        $moreBlogList=$this->callApiService->callApi([
            'url' => 'post-page',
            'data' => [
                'PageVars' => [
                    'Query' => $query,
                    'Desc' => 'created_on',
                    'Limit' => $limit,
                    'Offset' => $offset
                ]
            ]
        ]);
        // post_code 만들기
        if (isset($moreBlogList['Page']) && !empty($moreBlogList['Page'])) {
            foreach ($moreBlogList['Page'] as &$mainBlog) {
                if (isset($mainBlog['PostTypeId'])) {
                    switch ($mainBlog['PostTypeId']) {
                        case 41:
                            $mainBlog['PostCode'] = 'golf-blog';
                            break;
                        case 44:
                            $mainBlog['PostCode'] = 'condo-blog';
                            break;
                        default:
                            $mainBlog['PostCode'] = 'fitness-blog';
                    }
                }
            }
        }

        if ($this->callApiService->verifyApiError($moreBlogList)) {
            notify()->error($moreBlogList['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }
        return $moreBlogList;
    }
}
