<?php


namespace App\Core\Service;

use App\Core\Repository\Language\LanguageRepository;

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


class LanguageService
{
    private $languageRepository;


    public function __construct
    (
        Request $request,
        LanguageRepository $languageRepository
    )
    {
        $this->request = $request;
        $this->languageRepository = $languageRepository;
    }



    public function select($param = [], $column = ['*'])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $data = $this->languageRepository->findByCriteria($param, $column);
            $response['status'] = 1;
            $response['data'] = $data;


        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $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($param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $languageData =
                [
                    "name" => fillOnUndefined($param, "name"),
                    "rating" => fillOnUndefined($param, "rating"),
                    "longitude" => fillOnUndefined($param, "longitude"),
                    "latitude" => fillOnUndefined($param, "latitude"),
                    "status" => fillOnUndefined($param, "status", 0),
                    "currency_type" => fillOnUndefined($param, "currency_type"),
                    "created_by" => fillOnUndefined($param, "created_by"),
                    "updated_by" => null,
                    "created_at" => time(),
                    "updated_at" => time(),

                ];

            $validationResult = $this->languageCreateValidator->validate($languageData);

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


            $languageCreateResult = $this->languageRepository->create($languageData);

            if ($languageCreateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $response['status'] = 1;
            $response['data'] = $languageCreateResult["data"];

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $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 getAllLanguages($languageCriteria, $column = ['*'])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $languages =$this->languageRepository->findByCriteria($languageCriteria,$column);

            if(!$languages){
                throw new ApiErrorException(lang('Language data not found'));
            }

            $response['status'] = 1;
            $response['data'] = $languages;

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $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 getApplicationLanguages()
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $languageCriteria = [
                'criteria' => [
                    ['field' => 'status' , 'condition' => '=', 'value' => 1 ],
                    ['field' => 'is_application' , 'condition' => '=', 'value' => 1 ],
                    ['field' => 'is_published' , 'condition' => '=', 'value' => 1 ]
                ] ,
                "orderBy" => [
                    ["field" => "name", "value" => "ASC"]
                ]
            ];
            $languageData = $this->select($languageCriteria, ['id','code','name','status', 'language_key']);
            $response['status'] = 1;
            $response['data'] = $languageData['data'];

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $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 propertyLanguageSpoken($params)
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $languageCriteria = [
                'criteria' => [
                    ['field' => 'status' , 'condition' => '=', 'value' => 1 ],
                ] ,
                "orderBy" => [
                    ["field" => "name", "value" => "ASC"]
                ]
            ];
            $languageData = $this->select($languageCriteria, ['id','code','name','status', 'language_key']);
            $spokenLanguages = $params['selected_languages'];
            $responseLanguage = [] ;

            foreach ($languageData['data'] as $language) {
                $responseItem = $language ;
                $responseItem['is_selected'] = array_search($language['code'], $spokenLanguages) > -1 ? true : false ;
                $responseLanguage[] = $responseItem ;

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

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

        return output($response);
    }

}
