<?php

namespace Modules\Subscribe\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
use Modules\Coupon\Services\CouponService;
use Modules\Notification\Enums\ManagementNotificationStatusEnum;
use Modules\Notification\Services\ManagementNotificationService;
use Modules\Notification\Services\NotificationService;
use Modules\Subscribe\Enums\SubscribePaymentStatusEnum;
use Modules\Subscribe\Enums\SubscribeStatusEnum;
use Modules\Subscribe\Services\PaymentService;
use Modules\Subscribe\Interfaces\SubscribeRepositoryInterface;
use Modules\Subscribe\Models\Subscribe;
use Modules\Subscribe\Repositories\Payment\MyFatoorahPayment;
use Modules\Product\Models\ProductAttribute;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Services\AccountService;

class SubscribeService
{
    private SubscribeRepositoryInterface $subscribeRepository ;

    public function __construct(SubscribeRepositoryInterface $subscribeRepository)
    {
        $this->subscribeRepository = $subscribeRepository;
    }

    public function get(Request $request)
    {
        $subscribes= $this->subscribeRepository->get($request);
        return $subscribes;
    }
    public function getItem(Subscribe $subscribe)
    {
        $subscribe = $this->subscribeRepository->getItem($subscribe);
        return $subscribe;
    }
    public function create(Request $request)
    {
        $account = $this->accountService->getAccount();
        if ($request->coupon)
        {
            $coupon = $this->couponService->getItemByCode($request->coupon);
            if ($coupon)
            {
                $request->merge(['coupon_id'=>$coupon->id]);
            }

        }
        $way_id = Session::get('way_id');
        if ($way_id)
        {
            $request->merge(['way_id'=>$way_id]);
        }
        $shipping_id = Session::get('shipping_id');
        if ($shipping_id)
        {
            $request->merge(['shipping_id'=>$shipping_id]);
        }
        $address_id = Session::get('address_id');
        if ($address_id)
        {
            $request->merge(['address_id'=>$address_id]);
        }
        $calculate = $this->cartService->calculate($request);
        foreach ($calculate as $key=>$row)
        {
            $request->merge([$key=>$row]);
        }

        if ($way_id == 1)
        {
            $token = str_replace(str_split('%\\/:*?"<>|.'), '', bcrypt(rand(11111, 99999) . time()));
            $request->merge(['token'=>$token,'user_status'=>$account['status'],'user_id'=>$account['user']->id,'status'=>SubscribeStatusEnum::NEW->value,'payment_status'=>SubscribePaymentStatusEnum::PAID->value]);
            $subscribe = $this->subscribeRepository->create($request);
            $subscribe->delete();
            $content = [
                'token'=>$token,
                'total'=>$calculate['total']
            ];
            if ($subscribe->total == 0)
            {
                $this->subscribeRepository->restore($request,$subscribe->id);
                $this->subscribeRepository->subscribeProductCount($subscribe);
                $this->cartService->destroyAll($request);
                $qr_code = QrCodeService::generate($subscribe->updated_at, $subscribe->total, $subscribe->total_tax);
                $subscribe->update(['qr_image' => $qr_code['image_path']]);
                if ($subscribe->coupon_id)
                {
                    $this->couponService->decrease($subscribe->coupon_id);
                }

                $request->merge(['status'=>ManagementNotificationStatusEnum::NEW_ORDER->value,'subscribe_id'=>$subscribe->id]);
                $this->managementNotificationService->create($request);
                foreach($subscribe->products as $row)
                {
                    if ($row->sub_attribute_id)
                    {
                        $attr = ProductAttribute::where('product_id',$row->product_id)->where('main_attribute_id','!=',null)->where('attribute_id',$row->sub_attribute_id)->first();
                        if ($attr)
                        {
                            $product_count = $attr->count;
                        }
                        else
                        {
                            $product_count = 0;
                        }
                    }
                    else
                    {
                        $product_count = $row->product->count;
                    }
                    if($product_count <= settings('notify_alert'))
                    {
                        $request->merge(['status'=>ManagementNotificationStatusEnum::PRODUCT_EMPTY->value,'product_id'=>$row->product_id]);
                        $this->managementNotificationService->create($request);
                    }
                }

                $url = ($account['status'] == AccountStatusEnum::USER->value) ? route('web.profile') : route('web.home');
            }
            else
            {
                $url = (new PaymentService(new MyFatoorahPayment()))->payment($content);
            }
        }
        else
        {
            $request->merge(['status'=>SubscribeStatusEnum::NEW->value,'payment_status'=>SubscribePaymentStatusEnum::UNPAID->value,'user_status'=>$account['status'],'user_id'=>$account['user']->id]);
            $subscribe = $this->subscribeRepository->create($request);
            $qr_code = QrCodeService::generate($subscribe->updated_at, $subscribe->total, $subscribe->total_tax);
            $subscribe->update(['qr_image' => $qr_code['image_path']]);
            $url = $account['status'] == AccountStatusEnum::VISITOR->value ? route('web.home') : route('web.profile');
            $request->merge(['status'=>$account['status'],'user_id'=>$account['user']->id]);
            $this->subscribeRepository->restore($request,$subscribe->id);
            $this->subscribeRepository->subscribeProductCount($subscribe);
            $this->cartService->destroyAll($request);
            if ($subscribe->coupon_id)
            {
                $this->couponService->decrease($subscribe->coupon_id);
            }

            $request->merge(['status'=>ManagementNotificationStatusEnum::NEW_ORDER->value,'subscribe_id'=>$subscribe->id]);
            $this->managementNotificationService->create($request);
            foreach($subscribe->products as $row)
            {
                if ($row->sub_attribute_id)
                {
                    $attr = ProductAttribute::where('product_id',$row->product_id)->where('main_attribute_id','!=',null)->where('attribute_id',$row->sub_attribute_id)->first();
                    if ($attr)
                    {
                        $product_count = $attr->count;
                    }
                    else
                    {
                        $product_count = 0;
                    }
                }
                else
                {
                    $product_count = $row->product->count;
                }
                if($product_count <= settings('notify_alert'))
                {
                    $request->merge(['status'=>ManagementNotificationStatusEnum::PRODUCT_EMPTY->value,'product_id'=>$row->product_id]);
                    $this->managementNotificationService->create($request);
                }
            }
        }


        return $url;
    }
    public function update(Request $request , $subscribe) :Subscribe
    {
        $subscribe = $this->subscribeRepository->update($request, $subscribe);
        if ($subscribe->user_status == AccountStatusEnum::USER->value)
        {
            $requestData = [
                'status' => $subscribe->status,
                'user_id' => $subscribe->user_id,
                'subscribe_id' => $subscribe->id,
            ];
            $this->notificationService->subscribeChangeStatus($request, $requestData);
        }
        return $subscribe;
    }
    public function paymentStatus(Request $request)
    {
        $subscribe = null;
        if (isset($_GET['paymentId'])) {
            $username = Config::get('app.username');
            $password = Config::get('app.password');
            $tokenApi = Config::get('app.tokenApi');
            $transaction = Config::get('app.transaction');

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $tokenApi);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('grant_type' => 'password', 'username' => $username, 'password' => $password)));
            $result = curl_exec($curl);
            $error = curl_error($curl);
            $info = curl_getinfo($curl);
            curl_close($curl);
            $json = json_decode($result, true);

            $access_token = $json['access_token'];
            $token_type = $json['token_type'];
            if (isset($_GET['paymentId'])) {
                $id = $_GET['paymentId'];
            }
            $url = $transaction . $id;
            $soap_do1 = curl_init();
            curl_setopt($soap_do1, CURLOPT_URL, $url);
            curl_setopt($soap_do1, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($soap_do1, CURLOPT_TIMEOUT, 10);
            curl_setopt($soap_do1, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($soap_do1, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($soap_do1, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($soap_do1, CURLOPT_POST, false);
            curl_setopt($soap_do1, CURLOPT_POST, 0);
            curl_setopt($soap_do1, CURLOPT_HTTPGET, 1);
            curl_setopt($soap_do1, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json', 'Authorization: Bearer ' . $access_token));
            $result_in = curl_exec($soap_do1);

            $err_in = curl_error($soap_do1);
            $file_contents = htmlspecialchars(curl_exec($soap_do1));
            curl_close($soap_do1);
            $getRecorById = json_decode($result_in, true);

            $customFiles = explode('&', $getRecorById['ApiCustomFileds']);
            foreach ($customFiles as $value) {
                $field = explode('=', $value);
                $fields[$field[0]] = $field[1];
            }

            $token = $fields['token'];

            $subscribe = $this->subscribeRepository->getItemByToken($token);

            if (in_array($getRecorById['TransactionStatus'], array(1, 2)) && $subscribe) {

                $account = $this->accountService->getAccount();
                $this->subscribeRepository->restore($request,$subscribe->id);
                $qr_code = QrCodeService::generate($subscribe->updated_at, $subscribe->total, $subscribe->total_tax);
                $request->merge(['payment_log' => $result_in, 'payment_id' => $getRecorById['InvoiceId'],'token'=>null,'qr_image' => $qr_code['image_path']]);
                $this->subscribeRepository->update($request,$subscribe);
                $this->subscribeRepository->subscribeProductCount($subscribe);
                $request->merge(['status'=>$account['status'],'user_id'=>$account['user']->id]);
                $this->cartService->destroyAll($request);
                if ($subscribe->coupon_id) {
                    $this->couponService->decrease($subscribe->coupon_id);
                }
                $request->merge(['status'=>ManagementNotificationStatusEnum::NEW_ORDER->value,'subscribe_id'=>$subscribe->id]);
                $this->managementNotificationService->create($request);
                foreach($subscribe->products as $row)
                {
                    if ($row->sub_attribute_id)
                    {
                        $attr = ProductAttribute::where('product_id',$row->product_id)->where('main_attribute_id','!=',null)->where('attribute_id',$row->sub_attribute_id)->first();
                        if ($attr)
                        {
                            $product_count = $attr->count;
                        }
                        else
                        {
                            $product_count = 0;
                        }
                    }
                    else
                    {
                        $product_count = $row->product->count;
                    }
                    if($product_count <= settings('notify_alert'))
                    {
                        $request->merge(['status'=>ManagementNotificationStatusEnum::PRODUCT_EMPTY->value,'product_id'=>$row->product_id]);
                        $this->managementNotificationService->create($request);
                    }
                }
            }
        }
        return $subscribe;
    }

}
