<?php

namespace Themes\Pro\modunawa\app\Http\Controllers\Shop;

use App\Helpers\ResponseConverter;
use App\Http\Controllers\Controller;
use App\Services\CallApiService;
use App\Services\SeoMetaService;
use Illuminate\Pagination\LengthAwarePaginator;

class ItemController extends Controller
{
    private $callApiService;
    private $seoMetaService;

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

    public function index($igroupCode)
    {
        $limit = (int)request('limit', 12);
        $page = (int)request('page', 1);

        if ($igroupCode === 'all') {
            $igroup['IgroupName'] = '전체';
            $p = request('p', '');
            $simpleFilter = "item_name LIKE '%$p%'";
        } else {
            $igroupPick = $this->callApiService->callApi([
                'url' => 'igroup-pick',
                'data' => [
                    'Page' => [
                        ['IgroupCode' => $igroupCode ]
                    ]
                ],
            ]);

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

            $igroup = $igroupPick['Page'][0];
            $simpleFilter = "igroup_code='$igroupCode'";
        }


        $listType1Book = $this->callApiService->callApi([
            'url' => 'list-type1-book',
            'data' => [
                'Book' => [
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/item-gallery',
                            'SimpleFilter' => $simpleFilter,
                            'SubSimpleFilter' => "image_type = 'middle'",
                            'IsntPagination' => false,
                        ],
                        'ListType1Vars' => [
                            'OrderBy' => request('sort', 'mx.created_on desc')
                        ],
                        'PageVars' => [
                            'Limit' => $limit,
                            'Offset' => ($page - 1) * $limit
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/wish',
                            'SubSimpleFilter' => "image_type = 'thumb'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                ]
            ]
        ]);

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

//        dump($listType1Book['Book'][0]['Page']);
        $itemGallery['Page'] = ResponseConverter::joinFor($listType1Book['Book'][0]['Page'], $listType1Book['Book'][1]['Page'], 'C1');
        $itemGallery['Page'] = new LengthAwarePaginator($itemGallery['Page'], $listType1Book['Book'][0]['PageVars']['QueryCnt'],
            $limit, $page, ['path' => request()->url()]);



        return view('views.shop.item-gallery', compact('listType1Book', 'itemGallery', 'igroup'));
    }

    public function show($id)
    {
        $data = $this->seoMetaService->itemDetailsSeo($id);
        if ($data['error']) {
            notify()->error($data['message'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        $item_id = $data['data'];
//        dd($item_id);

        $listType1Book = $this->callApiService->callApi([
            'url' => 'list-type1-book',
            'data' => [
                'Book' => [
                    [
                        'QueryVars' => [
                            'QueryName' => 'point2u::pro:shop/item-details',
                            'SimpleFilter' => "mx.id = $item_id",
                            'SubSimpleFilter' => "image_type = 'middle'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 1
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/item-thm-media',
                            'SimpleFilter' => "mx.id = $item_id",
                            'SubSimpleFilter' => "image_type = 'middle'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 1
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/related-item-gallery',
                            'SimpleFilter' => '',
                            'SubSimpleFilter' => "image_type = 'big'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 100
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/item-details-prenext',
                            'SimpleFilter' => "mx.id = (select max(id) from dbr_item where id < $item_id)",
                            'SubSimpleFilter' => "image_type = 'thumb'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 1
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/item-details-prenext',
                            'SimpleFilter' => "mx.id = (select min(id) from dbr_item where id > $item_id)",
                            'SubSimpleFilter' => "image_type = 'thumb'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 1
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/wish',
                            'SubSimpleFilter' => "image_type = 'thumb'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'post/blog-single-center-std',
                            'SimpleFilter' => "mx.post_type_id = 15 and mx.status = '0'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro/setup',
                            'SimpleFilter' => "setup_code='shop-pay-method'",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                ]
            ]
        ]);

        if ($this->callApiService->verifyApiError($listType1Book) || empty($listType1Book['Book'][0]['Page'])) {
            notify()->error($listType1Book['body'] ?? 'Api Error', 'Error', 'bottomRight');
            return redirect()->back();
        }

//        dd($listType1Book);

        $relatedItemGallery = ResponseConverter::joinFor($listType1Book['Book'][2]['Page'], $listType1Book['Book'][5]['Page'], 'C1');
        $listType1Book['Book'][0]['Page'] = ResponseConverter::joinFor($listType1Book['Book'][0]['Page'], $listType1Book['Book'][5]['Page'], 'C1');

        $itemDetails = $listType1Book['Book'][0]['Page'][0];
        $pointPayrate = $this->makeItemPointPayrate($itemDetails);
        $shopPayMethodSetup = json_decode($listType1Book['Book'][7]['Page'][0]['C1'], true);
//        dump($shopPayMethodSetup);
        return view('views.shop.item-details', compact('listType1Book', 'relatedItemGallery', 'itemDetails', 'pointPayrate', 'shopPayMethodSetup'));
    }

    function makeItemPointPayrate($itemDetails)
    {
//        dd($itemDetails);
        $p2uPercentA = (int)$itemDetails['C11'] === 0 ? 50 : $itemDetails['C11'];
        $msqxpPercentA = (int)$itemDetails['C11'] === 0 ? 50 : $itemDetails['C12'];

        $p2uPercentB = (int)$itemDetails['C13'] === 0 ? 30 : $itemDetails['C13'];
        $cashPercentB = (int)$itemDetails['C13'] === 0 ? 70 : $itemDetails['C14'];

        $result = [
            'crypto1' => [
                'p2up' => (int)(($p2uPercentA * 0.01) * $itemDetails['C7']),
                'msqxp' => (int)(($msqxpPercentA * 0.01) * $itemDetails['C7']),
                'p2up_percent' => (int)$p2uPercentA,
                'msqxp_percent' => (int)$msqxpPercentA,
            ],
            'crypto2' => [
                'p2up' => (int)(($p2uPercentB * 0.01) * $itemDetails['C7']),
                'cash' => (int)(($cashPercentB * 0.01) * $itemDetails['C7']),
                'p2up_percent' => (int)$p2uPercentB,
                'cash_percent' => (int)$cashPercentB,
            ]
        ];

        return $result;
    }
}
