/*:ja
 * @plugindesc 
 */
let CardTradePicPath = "pictures/UI_CTrade/";
const PN_CtEndBtn = 328; 
const PN_CtOpen = PN_CtEndBtn + 1;
const ItemNo_TrdPack = 96;  
let CTradeFlg = false;
let NCTrade_ini = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    NCTrade_ini.call(this);
    this.CTradeSeed = 0;
};
let CTradeCard = []; 
Game_Interpreter.prototype.CardTradeStart = function () {
    isFastOff = true; 
    _junNo = 0;         
    _junRtFlg = false;  
    _siboNo = 0;        
    _siboRtFlg = false; 
    CTradeFlg = true;
    _DeckZure = 0
    AbmNoArr_Get(); 
    EDeckArr = [];
    ItDeckArr = [];
    EDeckTPArr = [];
    CTradeCard = []; 
    DeckMode = _DeckMode.カードトレーダー;
    this.SetSpriteFIn(PN_deBack, CardTradePicPath + "PN_CTrade", 0, 0, 0, 0, 15);
    this.SetSpriteFIn(PN_CtEndBtn, CardTradePicPath + "PN_CtEndBtn", 30, 705, 0, 0, 15);
    this.SetSpriteC(PN_CtOpen, CG_PicPath + "cgura", 390, 540, 255, 80, 80);
    this.CardTrade_EDeckArrSet(); 
    this.PoolCardView(); 
    if ($gameSystem.CTradeSeed == 0) {
        let _SetSeed = Math.floor(Math.random() * 10000000);
        $gameSystem.CTradeSeed = _SetSeed; 
    }
    Menu禁止Flg = true;
    GetCard_Setting(); 
    this.RdoBtnView(); 
}
Game_Interpreter.prototype.CardTradeEnd = function () {
}
Game_Interpreter.prototype.CardTrade_EDeckArrSet = function () {
    EDeckArr = [];
    for (let k = 0; k < $gameSystem.DeckArr.length - 1; k++) {
        for (let i = 0; i <= $gameSystem.DeckArr[k].length - 1; i++) {
            if ($gameSystem.DeckArr[k][i] == CId_待機) {
                $gameSystem.DeckArr[k].splice(i, 1);
                i--;
            }
        }
        let _ckDeck = $gameSystem.DeckArr[k];
        let _SetDeckArr = [];
        for (let i = 0; i <= _ckDeck.length - 1; i++) {
            let _nAddFlg = true;
            for (let j = 0; j <= _SetDeckArr.length - 1; j++) {
                if (_SetDeckArr[j]._CNo == _ckDeck[i]) {
                    _nAddFlg = false;
                    _SetDeckArr[j]._Cnt++;
                }
            }
            if (_nAddFlg) { 
                _SetDeckArr.push(new Deck_Edit(_ckDeck[i]));
            }
        }
        for (let i = 0; i <= _SetDeckArr.length - 1; i++) {
            let _addflg = true;
            for (let j = 0; j <= EDeckArr.length - 1; j++) {
                if (_SetDeckArr[i]._CNo == EDeckArr[j]._CNo) {
                    _addflg = false;
                    if (_SetDeckArr[i]._Cnt > EDeckArr[j]._Cnt) {
                        EDeckArr[j]._Cnt = _SetDeckArr[i]._Cnt;
                    }
                }
            }
            if (_addflg) {
                EDeckArr.push(_SetDeckArr[i]);
            }
        }
    }
    for (let i = 0; i <= CTradeCard.length - 1; i++) {
        let _addflg = true;
        for (let j = 0; j <= EDeckArr.length - 1; j++) {
            if (EDeckArr[j]._CNo == CTradeCard[i]) {
                EDeckArr[j]._Cnt++;
                _addflg = false;
                break;
            }
        }
        if (_addflg) {
            EDeckArr.push(new Deck_Edit(CTradeCard[i]));
        }
    }
}
let _CTrade_Openflame = 0;
let _Trade_GetCNo = 0;
Game_Interpreter.prototype.CTradeUpdate = function () {
    if (CTradeCard.length >= 3) {
        EasingStr = "easeOutQuad";
        if (_CTrade_Openflame == 15) {
            this.de_MiniSyosaiDel();
            this.CardSpriteMove(PN_deDCd + (0 * 3), 390, 540, 30, 0, 40, 40);
            this.SpriteCngColor(PN_CtOpen, [255, 255, 255, 0], 50);
            this.PlaySe(CG_SePath + "cube_Open");
        }
        if (_CTrade_Openflame == 30) {
            this.CardSpriteMove(PN_deDCd + (1 * 3), 390, 540, 30, 0, 40, 40);
            this.PlaySe(CG_SePath + "cube_Open");
        }
        if (_CTrade_Openflame == 45) {
            this.CardSpriteMove(PN_deDCd + (2 * 3), 390, 540, 30, 0, 40, 40);
            this.PlaySe(CG_SePath + "cube_Open");
        }
        if (_CTrade_Openflame == 60) {
            this.DelSprite(PN_deDCd + 2);
        }
        if (_CTrade_Openflame == 75) {
            let _CardArr = [];
            let _getPackArr = [];
            let OpenPackIds = [];
            OpenPackIds = [82 , 83];
            if ($gameVariables.value(VN_Scenario2Wa) > 0) {
                OpenPackIds.push(85);
                OpenPackIds.push(87);
            }
            if ($gameVariables.value(VN_Scenario3Wa) > 0) {
                OpenPackIds.push(86);
            }
            if ($gameVariables.value(VN_Scenario5Wa) > 0) {
                OpenPackIds.push(88);
            }
            for (let i = 0; i <= Pack_Data.length - 1; i++) {
                if (OpenPackIds.indexOf(Pack_Data[i][0]) > -1) {
                    Pack_Data[i][1].forEach(_CardNo => {
                        if (_CardArr.indexOf(_CardArr) == -1) {
                            _CardArr.push(_CardNo);
                        }
                    });
                }
            }
            for (let i = 1; i <= $dataWeapons.length - 1; i++) {
                if (_CardArr.indexOf(i) == -1) continue;
                switch ($dataWeapons[i].meta["Rea"]) {
                    case "0":
                        _Rea0Arr.push(i);
                        break;
                    case "1":
                        _Rea1Arr.push(i);
                        break;
                    case "2":
                        _Rea2Arr.push(i);
                        break;
                    case "3":
                        _Rea3Arr.push(i);
                        break;
                }
            }
            let _GetCardNo = 0;
            let _BrkCnt = 0;
            _PackSeed = $gameSystem.CTradeSeed;
            console.log("パックシード値" + _PackSeed);
            _PRand = new SeedRand(_PackSeed); 
            let _ReaCk = 0;
            for (let i = 0; i <= CTradeCard.length - 1; i++) {
                let _Rea = MetaChecker($dataWeapons[CTradeCard[i]], "Rea", 0);
                _ReaCk += Number(_Rea);
            }
            let _ReaPar = [25, 25, 25, 25]; 
            if (_ReaCk == 0) _ReaPar = [50, 50, 0, 0];
            if (_ReaCk == 1) _ReaPar = [25, 70, 5, 0];
            if (_ReaCk == 2) _ReaPar = [0, 90, 10, 0];
            if (_ReaCk == 3) _ReaPar = [0, 50, 50, 0];
            if (_ReaCk == 4) _ReaPar = [0, 25, 70, 5];
            if (_ReaCk == 5) _ReaPar = [0, 0, 90, 10];
            if (_ReaCk == 6) _ReaPar = [0, 0, 80, 20];
            if (_ReaCk == 7) _ReaPar = [0, 0, 50, 50];
            if (_ReaCk == 8) _ReaPar = [0, 0, 20, 80];
            if (_ReaCk == 9) _ReaPar = [0, 0, 0, 100];
            while (true) { 
                let _random = _PRand.random() * 100;
                console.log(_random);
                if (_random <= _ReaPar[0]) {
                    _GetCardNo = (new OpenCClass(ArrRndGet(_Rea0Arr)))._cNo; 
                }
                if (_random > _ReaPar[0] && _random <= _ReaPar[0] + _ReaPar[1]) {
                    _GetCardNo = (new OpenCClass(ArrRndGet(_Rea1Arr)))._cNo; 
                }
                if (_random > _ReaPar[0] + _ReaPar[1] && _random <= _ReaPar[0] + _ReaPar[1] + _ReaPar[2]) {
                    _GetCardNo = (new OpenCClass(ArrRndGet(_Rea2Arr)))._cNo; 
                }
                if (_random > 100 - _ReaPar[3]) {
                    _GetCardNo = (new OpenCClass(ArrRndGet(_Rea3Arr)))._cNo; 
                }
                if (_GetCardNo != 0) {
                    break;
                } else {
                    _BrkCnt++;
                }
            }
            $gameSystem.CTradeSeed = Math.floor(_PRand.random() * 10000000); 
            let _Rea = MetaChecker($dataWeapons[_GetCardNo], "Rea", 0);
            switch (Number(_Rea)) {
                case 0: 
                    this.PlaySe(CG_SePath + "Rea0");
                    this.PlayPtcSc2Top("CCRea0", 390, 540);     
                    break;
                case 1: 
                    this.PlaySe(CG_SePath + "Rea1");
                    this.PlayPtcSc2Top("CCRea1", 390, 540);     
                    break;
                case 2: 
                    this.PlaySe(CG_SePath + "Rea2");
                    this.PlayPtcSc2Top("CCRea2", 390, 540);     
                    break;
                case 3: 
                    this.PlaySe(CG_SePath + "Rea3");
                    this.PlayPtcSc2Top("CCRea3", 390, 540);     
                    break;
            }
            this.CardSpriteSetC(_GetCardNo, PN_CtOpen, 390, 540, 255, 100, 100);
            this.CardSpriteMove(PN_CtOpen, true, true, 60, 255, 80, 80);
            this.SpriteCngColor(PN_CtOpen, [255, 255, 255, 0]);
            $gameParty.gainItem($dataWeapons[_GetCardNo], 1); 
            GetCard_CardSet(_GetCardNo);
            for (let i = 0; i <= CTradeCard.length - 1; i++) {
                $gameParty.gainItem($dataWeapons[CTradeCard[i]], -1); 
            }
            _Trade_GetCNo = _GetCardNo;
        }
        if (_CTrade_Openflame == 76) {
            this.SpriteCngColor(PN_CtOpen, [0, 0, 0, 0], 30);
        }
        if (_CTrade_Openflame == 150) {
            this.SetSpriteC(PN_CtOpen, CG_PicPath + "cgura", 390, 540, 255, 80, 80);
            this.CardSpriteSetC(_Trade_GetCNo, PN_CtOpen + 1, 390, 540, 255, 80, 80);
            this.CardSpriteMove(PN_CtOpen + 1, 390 + 500, 540, 30, 0, 40, 40);
            CTradeCard = [];
            this.CTrade_ReView();
        }
        _CTrade_Openflame++;
    } else {
        _CTrade_Openflame = 0;
        this.SpriteCngColor(PN_CtEndBtn);
        if (overPointerSpCk(PN_CtEndBtn)) this.SpriteCngColor(PN_CtEndBtn, [150, 150, 150, 0]);
        if (overPointerSpCk(PN_CtEndBtn) && TouchInput.isTriggered()) {
            this.DelSpriteSpan(PN_deBack, PN_deMnTxt + 2);
            this.DelPict(PN_deScBar); 
            Menu禁止Flg = false;
            _editDNo = $gameSystem.SelDeck; 
            for (let i = 0; i < $gameSystem.DeckArr.length; i++) {
                let _ckDeck = $gameSystem.DeckArr[i];
                let _dcCnt = _ckDeck.length; 
                for (let j = _dcCnt; j < 20; j++) {
                    $gameSystem.DeckArr[i].push(CId_待機);
                }
            }
            CTradeFlg = false;
            isFastOff = false; 
            return false; 
        } else {
            this.Menu_DE_Update_L1(); 
        }
        let _CNo = overPointerSpCkArr(CTrade_TcPic)
        if (_CNo != -1) {
            let _SelNo = (_CNo - PN_deDCd) / 3;
            let _PosX = 95 + (140 * _SelNo);
            let _PosY = 290;
            this.de_MiniSyosai(CTradeCard[_SelNo], _PosX + 107, _PosY + 70);
            if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
                CTradeCard.splice(_SelNo, 1);
                this.CTrade_ReView();
            }
        }
        if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
            this.PlayPtcScTop("S_Click", PL_MouseX, PL_MouseY);
        }
    }
    return true;
}
Game_Interpreter.prototype.CTrade_ReView = function () {
    this.CardTrade_EDeckArrSet(); 
    this.PoolCardView(); 
    this.CTrade_CardView();
}
let CTrade_TcPic = [];
Game_Interpreter.prototype.CTrade_CardView = function () {
    CTrade_TcPic = [];
    this.DelSpriteSpan(PN_deDCd, PN_deDCd + (3 * 3));
    for (let i = 0; i <= CTradeCard.length - 1; i++) {
        let _PosX = 249 + (140 * i);
        let _PosY = 290;
        this.CardSpriteSetC(CTradeCard[i], PN_deDCd + (i * 3), _PosX, _PosY, 255, 40, 40);
        CTrade_TcPic.push(PN_deDCd + (i * 3));
    }
}
Game_Interpreter.prototype.CTradeRClick = function (_SelCardNo) {
    this.PlaySe("change_day");
    CTradeCard.push(_SelCardNo); 
    this.CTrade_ReView();
}
