<?php

/**
 * Created by PhpStorm.
 * User: MainUser
 * Date: 26.06.2016
 * Time: 18:31
 */
class Vote extends AccountModule
{
    public $VoteConfig;
    public function __construct()
    {
        $this->VoteConfig = App::$Configs->mod_Vote;
    }

    public function ParseTxtMMOTop($url, $Account)
    {
        $handle = fopen($url, "r");
        $count = 0;
        while ($userinfo = fscanf($handle, "%s\t%s\t%s\t%s\t%s\t%s")) {
            //list ($id, $date, $time, $ip, $login, $type) = $userinfo;
            if ($userinfo[4] == $Account) {
                $List[] = $userinfo;
                $count++;
            }
        }
        fclose($handle);
        return $List;
    }

    public function GetListMMOTop()
    {
        $AccountID = $_SESSION['login'];
        /*if (!file_exists($this->VoteConfig['MMOTop']['Link'])) {
            return false;
        } */
        if (!$this->VoteConfig['MMOTop']['Enable']) {
            return false;
        }
        $ListTxt = $this->ParseTxtMMOTop($this->VoteConfig['MMOTop']['Link'] , $AccountID);

        $ListSql = App::$DBs[$_SESSION['user_db']]->select('*')->from('mmotop_vote')->where('vote_acc', $AccountID)->get()->result_array();

        if (!empty($ListSql)) {
            for ($i=0; $i < count($ListSql); $i++) {
                foreach ($ListTxt as $key => $value) {
                    if ($value[0] == $ListSql[$i]['vote_id']) {
                        unset($ListTxt[$key]);
                        sort($ListTxt);
                    }
                }
            }
        }
        for ($i=0; $i < count($ListTxt); $i++) {
            if ($ListTxt[$i][5] == 1) {
                $ListTxt[$i]['Valutes']['Count'] = $this->VoteConfig['MMOTop']['Reward']['Normal']['Valutes'];
                $ListTxt[$i]['Valutes']['Type'] = 'Normal';
            }
            else if ($ListTxt[$i][5] == 2) {
                $ListTxt[$i]['Valutes']['Count'] = $this->VoteConfig['MMOTop']['Reward']['SMS']['Valutes'];
                $ListTxt[$i]['Valutes']['Type'] = 'SMS';
            }
        }
        return $ListTxt;
    }

    public function GetRewardMMOTop($id)
    {
        if ($this->AccountOnline)
        {
            return false;
        }
        if (!$this->VoteConfig['MMOTop']['Enable'])
            return false;

        $AccountID = $_SESSION['login'];

        /*if (!file_exists($this->VoteConfig['MMOTop']['Link'])) {
            return false;
        }*/
        $ListTxt = $this->ParseTxtMMOTop($this->VoteConfig['MMOTop']['Link'] , $AccountID);

        foreach ($ListTxt as $key => $value) {
            if ($id == $value[0]) {
                if (! App::$DBs[$_SESSION['user_db']]->from('mmotop_vote')->where(array('vote_acc' => $AccountID, 'vote_id' => $id, 'vote_time' => strtotime($value[1]. " " .$value[2])))->count_all_results() ) {

                    if (App::$DBs[$_SESSION['user_db']]
                        ->insert('mmotop_vote', array('vote_id'=>$id, 'vote_time'=> strtotime($value[1]. " " .$value[2] ), 'vote_ip' => $value[3], 'vote_acc'=> $value[4], 'vote_type' =>  $value[5] ))) {

                        if ($value[5] == 1) {
                            ValuteManager::RewardFive($this->VoteConfig['MMOTop']['Reward']['Normal']['Valutes'][0],$this->VoteConfig['MMOTop']['Reward']['Normal']['Valutes'][1],$this->VoteConfig['MMOTop']['Reward']['Normal']['Valutes'][2],$this->VoteConfig['MMOTop']['Reward']['Normal']['Valutes'][3],$this->VoteConfig['MMOTop']['Reward']['Normal']['Valutes'][4]);
                            Messages::Add('success_vote_mmotop_normal');
                            Logs::WriteLog("Account: [{$AccountID}] Success get reward to vote. Top: [MMOTop], Type: [Normal], ID: [{$value[0]}]","Modules.log");
                            return true;
                        }
                        else if ($value[5] == 2) {
                            ValuteManager::RewardFive($this->VoteConfig['MMOTop']['Reward']['SMS']['Valutes'][0],$this->VoteConfig['MMOTop']['Reward']['SMS']['Valutes'][1],$this->VoteConfig['MMOTop']['Reward']['SMS']['Valutes'][2],$this->VoteConfig['MMOTop']['Reward']['SMS']['Valutes'][3],$this->VoteConfig['MMOTop']['Reward']['SMS']['Valutes'][4]);
                            Messages::Add('success_vote_mmotop_sms');
                            Logs::WriteLog("Account: [{$AccountID}] Success get reward to vote. Top: [MMOTop], Type: [SMS], ID: [{$value[0]}]","Modules.log");
                            return true;
                        }
                    }
                }
            }
        }
        Messages::Add('fail_vote_mmotop');
        Logs::WriteLog("Account: [{$AccountID}] Fail to get vote reward. Top: [MMOTop], ID: [{$value[0]}]","Modules.log");
        return false;
    }
    public function ParseTxtQTop($url, $Account)
    {
        $handle = fopen($url, "r");
        $count = 0;
        $List = array();
        while (!feof($handle)) {
            # Считываем построчно данные из блокнота
            $str = fgets($handle, 1024);
            # Разбиваем строку $str на отдельные элементы, используя в качестве разделителя ||
            list($vote['id'], $vote['date'], $vote['time'], $vote['ip'], $vote['user_name'], $vote['type']) = explode('||', $str);

            $vote['type'] = trim($vote['type']);

            if ($vote['user_name'] == $Account) {
                $List[] = $vote;
                $count++;
            }
        }
        fclose($handle);
        return $List;

    }
    public function GetListQTop()
    {
        $AccountID = $_SESSION['login'];
        /*if (!file_exists($this->VoteConfig['QTop']['Link'])) {
            return false;
        }*/
        if (!$this->VoteConfig['QTop']['Enable']) {
            return false;
        }
        $ListTxt = $this->ParseTxtQTop($this->VoteConfig['QTop']['Link'] , $AccountID);

        $ListSql = App::$DBs[$_SESSION['user_db']]->select('*')->from('qtop_vote')->where('v_username', $AccountID)->get()->result_array();

        if (!empty($ListSql)) {
            for ($i=0; $i < count($ListSql); $i++) {
                foreach ($ListTxt as $key => $value) {
                    if ($value['id'] == $ListSql[$i]['v_id']) {
                        unset($ListTxt[$key]);
                        sort($ListTxt);
                    }
                }
            }
        }
        for ($i=0; $i < count($ListTxt); $i++) {
            if ($ListTxt[$i]['type'] == 1) {
                $ListTxt[$i]['Valutes'] = $this->VoteConfig['QTop']['Reward']['Normal']['Valutes'];
                $ListTxt[$i]['Valutes']['Type'] = 'Normal';
            }
            else if ($ListTxt[$i]['type'] == 2) {
                $ListTxt[$i]['Valutes'] = $this->VoteConfig['QTop']['Reward']['SMS']['Valutes'];
                $ListTxt[$i]['Valutes']['Type'] = 'SMS';
            }
        }
        return $ListTxt;
    }

    public function GetRewardQTop($id)
    {
        if ($this->AccountOnline)
        {
            return false;
        }
        if (!$this->VoteConfig['QTop']['Enable'])
            return false;

        $AccountID = $_SESSION['login'];

        /*if (!file_exists($this->VoteConfig['QTop']['Link'])) {
            return false;
        }*/
        $ListTxt = $this->ParseTxtQTop($this->VoteConfig['QTop']['Link'] , $AccountID);

        foreach ($ListTxt as $key => $value) {
            if ($id == $value['id']) {
                if (! App::$DBs[$_SESSION['user_db']]->from('qtop_vote')->where(array('v_username' => $AccountID, 'v_id' => $id, 'v_time' => strtotime($value['date']. " " .$value['time'])))->count_all_results() ) {

                    if (App::$DBs[$_SESSION['user_db']]
                        ->insert('qtop_vote', array('v_id'=>$id, 'v_time'=> strtotime($value['date']. " " .$value['time'] ), 'v_ip' => $value['ip'], 'v_username'=> $value['user_name'], 'v_type' =>  $value['type'] ))) {

                        if ($value['type'] == 1) {
                            ValuteManager::RewardFive($this->VoteConfig['QTop']['Reward']['Normal']['Valutes'][0],$this->VoteConfig['QTop']['Reward']['Normal']['Valutes'][1],$this->VoteConfig['QTop']['Reward']['Normal']['Valutes'][2],$this->VoteConfig['QTop']['Reward']['Normal']['Valutes'][3],$this->VoteConfig['QTop']['Reward']['Normal']['Valutes'][4]);
                            Messages::Add('success_vote_qtop_normal');
                            Logs::WriteLog("Account: [{$AccountID}] Success get reward to vote. Top: [QTop], Type: [Normal], ID: [{$value['id']}]","Modules.log");
                            return true;
                        }
                        else if ($value['type'] == 2) {
                            ValuteManager::RewardFive($this->VoteConfig['QTop']['Reward']['SMS']['Valutes'][0],$this->VoteConfig['QTop']['Reward']['SMS']['Valutes'][1],$this->VoteConfig['QTop']['Reward']['SMS']['Valutes'][2],$this->VoteConfig['QTop']['Reward']['SMS']['Valutes'][3],$this->VoteConfig['QTop']['Reward']['SMS']['Valutes'][4]);
                            Messages::Add('success_vote_qtop_sms');
                            Logs::WriteLog("Account: [{$AccountID}] Success get reward to vote. Top: [QTop], Type: [SMS], ID: [{$value['id']}]","Modules.log");
                            return true;
                        }
                    }
                }
            }

        }
        Messages::Add('fail_vote_qtop');
        Logs::WriteLog("Account: [{$AccountID}] Fail to get vote reward. Top: [QTop], ID: [{$value['id']}]","Modules.log");
        return false;
    }


}