<?php
class ReferralSystem extends AccountModule
{
    public $Config;

    public function __construct()
    {
        parent::__construct();
        $this->Config = App::$Configs->mod_ReferralSystem;
    }


    public static function AddReferral($ReferrerNick,$Account,$Db = 0 )
    {
        $Account = App::Validate($Account);
        $ReferrerNick = App::Validate($ReferrerNick);
        // $sql = 'SELECT AccountID FROM Character WHERE Name = \'' . $ReferrerNick . '\'';
        $result = App::$DBs[$Db]->select('AccountID')->from('Character')->where('Name', $ReferrerNick)->get();
        if ($result->num_rows()) {
            $result = $result->result_array();
            $AccountID = $result[0]['AccountID'];
            //$sql = 'UPDATE MEMB_INFO SET referer = \'' . $AccountID . '\' WHERE memb___id = \'' . $Account . '\'';
            if (App::$DBs[$Db]->where('memb___id',$Account)->update('MEMB_INFO', array('referer'=>$AccountID))) {
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }
    public function GetListReferral()
    {
        if ($this->AccountOnline){
        }
        // $sql = 'SELECT memb___id, RewardReferral FROM MEMB_INFO WHERE referer = \'' . $_SESSION['login'] . '\'';
        $Accounts = App::$DBs[$_SESSION['user_db']]->select('memb___id, RewardReferral')->from('MEMB_INFO')->where('referer', $_SESSION['login'])->get()->result_array();
        for ($i = 0; $i < count($Accounts); $i++)
        {
            $Chars[$i] = Character::GetActivePlayerInfo(null,$Accounts[$i]['memb___id']);
            if ($Chars[$i] != null) {
                $Chars[$i] = $Chars[$i][0];
                $ListRewardSql = explode( ',',$Accounts[$i]['RewardReferral']);
                $ListReward = array(1,2,3,4,5);
                $Chars[$i]['ListReward'] = array_diff($ListReward, $ListRewardSql);
            }
        }
        return $Chars;
    }
    public function GetRewardReferral($Step, $Referral, $Nick )
    {
        if ($this->AccountOnline){
            return;
        }
        $Step = App::Validate($Step);
        $Referral = App::Validate($Referral);
        $Nick = App::Validate($Nick[0][0]);

        //$sql = 'SELECT memb___id, RewardReferral FROM MEMB_INFO WHERE referer = \'' . $_SESSION['login'] . '\'';
        $Accounts = App::$DBs[$_SESSION['user_db']]->select('memb___id, RewardReferral')->from('MEMB_INFO')->where('referer', $_SESSION['login'])->get()->result_array();

       
        //$sql = 'SELECT AccountID, Resets  FROM Character WHERE Name = \'' . $Referral . '\'';
        $Character = App::$DBs[$_SESSION['user_db']]->select(array('AccountID', App::$DBs[$_SESSION['user_db']]->column_names['Resets']))->from('Character')->where('Name',$Referral)->get()->result_array();
        for ($i = 0; $i < count($Accounts); $i++){
            if (App::$Configs->user['LoginToLower']){
                $Character[0]['AccountID'] = strtolower($Character[0]['AccountID']);
            }
            if (App::$Configs->user['LoginToLower']){
                $Accounts[$i]['memb___id'] = strtolower($Accounts[$i]['memb___id']);
            }

            if ($Character[0]['AccountID'] == $Accounts[$i]['memb___id']) {
                $ReferralAccount = $Character[0]['AccountID'];
                $ReferralReset = $Character[0][App::$DBs[$_SESSION['user_db']]->column_names['Resets']];
                $ReferralReward = $Accounts[$i]['RewardReferral'];
                
                $ReferralReward = explode(',', $ReferralReward);
               
                if (in_array($Step, $ReferralReward))
                    return;
                $ReferralReward[] = $Step;
                $ReferralReward = implode(',', $ReferralReward);
                if ($ReferralReset >= $this->Config['Steps'][$Step]['NeedReset']) {
                    //$sqlReferral = 'UPDATE MEMB_INFO SET RewardReferral = \'' . $ReferralReward . '\' WHERE memb___id = \'' . $ReferralAccount . '\'';
                    App::$DBs[$_SESSION['user_db']]->where('memb___id', $ReferralAccount)->update('MEMB_INFO', array('RewardReferral'=>$ReferralReward));

                    Logs::WriteLog("Module: [ReferralSystem], Account [{$_SESSION['login']}]. Success get reward Referral Account({$ReferralAccount}), Step({$Step})", 'Modules.log');
                    Messages::Add('success_referralsystem');
                    ValuteManager::RewardFive($this->Config['Steps'][$Step]['Valutes'][0],$this->Config['Steps'][$Step]['Valutes'][1],$this->Config['Steps'][$Step]['Valutes'][2],$this->Config['Steps'][$Step]['Valutes'][3],$this->Config['Steps'][$Step]['Valutes'][4]);

                }

            }
        }
        Logs::WriteLog("Module: [ReferralSystem], Account [{$_SESSION['login']}]. Fail get reward Referral({$Referral}), Step({$Step})", 'Modules.log');
        Messages::Add('fail_referralsystem');
    }
    public static function GetReferralLink()
    {
        //var_dump($_SERVER);
        $Character = Character::GetActivePlayerInfo();
        $Character = $Character[0];
        return 1;
    }

}