<?php


namespace App\Http\Controllers\V1;

use App\Core\Service\LanguageService;
use App\Http\Controllers\Controller;
use App\Core\Service\LanguageBaseService ;


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

class LanguageController extends Controller
{

    private $languageService;
    private $languageBaseService ;

    public function __construct(
        LanguageService $languageService,
        LanguageBaseService $languageBaseService
    )
    {
        $this->languageService = $languageService;
        $this->languageBaseService = $languageBaseService ;
    }

    public function getAllLanguages($param)
    {
        $response = ['status' => false,  'message' => '', 'data' => null, 'statusCode' => 500 ];
        try {

            if($param === "all"){

                $languageCriteria = [
                    'criteria' => [
                        ['field' => 'status' , 'condition' => '=', 'value' => 1 ]
                    ] ,
                    "orderBy" => [
                        ["field" => "name", "value" => "ASC"]
                    ]
                ];

            }elseif ($param === "app"){

                $languageCriteria = [
                    'criteria' => [
                        ['field' => 'status' , 'condition' => '=', 'value' => 1 ],
                        ['field' => 'is_application' , 'condition' => '=', 'value' => 1 ],
                        ['field' => 'is_published' , 'condition' => '=', 'value' => 1 ]
                    ] ,
                    "orderBy" => [
                        ["field" => "name", "value" => "ASC"]
                    ]
                ];

            }else{
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $languages = $this->languageService->getAllLanguages($languageCriteria, ['id','code','name','status', 'language_key']);

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



            $response = ['status' => 1, 'statusCode' => 200, 'message' => null,'data' => $languages['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 createApplicationLanguageFiles()
    {
        $response = ['status' => false,  'message' => '', 'data' => null, 'statusCode' => 500 ];
        try {
            $languageBaseResponse = $this->languageBaseService->createApplicationLanguageFiles() ;
            if ($languageBaseResponse['status'] != 'success') {
                throw new ApiErrorException($languageBaseResponse['message']);
            }
            $response = ['status' => 1, 'statusCode' => 200, 'message' => null,'data' => null ];

        } 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 getNullDescriptions()
    {
        $response = ['status' => false,  'message' => '', 'data' => null, 'statusCode' => 500 ];
        try {
            $applicationLanguages = $this->languageService->getApplicationLanguages();
            $applicationLanguages = $applicationLanguages['data'];
            $responseLangTitle = [] ;
            foreach ($applicationLanguages as $applicationLanguage) {
                $langKey = $applicationLanguage['code'];
                $responseLangTitle[] = [
                    'language_code' => $langKey,
                    'description' => isset($titleLangContents[$langKey]) ? $titleLangContents[$langKey] : null
                ];
            }

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

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

}
