<?php

namespace App\Http\Controllers\V1;


use App\Http\Controllers\Controller;
use App\Core\Service\PropertyFactMappingService;


use Predis\Replication\RoleException;
use Illuminate\Http\Request;

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

class PropertyFactMappingController extends Controller
{


    private $request;
    private $propertyFactMappingService;


    public function __construct(
        Request $request,
        PropertyFactMappingService $propertyFactMappingService
    )
    {
        $this->request = $request;
        $this->propertyFactMappingService = $propertyFactMappingService;

    }

    public function getPropertyFactMapping(Request $request)
    {

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

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



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

        } 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 removePropertyFactMapping(Request $request)
    {

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

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

            $params = json_decode($request->getContent(),1);

            $params = $params['property_fact_mapping_remove'] ;

            $requestParams = [
                'active_user' => $this->request->credentials->user_id,
                'facts' => fillOnUndefined($params, 'facts'),
            ];

            $removeResponse = $this->propertyFactMappingService->removePropertyFactMapping($requestParams);


            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $removeResponse];
            //$response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyContent['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 addPropertyFactMapping()
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try
        {
            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = json_decode($this->request->getContent(),1);
            $params = current($params);
            $params['user_id'] =  $this->request->auth->id;

            $response =  $this->propertyFactMappingService->addPropertyFactMapping($params);



        }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 updatePropertyFactMapping(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try
        {
            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = json_decode($this->request->getContent(),1);
            $params = current($params);
            $params['user_id'] =  $this->request->auth->id;

            $response =  $this->propertyFactMappingService->updatePropertyFactMapping($params);

        }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']);
    }




}
