<?php

namespace App\Core\Service;


use App\Exceptions\ApiErrorException;
use App\Core\Repository\SiteConfig\SiteConfigRepository;


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

class SiteConfigService
{

    private $siteConfigRepository;


    public function __construct(
        SiteConfigRepository $siteConfigRepository
    )
    {
        $this->siteConfigRepository = $siteConfigRepository;
    }

    public function create($param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $insertData =
                [
                    "config_key" => fillOnUndefined($param, "config_key"),
                    "config_value_json" => fillOnUndefined($param, "config_value_json"),
                    "key_comment" => fillOnUndefined($param, "key_comment"),
                    "status" => fillOnUndefined($param, "status", 1),
                    "created_by" => fillOnUndefined($param, "created_by"),
                    "updated_by" => fillOnUndefined($param, "updated_by"),
                    "created_at" => time(),
                    "updated_at" => time(),
                ];


            $validationResult = $this->siteConfigCreateValidator->validate($insertData);

            if ($validationResult->errors()->first()) {
                $errors = $validationResult->errors()->all();
                throw new ApiErrorException($errors);
            }


            $userCreateResult = $this->siteConfigRepository->create($insertData);
            if ($userCreateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $userData = $userCreateResult["data"];
            $response = [
                'status' => true,
                'data' => $userData,
            ];

        } 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 select($param = [], $column = ['*'])
    {

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

        try {

            $data = $this->siteConfigRepository->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 update($id, $param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $updateResult = $this->siteConfigRepository->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 siteHints($params){

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

            $return = [];

            $siteConfigRequest = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'config_key', 'condition' => '=', 'value' => 'hint_list'],
                ],
                'firstRow' => 1
            ];

            $siteConfig = $this->siteConfigRepository->findByCriteria($siteConfigRequest);
            $siteConfig = $siteConfig ? $siteConfig : [] ;
            $hintData = fillOnUndefined($siteConfig, 'config_value_json', null);
            $hintData = json_decode($hintData,1);


            foreach ($hintData as $value){

                $return[] = [
                    'title' =>  $value['title'],
                    'content' =>  $value['content'],
                    'icon' => $value['icon'],
                    'url' => $value['url'],
                ];
            }

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


}
