<?php
/**
 *
 */
class Donate
{
    public $Donate;
    private $Category;
    private $Items;
    public $Enable;
    
    
    function __construct()
    {
        $this->Category = array();
        $this->Donate = App::$Configs->mod_Donate;

        $this->Enable = $this->Donate['Enable'];
        unset($this->Donate['Enable']);

        $this->Items = new Item();
        $this->Items->LoadItemBase();

        $i = 0;

        foreach ($this->Donate as $key => $item) {
            if (!in_array($this->Donate[$key]['Type'], $this->Category)) {
                $this->Category[] = $this->Donate[$key]['Type'];
            }
            $i++;
        }


        sort($this->Category);
    }
    function getCategoryName($cat){
        switch ($cat) {
            case 0:	return 'Swords'; break;
            case 1:	return 'Axes'; break;
            case 2:	return 'Scepters'; break;
            case 3:	return 'Spears'; break;
            case 4:	return 'Bows'; break;
            case 5:	return 'Staffs'; break;
            case 6:	return 'Shields'; break;
            case 7:	return 'Helms'; break;
            case 8:	return 'Armors'; break;
            case 9:	return 'Pants'; break;
            case 10: return 'Gloves'; break;
            case 11: return 'Boots'; break;
            case 12: return 'Wings/Ords/Seeds/Boxes'; break;
            case 13: return 'Miscs'; break;
            case 14: return 'Jewels/Potions/Tickets'; break;
            case 15: return 'Skills'; break;
            default: return 'Unknown';break;
        }
    }
    function getCategoryImage($cat){
        return "/web/resource/images/don-cat/" . $cat . ".gif";
    }
    function getCategoryesInfo(){
        $i = 0;
        $Info = array();
        
        foreach ($this->Category as $cat) {
            $Info[$i]['Name'] = $this->getCategoryName($cat);
            $Info[$i]['Img'] = $this->getCategoryImage($cat);
            $Info[$i]['Category'] = $cat;
            $i++;
        }

        return $Info;
    }
    function getCategoryItems($category){
        $DonItems = array();
        $i = 0;
    
        foreach ($this->Donate as $key => $item) {
            if ($item['Type'] == $category) {
                $itemo = $this->Items->GetItemInfo($this->Items->CreateItem($item['Type'], $item['Index'], $item['Level'], $item['Skill'], $item['Luck'], $item['Opt'], $item['Excellent']));
                $DonItems[$i]['Key'] = $key;
                $DonItems[$i]['Name'] = $itemo['name'];
                $DonItems[$i]['Info'] = "<div onmouseover=\"Tip('".$itemo['info']."');\" onmouseout=\"UnTip();\" style='background: url(".$itemo['thumb'] . ") center center no-repeat;' class='cat-don'></div>";
                $DonItems[$i]['Price'] = $item['Price'];
                $i++;
            }
        }
        return $DonItems;
    }

    function getSortItems(){
        $DonItems = array();
        $i = 0;
    
        foreach ($this->Donate as $key => $item) {
            if ($item['Type'] == $category) {
                $itemo = $this->Items->GetItemInfo($this->Items->CreateItem($item['Type'], $item['Index'], $item['Level'], $item['Skill'], $item['Luck'], $item['Opt'], $item['Excellent']));
                $DonItems[$i]['Key'] = $key;
                $DonItems[$i]['Name'] = $itemo['name'];
                $DonItems[$i]['Info'] = "<div onmouseover=\"Tip('".$itemo['info']."');\" onmouseout=\"UnTip();\" style='background: url(".$itemo['thumb'] . ") center center no-repeat;' class='cat-don'></div>";
                $DonItems[$i]['Price'] = $item['Price'];
                $i++;
            }
        }
        return $DonItems;
    }

    function Buy($item){
        
        $DonItem = $this->Donate[$item];
        //Check to Valutes
        if (!ValuteManager::CheckFive($this->Donate[$item]['Price']['Valutes'][0],$this->Donate[$item]['Price']['Valutes'][1],$this->Donate[$item]['Price']['Valutes'][2],$this->Donate[$item]['Price']['Valutes'][3],$this->Donate[$item]['Price']['Valutes'][4])) {
            return false;
        }
        App::$DBs[$_SESSION['user_db']]->simple_query("declare @it varbinary(1920); set @it=(select [Items] from [warehouse] where [AccountID]='".$_SESSION['login']."'); print @it");
        $inv_items = mssql_get_last_message();
        $inv_items = substr($inv_items,2);

        $col_i = strlen($inv_items)/32;
        $str =0;
        if (round($col_i) == 0) {
            return false;
        }
        $RewardItem = $this->Items->CreateItem($DonItem['Type'], $DonItem['Index'], $DonItem['Level'], $DonItem['Skill'], $DonItem['Luck'], $DonItem['Opt'], $DonItem['Excellent']);
        $itemo = $this->Items->GetItemInfo($RewardItem);
        $SmartSearch = $this->Items->SmartSearch($inv_items,$itemo['X'],$itemo['Y']);

        if($SmartSearch != -1){

            $returnItem = substr_replace($inv_items,$RewardItem,($SmartSearch*32),32);

            ValuteManager::TakeFive($this->Donate[$item]['Price']['Valutes'][0],$this->Donate[$item]['Price']['Valutes'][1],$this->Donate[$item]['Price']['Valutes'][2],$this->Donate[$item]['Price']['Valutes'][3],$this->Donate[$item]['Price']['Valutes'][4]);
            if(App::$DBs[$_SESSION['user_db']]->simple_query('UPDATE warehouse SET Items= 0x'.$returnItem.' WHERE AccountID = \''.($_SESSION["login"]).'\'')){
                Messages::Add('Success buy '. $itemo['Name']);
                Logs::WriteLog("Account: {$_SESSION['login']} Success buy item {$itemo['Name']}, ItemID = {$itemo['Name']}}",'modules.log');
                return true;
            }
            Logs::WriteLog("Account: {$_SESSION['login']} Fail to buy item {$itemo['Name']}, ItemID = {$itemo['Name']}}",'modules.log');
            Messages::Add('Donate fail');
            return false;
        }

        return false;
    }
    function getDonateList(){
        return $this->Donate;
    }
    public static function UpdateItem($id,$Db)
    {
        $Config = file_get_contents("configs/modules/DB_" . $Db . "/Donate.json");
        $Config = json_decode($Config,true);
        $Item = $Config[$id];

        if (property_exists(App::$Post,'add_don_type') && App::$Post->add_don_type != '')
            $Item['Type'] = App::$Post->add_don_type;
        else
            $Item['Type'] = 0;
        if (property_exists(App::$Post,'add_don_index') && App::$Post->add_don_index != '')
            $Item['Index'] = App::$Post->add_don_index;
        else
            $Item['Index'] = 0;
        if (property_exists(App::$Post,'add_don_level') && App::$Post->add_don_level != '')
            $Item['Level'] = App::$Post->add_don_level;
        else
            $Item['Level'] = 0;
        if (property_exists(App::$Post,'add_don_skill') && App::$Post->add_don_skill != '')
            $Item['Skill'] = App::$Post->add_don_skill;
        else
            $Item['Skill'] = 0;
        if (property_exists(App::$Post,'add_don_luck') && App::$Post->add_don_luck != '')
            $Item['Luck'] = App::$Post->add_don_luck;
        else
            $Item['Luck'] = 0;
        if (property_exists(App::$Post,'add_don_opt') && App::$Post->add_don_opt != '')
            $Item['Opt'] = App::$Post->add_don_opt;
        else
            $Item['Opt'] = 0;
        if (property_exists(App::$Post,'add_don_exc') && App::$Post->add_don_exc != '')
            $Item['Excellent'] = App::$Post->add_don_exc;
        else
            $Item['Excellent'] = 0;
        if (property_exists(App::$Post,'add_don_soc1') && App::$Post->add_don_soc1 != '')
            $Item['Soc1'] = App::$Post->add_don_soc1;
        else
            $Item['Soc1'] = 0;
        if (property_exists(App::$Post,'add_don_soc2') && App::$Post->add_don_soc2 != '')
            $Item['Soc2'] = App::$Post->add_don_soc2;
        else
            $Item['Soc2'] = 0;
        if (property_exists(App::$Post,'add_don_soc3') && App::$Post->add_don_soc3 != '')
            $Item['Soc3'] = App::$Post->add_don_soc3;
        else
            $Item['Soc3'] = 0;
        if (property_exists(App::$Post,'add_don_soc4') && App::$Post->add_don_soc4 != '')
            $Item['Soc4'] = App::$Post->add_don_soc4;
        else
            $Item['Soc4'] = 0;
        if (property_exists(App::$Post,'add_don_soc5') && App::$Post->add_don_soc5 != '')
            $Item['Soc5'] = App::$Post->add_don_soc5;
        else
            $Item['Soc5'] = 0;
        if (property_exists(App::$Post,'add_don_pvp') && App::$Post->add_don_pvp != '')
            $Item['Pvp'] = App::$Post->add_don_pvp;
        else
            $Item['Pvp'] = 0;
        if (property_exists(App::$Post,'add_don_anc') && App::$Post->add_don_anc != '')
            $Item['Ancient'] = App::$Post->add_don_anc;
        else
            $Item['Ancient'] = 0;
        if (property_exists(App::$Post,'add_don_harm') && App::$Post->add_don_harm != '')
            $Item['Harmony'] = App::$Post->add_don_harm;
        else
            $Item['Harmony'] = 0;


        for ($i = 0; $i < 5; $i++){
           if (property_exists(App::$Post, 'val_take_'. $i)){
               $Item['Price']['Valutes'][$i] = App::$Post->{'val_take_'.$i};
           }
            else{
                $Item['Price']['Valutes'][$i] = 0;
            }
        }
        $Config[$id] = $Item;
        file_put_contents('configs/modules/DB_' . $Db . '/Donate.json', json_encode($Config));

    }
    public static function DeleteItem($id,$Db)
    {
        $Config = file_get_contents("configs/modules/DB_" . $Db . "/Donate.json");
        $Config = json_decode($Config,true);
        unset($Config[$id]);
        file_put_contents('configs/modules/DB_' . $Db . '/Donate.json', json_encode($Config));
    }
    public static function AddItem($Db)
    {
        $Config = file_get_contents("configs/modules/DB_" . $Db . "/Donate.json");
        $Config = json_decode($Config,true);

        if (property_exists(App::$Post,'add_don_type') && App::$Post->add_don_type != '')
            $Item['Type'] = App::$Post->add_don_type;
        else
            $Item['Type'] = 0;
        if (property_exists(App::$Post,'add_don_index') && App::$Post->add_don_index != '')
            $Item['Index'] = App::$Post->add_don_index;
        else
            $Item['Index'] = 0;
        if (property_exists(App::$Post,'add_don_level') && App::$Post->add_don_level != '')
            $Item['Level'] = App::$Post->add_don_level;
        else
            $Item['Level'] = 0;
        if (property_exists(App::$Post,'add_don_skill') && App::$Post->add_don_skill != '')
            $Item['Skill'] = App::$Post->add_don_skill;
        else
            $Item['Skill'] = 0;
        if (property_exists(App::$Post,'add_don_luck') && App::$Post->add_don_luck != '')
            $Item['Luck'] = App::$Post->add_don_luck;
        else
            $Item['Luck'] = 0;
        if (property_exists(App::$Post,'add_don_opt') && App::$Post->add_don_opt != '')
            $Item['Opt'] = App::$Post->add_don_opt;
        else
            $Item['Opt'] = 0;
        if (property_exists(App::$Post,'add_don_exc') && App::$Post->add_don_exc != '')
            $Item['Excellent'] = App::$Post->add_don_exc;
        else
            $Item['Excellent'] = 0;
        if (property_exists(App::$Post,'add_don_soc1') && App::$Post->add_don_soc1 != '')
            $Item['Soc1'] = App::$Post->add_don_soc1;
        else
            $Item['Soc1'] = 0;
        if (property_exists(App::$Post,'add_don_soc2') && App::$Post->add_don_soc2 != '')
            $Item['Soc2'] = App::$Post->add_don_soc2;
        else
            $Item['Soc2'] = 0;
        if (property_exists(App::$Post,'add_don_soc3') && App::$Post->add_don_soc3 != '')
            $Item['Soc3'] = App::$Post->add_don_soc3;
        else
            $Item['Soc3'] = 0;
        if (property_exists(App::$Post,'add_don_soc4') && App::$Post->add_don_soc4 != '')
            $Item['Soc4'] = App::$Post->add_don_soc4;
        else
            $Item['Soc4'] = 0;
        if (property_exists(App::$Post,'add_don_soc5') && App::$Post->add_don_soc5 != '')
            $Item['Soc5'] = App::$Post->add_don_soc5;
        else
            $Item['Soc5'] = 0;
        if (property_exists(App::$Post,'add_don_pvp') && App::$Post->add_don_pvp != '')
            $Item['Pvp'] = App::$Post->add_don_pvp;
        else
            $Item['Pvp'] = 0;
        if (property_exists(App::$Post,'add_don_anc') && App::$Post->add_don_anc != '')
            $Item['Ancient'] = App::$Post->add_don_anc;
        else
            $Item['Ancient'] = 0;
        if (property_exists(App::$Post,'add_don_harm') && App::$Post->add_don_harm != '')
            $Item['Harmony'] = App::$Post->add_don_harm;
        else
            $Item['Harmony'] = 0;

        for ($i = 0; $i < 5; $i++){
            if (property_exists(App::$Post, 'val_take_'. $i)){
                $Item['Price']['Valutes'][$i] = App::$Post->{'val_take_'.$i};
            }
            else{
                $Item['Price']['Valutes'][$i] = 0;
            }
        }
        $Config[] = $Item;
        file_put_contents('configs/modules/DB_' . $Db . '/Donate.json', json_encode($Config));
        
    }
    public static function ChangeEnable($Db)
    {
        $Config = file_get_contents("configs/modules/DB_" . $Db . "/Donate.json");
        $Config = json_decode($Config,true);
        if (property_exists(App::$Post,'Enable')) {
            $Config['Enable'] = true;
        }
        else {
            $Config['Enable'] = false;
        } 
        file_put_contents('configs/modules/DB_' . $Db . '/Donate.json', json_encode($Config));

    }
}

?>