<?php
class KBELocale
{
    /////
    /////////
    private $restapi;
    ////////
    public $utils;
    ////////////
    ////////////////
    private $dbUtils;

    public function __construct($auth, $restapi = null)
    {
        $company = $auth["company"];
        extract($company);
        ///////////////////////////////////////////
        $this->restapi = $restapi;
        $this->utils = new Utils();
        /////////////////////////////////////////////
        $this->dbUtils = new DBUtils($db_name);
        /////////////////////////////////////////
        if (!$this->dbUtils->isDBInService()) {
            die($this->restapi->Response(["message" => "Failed to connect to the database, please try again laiter", "success" => false], 500));
        }
        /////////////////////////////////////////
    }

    public function getLang($lang = "en")
    {
        $response = array();
        $stmt = $this->dbUtils->runQuery("SELECT * FROM  pm_lang WHERE locale=:ulang OR tag=:ulang");
        $stmt->bindparam(":ulang", $lang);
        $stmt->execute();
        $counter = $stmt->rowCount();
        if ($counter > 0) {
            $response = $stmt->fetch(PDO::FETCH_ASSOC);
            return $response;
        } else {
            return null;
        }
        ///////////////
        return $response;
    }

    public function _initLang($lang = "en")
    {
        // TO-DO: Get Token
        // params
        // headers
        // sessions
        $headers = $this->restapi->decodeHeaders();
        /////////////////////////////////
        if ($this->restapi->purge('lang')) {
            $lang = trim($this->restapi->request('lang'));
            if (empty($lang) || is_null($lang)) {
                return $this->getLang('en');
            } else {
                return $this->getLang($lang);
            }
        } else  if ($this->restapi->purge('lang')) {
            $lang = trim($this->restapi->request('lang'));
            if (empty($lang) || is_null($lang)) {
                return $this->getLang('en');
            } else {
                return $this->getLang($lang);
            }
        } else if (array_key_exists("lang", $headers)) {
            $lang = trim($headers["lang"]);
            if (empty($lang) || is_null($lang)) {
                return $this->getLang('en');
            } else {
                return $this->getLang($lang);
            }
        } else {
            if ($lang = null) {
                return $this->getLang('en');
            } else {
                return $this->getLang('en');
            }
        }
    }



    public function getCurrencyById($id)
    {
        $response = array();
        $stmt = $this->dbUtils->runQuery("SELECT * FROM  pm_currency WHERE id=:uid");
        $stmt->bindparam(":uid", $id);
        $stmt->execute();
        $counter = $stmt->rowCount();
        if ($counter > 0) {
            $response = $stmt->fetch(PDO::FETCH_ASSOC);
            return $response;
        } else {
            return null;
        }
        ///////////////
        return $response;
    }

    public function getCurrencyByCode($code)
    {
        $response = array();
        $stmt = $this->dbUtils->runQuery("SELECT * FROM  pm_currency WHERE code=:ucode");
        $stmt->bindparam(":ucode", $code);
        $stmt->execute();
        $counter = $stmt->rowCount();
        if ($counter > 0) {
            $response = $stmt->fetch(PDO::FETCH_ASSOC);
            return $response;
        } else {
            return null;
        }
        ///////////////
        return $response;
    }

    public function getDefaultCurrency()
    {
        $response = array();
        $stmt = $this->dbUtils->runQuery("SELECT * FROM  pm_currency WHERE main=1");
        $stmt->execute();
        $counter = $stmt->rowCount();
        if ($counter > 0) {
            $response = $stmt->fetch(PDO::FETCH_ASSOC);
            return $response;
        } else {
            return null;
        }
        ///////////////
        return $response;
    }
}
