<?php


namespace App\Http\Controllers\V1;

use App\Core\Service\BookingPaymentService;
use App\Core\Service\CurrencyService;
use App\Core\Service\PropertyPaymentService;
use App\Exceptions\ApiErrorException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Http\Request ;
use Exception ;
use phpDocumentor\Reflection\Types\Collection;

class PaymentController
{

    private $request ;
    private $propertyPaymentService;
    private $currencyService ;
    private $bookingPaymentService;


    public function __construct(
        Request $request,
        PropertyPaymentService $propertyPaymentService,
        BookingPaymentService $bookingPaymentService,
        CurrencyService $currencyService

    )
    {
        $this->propertyPaymentService = $propertyPaymentService ;
        $this->request = $request ;
        $this->currencyService = $currencyService ;
        $this->bookingPaymentService = $bookingPaymentService ;

    }


    public function getPaymentTypeList(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->getPaymentTypeList($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $currencyList = $this->currencyService->getCurrencyList();

            if($currencyList['status'] != 'success'){
                throw new Exception($currencyList['message']);
            }

            $currencyList = collect($currencyList['data'])->map(function ($value){
                return [
                    'code' => $value['code'],
                    'name' => $value['name'],
                    'language_key' => $value['language_key'],
                ];
            }) ;
            $responseData['payment_types'] = $paymentList['data'] ;
            $responseData['currencies'] = $currencyList ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function getPaymentType(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->getPaymentType($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $paymentList['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function getPaymentMappingList(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->getPaymentMappingList($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $responseData = $paymentList['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function createPaymentMapping(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] =  $this->request->auth->id;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->createPaymentMapping($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $responseData = $paymentList['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePaymentMappingStatus(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] =  $this->request->auth->id;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->updatePaymentMappingStatus($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $responseData = $paymentList['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function setPaymentMappingDefault(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] =  $this->request->auth->id;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->setPaymentMappingDefault($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $responseData = $paymentList['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePaymentMapping(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] =  $this->request->auth->id;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->updatePaymentMapping($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $responseData = $paymentList['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePaymentInstallments(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] =  $this->request->auth->id;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->updateInstallments($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $responseData = $paymentList['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePaymentInstallmentStatus(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] =  $this->request->auth->id;
            $responseData = null ;
            $paymentList = $this->propertyPaymentService->updatePaymentInstallmentStatus($params);

            if($paymentList['status'] != 'success'){
                throw new Exception($paymentList['message']);
            }

            $responseData = $paymentList['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function paymentDashboard(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;
            $paymentDashboard = $this->bookingPaymentService->getPaymentDashboard($params);
            if($paymentDashboard['status'] != "success"){
                throw new ApiErrorException($paymentDashboard['message']);
            }
            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $paymentDashboard['data']];
        } catch (ApiErrorException $e) {
            $responseData['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $responseData['message'] = $e->getMessage();
        }

        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);



    }
}
