<?php


namespace App\Core\Service;

use  App\Core\Repository\ApiAccessToken\ApiAccessTokenRepository;
use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;


class ApiAccessTokenService
{
    private $apiAccessTokenRepository;



    public function __construct
    (
        ApiAccessTokenRepository $apiAccessTokenRepository
    )
    {
        $this->apiAccessTokenRepository = $apiAccessTokenRepository;

    }

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


            $data = $this->apiAccessTokenRepository->findByCriteria($param, $column);
            if(!$data){
                throw new ApiErrorException(lang('An unknown error occurred'));
            }
            $response['status'] = 1;
            $response['data'] = $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 create($param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $tokenData =
                [
                    "token" => fillOnUndefined($param, "token"),
                    "expire_date" => fillOnUndefined($param, "expire_date"),
                    "user_id" => fillOnUndefined($param, "user_id"),
                    "invalidate" => fillOnUndefined($param, "invalidate"),
                    "created_at" => time(),
                    "updated_at" => time(),

                ];
            $tokenCreateResult = $this->apiAccessTokenRepository->create($tokenData);

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

            $response['status'] = 1;
            $response['data'] = $tokenCreateResult["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 update($id, $param = [])
    {

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

            $updateResult = $this->apiAccessTokenRepository->update($id, $param);
            if ($updateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

            $response['status'] = 1;
            $response['data'] = $updateResult["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);
    }
}
