<?php

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

use App\Http\Controllers\Controller;
use App\Services\CallApiService;
use Carbon\Carbon;
use Illuminate\Pagination\LengthAwarePaginator;

class WishController extends Controller
{
    private $callApiService;

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

    public function index()
    {
        $listType1Book = $this->callApiService->callApi([
            'url' => 'list-type1-book',
            'data' => [
                'Book' => [
                    [
                        '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]);

        return view('views.shop.wish-list', compact('listType1Book'));
    }

    public function favorite()
    {
        if (empty(request('item_id'))) {
            notify()->error('상품을 선택해주세요', 'Error', 'bottomRight');
            return redirect()->back();
        }

        $wishPick = $this->callApiService->callApi([
            'url' => 'wish-pick',
            'data' => [
                'Page' => [
                    [
                        'ItemId' => (int)request('item_id'),
                        'MemberBuyerId' => session('member')['MemberBuyerId']
                    ]
                ]
            ],
        ]);

        if (! $this->callApiService->verifyApiError($wishPick)) {
            notify()->error('이미 위시 리스트에 존재합니다', 'Error', 'bottomRight');
            return redirect()->back();
        }

        $item = $this->callApiService->callApi([
            'url' => 'item-pick',
            'data' => [
                'Page' => [
                    ['Id' => (int)request('item_id') ]
                ]
            ],
        ])['Page'][0];

        $wishAct = $this->callApiService->callApi([
            'url' => 'wish-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => 0,
                        'WishDate' => Carbon::now()->format('Ymd'),
                        'ItemId' => $item['Id'],
                        'WishQty' => request('wish_qty', '1'),
                        'WishPrc' => (string)$item['SalesPrc'],
                        'WishAmt' => (string)$item['SalesPrc'],
                        'IsSubscribed' => '0',
                    ]
                ]
            ]
        ]);

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

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');
        return redirect()->back();
    }

    public function destroy()
    {
        $wishPick = $this->callApiService->callApi([
            'url' => 'wish-pick',
            'data' => [
                'Page' => [
                    [
                        'ItemId' => (int)request('item_id'),
                        'MemberBuyerId' => session('member')['MemberBuyerId']
                    ]
                ]
            ],
        ]);

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

        $wishAct = $this->callApiService->callApi([
            'url' => 'wish-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => -$wishPick['Page'][0]['Id'],
                    ]
                ]
            ],
        ]);

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

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');
        return redirect()->back();
    }
}
