<?php

class ValuteManager
{

    /**
     * @param int $Valute {@min 0}{@max 4}
     * @param int $Count
     * @return bool
     */
    public static function Check($Valute, $Count)
    {
        if ($Valute < 0 || $Valute > 4) return false;
        if ($Count == 0) return true;

        $read = file_get_contents("configs/DB_" . $_SESSION['user_db'] . "/ValuteManager.json");
        if (($ValuteManagerConfig = json_decode($read,true)) !== null)
        {
            if ($ValuteManagerConfig[$Valute]['Name'] == ''
                || $ValuteManagerConfig[$Valute]['Table'] == ''
                || $ValuteManagerConfig[$Valute]['Column'] == '') return true;

            if ($ValuteManagerConfig[$Valute]['CharacterNameColumn'] != '')
            {
                //$sql = 'SELECT ' . $ValuteManagerConfig[$Valute]['CharacterNameColumn'] . ', ' . $ValuteManagerConfig[$Valute]['Column'] . ' FROM ' . $ValuteManagerConfig[$Valute]['Table'] . ' WHERE ' . $ValuteManagerConfig[$Valute]['CharacterNameColumn'] . ' = \'' . $_SESSION['ActivePlayer'] . '\'';

                $res = App::$DBs[$_SESSION['user_db']]->select(array($ValuteManagerConfig[$Valute]['CharacterNameColumn'] , $ValuteManagerConfig[$Valute]['Column']))->from($ValuteManagerConfig[$Valute]['Table'])->where($ValuteManagerConfig[$Valute]['CharacterNameColumn'],$_SESSION['ActivePlayer'])->get()->result_array();
                //$res = App::$DBs[$_SESSION['user_db']]->Query($sql);
                //$res = App::$DBs[$_SESSION['user_db']]->FetchArr($res);
                if ($res[0][$ValuteManagerConfig[$Valute]['Column']] >= $Count)
                {
                    return true;
                }
            }
            else if($ValuteManagerConfig[$Valute]['AccountNameColumn'] != '')
            {
                //$sql = 'SELECT ' . $ValuteManagerConfig[$Valute]['AccountNameColumn'] . ', ' . $ValuteManagerConfig[$Valute]['Column'] . ' FROM ' . $ValuteManagerConfig[$Valute]['Table'] . ' WHERE ' . $ValuteManagerConfig[$Valute]['AccountNameColumn'] . ' = \'' . $_SESSION['login'] . '\'';
                $res = App::$DBs[$_SESSION['user_db']]->select(array($ValuteManagerConfig[$Valute]['AccountNameColumn'] , $ValuteManagerConfig[$Valute]['Column']))->from($ValuteManagerConfig[$Valute]['Table'])->where($ValuteManagerConfig[$Valute]['AccountNameColumn'],$_SESSION['login'])->get()->result_array();
                //$res = App::$DBs[$_SESSION['user_db']]->Query($sql);
                //$res = App::$DBs[$_SESSION['user_db']]->FetchArr($res);
                if ($res[0][$ValuteManagerConfig[$Valute]['Column']] >= $Count)
                {
                    return true;
                }
            }
        }
        Messages::Add('incorect_count_valute', $ValuteManagerConfig[$Valute]['Name']);
        return false;
    }
    /**
     * Check All Valutes
     * @param $One int
     * @param $Two int
     * @param $Three int
     * @param $Four int
     * @param $Five int
     * @return bool
     */
    public static function CheckFive($One,$Two,$Three, $Four, $Five)
    {
        if ($One != 0) if(!self::Check(0,$One)) return false;
        if ($Two != 0) if(!self::Check(1,$Two)) return false;
        if ($Three != 0) if(!self::Check(2,$Three)) return false;
        if ($Four != 0) if(!self::Check(3,$Four)) return false;
        if ($Five != 0) if(!self::Check(4,$Five)) return false;
        
        return true;
    }
    /**
     * @param int $Valute {@min 0}{@max 4}
     * @param int $Count
     * @return bool
     */
    public static function Take($Valute, $Count)
    {
        if ($Valute < 0 || $Valute > 4) return false;
        if ($Count == 0) return true;

        $read = file_get_contents("configs/DB_" . 0 . "/ValuteManager.json");
        if (($ValuteManagerConfig = json_decode($read,true)) !== null)
        {
            if ($ValuteManagerConfig[$Valute]['Name'] == ''
                || $ValuteManagerConfig[$Valute]['Table'] == ''
                || $ValuteManagerConfig[$Valute]['Column'] == '') return true;

            if ($ValuteManagerConfig[$Valute]['CharacterNameColumn'] != '')
            {

                //$sql = 'SELECT ' . $ValuteManagerConfig[$Valute]['CharacterNameColumn'] . ', ' . $ValuteManagerConfig[$Valute]['Column'] . ' FROM ' . $ValuteManagerConfig[$Valute]['Table'] . ' WHERE ' . $ValuteManagerConfig[$Valute]['CharacterNameColumn'] . ' = \'' . $_SESSION['ActivePlayer'] . '\'';

                $res = App::$DBs[$_SESSION['user_db']]->select(array($ValuteManagerConfig[$Valute]['CharacterNameColumn'] , $ValuteManagerConfig[$Valute]['Column']))->from($ValuteManagerConfig[$Valute]['Table'])->where($ValuteManagerConfig[$Valute]['CharacterNameColumn'],$_SESSION['ActivePlayer'])->get()->result_array();
                //$res = App::$DBs[$_SESSION['user_db']]->Query($sql);
                //$res = App::$DBs[$_SESSION['user_db']]->FetchArr($res);
                if ($res[0][$ValuteManagerConfig[$Valute]['Column']] >= $Count)
                {
                    //$sql = 'UPDATE ' . $ValuteManagerConfig[$Valute]['Table'] . ' SET ' . $ValuteManagerConfig[$Valute]['Column'] . ' = ' . $ValuteManagerConfig[$Valute]['Name'] . ' - ' . $Count . ' WHERE ' . $ValuteManagerConfig[$Valute]['CharacterNameColumn'] . ' = \'' . $_SESSION['ActivePlayer'] . '\'';
                    if(
                    App::$DBs[$_SESSION['user_db']]->set($ValuteManagerConfig[$Valute]['Column'], $ValuteManagerConfig[$Valute]['Column'] . '-' . $Count, FALSE)->where($ValuteManagerConfig[$Valute]['CharacterNameColumn'], $_SESSION['ActivePlayer'])->update($ValuteManagerConfig[$Valute]['Table'])
                    ) {
                        return true;
                    }
                }
            }
            else if($ValuteManagerConfig[$Valute]['AccountNameColumn'] != '') {
                //$sql = 'SELECT ' . $ValuteManagerConfig[$Valute]['AccountNameColumn'] . ', ' . $ValuteManagerConfig[$Valute]['Column'] . ' FROM ' . $ValuteManagerConfig[$Valute]['Table'] . ' WHERE ' . $ValuteManagerConfig[$Valute]['AccountNameColumn'] . ' = \'' . $_SESSION['login'] . '\'';
                //$res = App::$DBs[$_SESSION['user_db']]->Query($sql);
                //$res = App::$DBs[$_SESSION['user_db']]->FetchArr($res);
                $res = App::$DBs[$_SESSION['user_db']]->select(array($ValuteManagerConfig[$Valute]['AccountNameColumn'] , $ValuteManagerConfig[$Valute]['Column']))->from($ValuteManagerConfig[$Valute]['Table'])->where($ValuteManagerConfig[$Valute]['AccountNameColumn'],$_SESSION['login'])->get()->result_array();

                if ($res[0][$ValuteManagerConfig[$Valute]['Column']] >= $Count) {
                    //$sql = 'UPDATE ' . $ValuteManagerConfig[$Valute]['Table'] . ' SET ' . $ValuteManagerConfig[$Valute]['Column'] . ' = ' . $ValuteManagerConfig[$Valute]['Name'] . ' - ' . $Count . ' WHERE ' . $ValuteManagerConfig[$Valute]['AccountNameColumn'] . ' = \'' . $_SESSION['login'] . '\'';
                    if (
                    App::$DBs[$_SESSION['user_db']]->set($ValuteManagerConfig[$Valute]['Column'], $ValuteManagerConfig[$Valute]['Column'] . '-' . $Count, FALSE)->where($ValuteManagerConfig[$Valute]['AccountNameColumn'], $_SESSION['login'])->update($ValuteManagerConfig[$Valute]['Table'])
                ) {
                        return true;
                    }
                }
            }
        }
        Messages::Add('incorect_count_valute', $ValuteManagerConfig[$Valute]['Name']);
        return false;
    }

    /**
     * @param $One int
     * @param $Two int
     * @param $Three int
     * @param $Four int
     * @param $Five int
     */
    public static function TakeFive($One,$Two,$Three, $Four, $Five)
    {
        if ($One != 0) self::Take(0,$One);
        if ($Two != 0) self::Take(1,$Two);
        if ($Three != 0) self::Take(2,$Three);
        if ($Four != 0) self::Take(3,$Four);
        if ($Five != 0) self::Take(4,$Five);
    }

    /**
     * @param int $Valute {@min 0}{@max 4}
     * @param int $Count
     * @return bool
     */
    public static function Reward($Valute, $Count, $Account = null)
    {
        if ($Valute < 0 || $Valute > 4) return false;
        if ($Count == 0) return true;

        $read = file_get_contents("configs/DB_" . 0 . "/ValuteManager.json");
        if (($ValuteManagerConfig = json_decode($read,true)) !== null)
        {
            if ($ValuteManagerConfig[$Valute]['Name'] == ''
                || $ValuteManagerConfig[$Valute]['Table'] == ''
                || $ValuteManagerConfig[$Valute]['Column'] == '') return false;

            if ($Account != null) {
                if ($ValuteManagerConfig[$Valute]['CharacterNameColumn'] != '') {
                    //$sql = 'UPDATE ' . $ValuteManagerConfig[$Valute]['Table'] . ' SET ' . $ValuteManagerConfig[$Valute]['Name'] . ' = ' . $ValuteManagerConfig[$Valute]['Name'] . ' + ' . $Count . ' WHERE ' . $ValuteManagerConfig[$Valute]['CharacterNameColumn'] . ' = \'' . $_SESSION['ActivePlayer'] . '\'';
                    if (App::$DBs[$_SESSION['user_db']]->set($ValuteManagerConfig[$Valute]['Column'], $ValuteManagerConfig[$Valute]['Column'] . '+' . $Count, FALSE)->where($ValuteManagerConfig[$Valute]['CharacterNameColumn'], $_SESSION['ActivePlayer'])->update($ValuteManagerConfig[$Valute]['Table'])) {
                        return true;
                    } else {
                        return false;
                    }
                } else if ($ValuteManagerConfig[$Valute]['AccountNameColumn'] != '') {
                    //$sql = 'UPDATE ' . $ValuteManagerConfig[$Valute]['Table'] . ' SET ' . $ValuteManagerConfig[$Valute]['Name'] . ' = ' . $ValuteManagerConfig[$Valute]['Name'] . ' + ' . $Count . ' WHERE ' . $ValuteManagerConfig[$Valute]['AccountNameColumn'] . ' = \'' . $_SESSION['login'] . '\'';
                    if (App::$DBs[$_SESSION['user_db']]->set($ValuteManagerConfig[$Valute]['Column'], $ValuteManagerConfig[$Valute]['Column'] . '+' . $Count, FALSE)->where($ValuteManagerConfig[$Valute]['AccountNameColumn'], $Account)->update($ValuteManagerConfig[$Valute]['Table'])) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                if ($ValuteManagerConfig[$Valute]['CharacterNameColumn'] != '') {
                    //$sql = 'UPDATE ' . $ValuteManagerConfig[$Valute]['Table'] . ' SET ' . $ValuteManagerConfig[$Valute]['Name'] . ' = ' . $ValuteManagerConfig[$Valute]['Name'] . ' + ' . $Count . ' WHERE ' . $ValuteManagerConfig[$Valute]['CharacterNameColumn'] . ' = \'' . $_SESSION['ActivePlayer'] . '\'';
                    if (App::$DBs[$_SESSION['user_db']]->set($ValuteManagerConfig[$Valute]['Column'], $ValuteManagerConfig[$Valute]['Column'] . '+' . $Count, FALSE)->where($ValuteManagerConfig[$Valute]['CharacterNameColumn'], $_SESSION['ActivePlayer'])->update($ValuteManagerConfig[$Valute]['Table'])) {
                        return true;
                    } else {
                        return false;
                    }
                } else if ($ValuteManagerConfig[$Valute]['AccountNameColumn'] != '') {
                    //$sql = 'UPDATE ' . $ValuteManagerConfig[$Valute]['Table'] . ' SET ' . $ValuteManagerConfig[$Valute]['Name'] . ' = ' . $ValuteManagerConfig[$Valute]['Name'] . ' + ' . $Count . ' WHERE ' . $ValuteManagerConfig[$Valute]['AccountNameColumn'] . ' = \'' . $_SESSION['login'] . '\'';
                    if (App::$DBs[$_SESSION['user_db']]->set($ValuteManagerConfig[$Valute]['Column'], $ValuteManagerConfig[$Valute]['Column'] . '+' . $Count, FALSE)->where($ValuteManagerConfig[$Valute]['AccountNameColumn'], $_SESSION['login'])->update($ValuteManagerConfig[$Valute]['Table'])) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * @param int $One
     * @param int $Two
     * @param int $Three
     * @param int $Four
     * @param int $Five
     */
    public static function RewardFive($One,$Two,$Three, $Four, $Five)
    {
        if ($One != 0) self::Reward(0,$One);
        if ($Two != 0) self::Reward(1,$Two);
        if ($Three != 0) self::Reward(2,$Three);
        if ($Four != 0) self::Reward(3,$Four);
        if ($Five != 0) self::Reward(4,$Five);
    }

    public static function GetTitleValutes($Db)
    {
        $ValutesConfig = json_decode(file_get_contents("configs/DB_" . $Db . "/ValuteManager.json"));
    }
    public static function GetAllValutes($Db)
    {
        $ValutesConfig = json_decode(file_get_contents("configs/DB_" . $Db . "/ValuteManager.json"),true);
        $ResultArr = array();
        $i = 0;
        foreach ($ValutesConfig as $valute)
        {
            if ($valute['Table'] == '') continue;
            $ResultArr[$i]['Name'] = $valute['Name'];
            /**
             * @var $query QueryBuilder
             */
            $query = App::$DBs[$Db]->select($valute['Column'])->from($valute['Table']);
            if ($valute['CharacterNameColumn'] != '')
            {
                $query->where($valute['CharacterNameColumn'], $_SESSION['ActivePlayer']);
            }
            else if ($valute['AccountNameColumn'])
            {
                $query->where($valute['AccountNameColumn'], $_SESSION['login']);
            }
            $ResultArr[$i]['Count'] = $query->get()->result_array();
            $ResultArr[$i]['Count'] = $ResultArr[$i]['Count'][0][$valute['Column']];
            $i++;
        }
        return $ResultArr;
    }

}