<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebLog\PropertyWebLogRepository;

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

class PropertyWebLogService
{

    private $propertyWebLogRepository;



    public function __construct(
        PropertyWebLogRepository $propertyWebLogRepository
    )
    {

        $this->propertyWebLogRepository = $propertyWebLogRepository;

    }

    public function select($param = [], $column = ['*'])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {
            $data = $this->propertyWebLogRepository->findByCriteria($param, $column);
            $response = [
                'status' => true,
                'data' => $data,
            ];
        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function create($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $propertyCreateResult = $this->propertyWebLogRepository->create($params);

            if ($propertyCreateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

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

        return output($response);
    }

    public function update($id, $param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $updateResult = $this->propertyWebLogRepository->update($id, $param);

            if ($updateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

            $updateData = $updateResult["data"];
            $response = [
                'status' => true,
                'data' => $updateData,
            ];

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

        return output($response);
    }

    public function createPropertyWebLog($params){

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

        try
        {
            $searchCriteria = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'ip', 'condition' => '=', 'value' => $params['ip']],
                    ['field' => 'web_id', 'condition' => '=', 'value' => $params['web_id']]
                ],
                'firstRow' => true
            ];

            $searchResult = $this->select($searchCriteria, ['id', 'web_id']);

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


            // Create
            if(empty($searchResult['data'])){

                $createData = [
                    'web_id' => fillOnUndefined($params, "web_id"),
                    'ip' => fillOnUndefined($params, "ip"),
                    'country_code' => fillOnUndefined($params, "country_code"),
                    'device_type' => $params['isDesktop'] ? 'web' : 'mobile',
                    'created_at' => time(),
                    'updated_at' => time()
                ];
                $createStatus = $this->create($createData) ;

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

            }else{
                // Update
                $updateData = [
                    'web_id' => fillOnUndefined($params, "web_id"),
                    'ip' => fillOnUndefined($params, "ip"),
                    'country_code' => fillOnUndefined($params, "country_code"),
                    'device_type' => $params['isDesktop'] ? 'web' : 'mobile',
                    'created_at' => time(),
                    'updated_at' => time()
                ];
                $updateStatus = $this->update($searchResult['data']['id'], $updateData) ;

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

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

        }catch (ApiErrorException $e)
        {
            $response = ['status' => 0, 'statusCode' => 500 ,'message' => $e->getMessage(), 'data' => null];
        }
        catch (Exception $e){
            $message = $e->getFile().' '.$e->getLine().' '.$e->getMessage();
            Log::error($message);
        }

        return output($response);
    }

    public function getPropertyWebLog($param = [], $column = ['*']){

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

            $return = [];


            $searchResult = $this->select($param, $column);

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

            $return = $searchResult['data'];

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => ['property_web_logs' => $return ] ];
        } 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 output($response);
    }




}
