<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyChannelService;
use Illuminate\Http\Request;

use App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;

class PropertyChannelController
{


    private $request;
    private $propertyChannelService;


    public function __construct(
        Request $request,
        PropertyChannelService $propertyChannelService
    )
    {
        $this->request = $request;
        $this->propertyChannelService = $propertyChannelService;

    }



    public function getPropertyChannel(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;

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'parent_id' => fillOnUndefined($params, 'parent_id'),
                'type' => fillOnUndefined($params, 'type'),
                'channel_category_id' => fillOnUndefined($params, 'channel_category_id'),
                'country_code' => fillOnUndefined($params, 'country_code'),
            ];

            $propertyChannel = $this->propertyChannelService->getPropertyChannels($requestParams);
            if($propertyChannel['status'] != 'success'){
                throw new ApiErrorException($propertyChannel['message']);
            }

            //CHANNEL RESTRICTION PROPERTIES
            foreach ($propertyChannel['data'] as $channelId => $channel) {
                if(!empty($channel['restriction'])) {
                    $channelRestriction = json_decode($channel['restriction'],1);
                    if(!in_array($params['property_id'],$channelRestriction)) {
                        unset($propertyChannel['data'][$channelId]);
                    }
                }
            }


            //dd($propertyChannel['data'], $params['property_id']);

            //TODO: Channell sıralaması için bir alan seçilip kurgulanacak
            //$propertyChannelCollect = collect($propertyChannel['data']);
            //$propertyChannel['data'] = $propertyChannelCollect->sortByDesc('fax', SORT_NUMERIC )->toArray();
            //$propertyChannel['data'] = array_values($propertyChannel['data']);

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyChannel['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 addPropertyChannel(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;

            $requestParams = [
                'parent_id' => fillOnUndefined($params, 'parent_id'),
                'name' => fillOnUndefined($params, 'name'),
                'official_name' => fillOnUndefined($params, 'official_name'),
                'description' => fillOnUndefined($params, 'description'),
                'channel_category_id' => fillOnUndefined($params, 'channel_category_id'),
                'country_code' => fillOnUndefined($params, 'country_code'),
                'currency_code' => fillOnUndefined($params, 'currency_code', []),
                'logo' => fillOnUndefined($params, 'logo'),
                'address' => fillOnUndefined($params, 'address'),
                'zip_code' => fillOnUndefined($params, 'zip_code'),
                'email' => fillOnUndefined($params, 'email'),
                'phone' => fillOnUndefined($params, 'phone'),
                'phone2' => fillOnUndefined($params, 'phone2'),
                'fax' => fillOnUndefined($params, 'fax'),
                'score' => fillOnUndefined($params, 'score'),
                'user_id' => $this->request->auth->id,
            ];


            $propertyChannel = $this->propertyChannelService->addPropertyChannel($requestParams);
            if($propertyChannel['status'] != 'success'){
                throw new ApiErrorException($propertyChannel['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyChannel['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 updatePropertyChannel(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;

            $requestParams = [

                'id' => fillOnUndefined($params, 'id'),
                'parent_id' => fillOnUndefined($params, 'parent_id'),
                'name' => fillOnUndefined($params, 'name'),
                'official_name' => fillOnUndefined($params, 'official_name'),
                'description' => fillOnUndefined($params, 'description'),
                'channel_category_id' => fillOnUndefined($params, 'channel_category_id'),
                'country_code' => fillOnUndefined($params, 'country_code'),
                'currency_code' => fillOnUndefined($params, 'currency_code', []),
                'logo' => fillOnUndefined($params, 'logo'),
                'address' => fillOnUndefined($params, 'address'),
                'zip_code' => fillOnUndefined($params, 'zip_code'),
                'email' => fillOnUndefined($params, 'email'),
                'phone' => fillOnUndefined($params, 'phone'),
                'phone2' => fillOnUndefined($params, 'phone2'),
                'fax' => fillOnUndefined($params, 'fax'),
                'score' => fillOnUndefined($params, 'score'),
                'user_id' => $this->request->auth->id,
            ];

            $propertyChannel = $this->propertyChannelService->updatePropertyChannel($requestParams);
            if($propertyChannel['status'] != 'success'){
                throw new ApiErrorException($propertyChannel['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyChannel['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']);


    }






}
