/*:ja
 * @plugindesc 
 */
//https://docs.google.com/spreadsheets/d/10tktWhyosnXxjjHqQ8Pgqhuo0R_3jZ8gd2DsIbdvuMc/edit#gid=2054574132
const VN_HandicapVar = 145;
let handiCnt = 0;
let _ハンディ選択Flg = false;
let _ハンディStr = "";
let handiSelectArr = [false , false , false];
let handiArr = [];
let handiSelectCardNo = []; 
let _HandiSave = Game_System.prototype.initialize; 
Game_System.prototype.initialize = function () { 
    _HandiSave.call(this);
    this.HandiSaveArr = []; 
};
Game_Interpreter.prototype.HandicapCardGet_Start= function() {
    handiSelectArr = [false , false , false];
    handiEndFlg = false;
    cg_OCardArr = [];
    for (let i = 0; i < handiArr.length; i++) {
        for (let j = 0; j < $gameSystem.HandiSaveArr.length; j++) {
            if (handiArr[i] == $gameSystem.HandiSaveArr[j]) {
                handiArr.splice(i, 1);
                i--; 
                break; 
            }
        }
    }
    let maxLoopCount = 100;
    for (let i = 0; i <= 3 - 1; i++) {
        let _cNo = ArrRndGet_Nomal(handiArr);
        if (cg_OCardArr.find(function (v) { return v._cNo == _cNo }) == undefined) {
            cg_OCardArr.push(new OpenCClass(_cNo));
        } else {
            i--;
        }
        maxLoopCount--;
        if(maxLoopCount <= 0) break;
    }
    handiCnt = $gameVariables.value(VN_HandicapVar);
    if(handiCnt == 0) handiCnt = 1;
    if(handiCnt > 3) handiCnt = 3;
    _STYカード取得中_flame = 0;
    _ハンディ選択Flg = true;
    _ハンディStr = "デッキに入れるカードを" + handiCnt + "枚選んでください";
}
Game_Interpreter.prototype.HandicapCardGet_Update= function () {
    this.Btl_STYカード取得Update(); 
    if($gameSystem.HandiSaveArr == undefined) $gameSystem.HandiSaveArr = [];
    if(!_ハンディ選択Flg) {
        handiSelectCardNo = [];
        for (let i = 0; i <= 3 - 1; i++) {
            if(handiSelectArr[i]) {
                $gameSystem.HandiSaveArr.push(cg_OCardArr[i]._cNo);
                handiSelectCardNo.push(cg_OCardArr[i]._cNo);
            }
        }
        return false;
    }
    return true; 
}
let ArrRndGet_Nomal = function (_Arr) {
    let _ckNo = Math.floor(Math.random() * _Arr.length);
    return _Arr[_ckNo];
}
let ArrTrueGet = function (_Arr) {
    let _Cnt = 0;
    for (let i = 0; i <= _Arr.length - 1; i++) {
        if (_Arr[i]) _Cnt++;
    }
    return _Cnt;
}
let Handi_出現カード指定 = function (_CNoArr) {
    handiArr = _CNoArr;
}
let Handi_取得カード確認 = function (_CardNo) {
    return (handiSelectCardNo.indexOf(_CardNo) != -1);
}
let Handi_デッキリセット = function () {
    $gameSystem.HandiSaveArr = [];
}
let Handi_デッキカード確認 = function (_CardNo) {
    return ($gameSystem.HandiSaveArr.indexOf(_CardNo) != -1);
}
let _RzSetArr = []; 
let RzType = {
    Gold: 1,
    Pack: 2,
    Reric: 3
};
class RzItem {
    constructor(_Type, _Var) {
        this.PicNo = 0;
        this.Type = _Type;
        this.Var = _Var;
    }
}
let _STY_BefRzCnt = 0;
let _STYデッキ追加カードid = -1; 
Game_Interpreter.prototype.Btl_STY報酬項目削除 = function (_Rzi) {
    EasingStr = "イージング名";
    this.MoveSprite(_RzSetArr[_Rzi].PicNo, true, true, 3, 0);
    this.MoveSprite(_RzSetArr[_Rzi].PicNo + 3, true, true, 3, 0);
    EasingStr = "";
    _RzSetArr.splice(_Rzi, 1); 
}
let _STY_カードRzi = -1; 
let _STYカード取得中Flg = false; 
let _STYカード取得中_flame = 0;
Game_Interpreter.prototype.Btl_STYカード取得Update = function () {
    if (_STYカード取得中_flame == 0) { 
        this.SetSpriteFIn(PN_BCSelectBack, "Black", 0, 0, 0, 0, 10, 150);
        for (let i = 0; i <= cg_OCardArr.length - 1; i++) {
            this.CardPicDraw(cg_OCardArr[i]._cNo, PN_BCSelCard + i, PN_Cmp, 155 + (232 * i), 274, 255, 75, 75);
            this.MovePict(PN_BCSelCard + i, true, true, 10, 255, true, true); 
        }
        if (_ハンディ選択Flg) {
            this.SetSpriteFIn(PN_BCSelBtn, BtlPicPath + "PN_BCSelBtn_Handi", 367, 554, 0, 0, 10, 170);
            this.SetSpriteFIn(PN_BCSelectWin, BtlPicPath + "PN_BCSelectWin_Handi", 0, 0, 0, 0, 10);
            this.SpriteStrC(PN_BCTxt, _ハンディStr, 28, 464, 217);
        } else {
            this.SetSpriteFIn(PN_BCSelBtn, BtlPicPath + "PN_BCSelBtn", 367, 554, 0, 0, 10);
            this.SetSpriteFIn(PN_BCSelectWin, BtlPicPath + "PN_BCSelectWin", 0, 0, 0, 0, 10);
        }
    }
    if (_STYカード取得中_flame > 30) {
        let _cgTCArr = [PN_BCSelCard + 0, PN_BCSelCard + 1, PN_BCSelCard + 2];
        this.PicCngColorArr(_cgTCArr);
        Alpha0SpriteSpan(PN_BCSetuWaku, PN_BCT_Setu + 2)
        let _OverNo = overPointerCkArr(_cgTCArr);
        if (_OverNo != -1) {
            let _ckNo = _OverNo - PN_BCSelCard;
            this.PicCngColor(PN_BCSelCard + _ckNo, [50, 50, 50, 0]);
            this.Btl_STYCard説明_Draw(cg_OCardArr[_ckNo]._cNo, 32 + (232 * _ckNo), 158);
            if (TouchInput.isTriggered()) {
                if (_ハンディ選択Flg) {
                    for (let i = 0; i < handiSelectArr.length; i++) {
                        handiSelectArr[i] = false; 
                    }
                    handiSelectArr[_ckNo] = !handiSelectArr[_ckNo];
                    if (ArrTrueGet(handiSelectArr) >= handiCnt) {
                        this.MoveSprite(PN_BCSelBtn, true, true, 1, 255); 
                    } else {
                        this.MoveSprite(PN_BCSelBtn, true, true, 1, 170); 
                    }
                    for (let i = 0; i <= 3 - 1; i++) {
                        if (handiSelectArr[i]) {
                            this.SetSprite(PN_BCCHikari + i, BtlPicPath + "PN_CHikariHandi",
                                155 + (232 * i) - 7, 274 - 15, 255, 75, 75);
                        } else {
                            Alpha0Sprite(PN_BCCHikari + i);
                        }
                    }
                } else {
                    _STYデッキ追加カードid = cg_OCardArr[_ckNo]._cNo;
                    this.Btl_STY報酬項目削除(_STY_カードRzi);
                    this.Btl_STYカード取得Close();
                }
            }
        }
        let _CloseFlg = false;
        if (TouchInput.isCancelled() && !_ハンディ選択Flg) _CloseFlg = true;
        this.SpriteCngColor(PN_BCSelBtn);
        if (overPointerSpCk(PN_BCSelBtn)) {
            if (_ハンディ選択Flg) {
                if (ArrTrueGet(handiSelectArr) >= handiCnt) {
                    this.SpriteCngColor(PN_BCSelBtn, [100, 100, 100, 0]);
                    if (TouchInput.isTriggered()) {
                        _CloseFlg = true;
                        _ハンディ選択Flg = false;
                    }
                }
            } else {
                this.SpriteCngColor(PN_BCSelBtn, [100, 100, 100, 0]);
                if (TouchInput.isTriggered()) {
                    _CloseFlg = true;
                }
            }
        }
        if (_CloseFlg) {
            this.Btl_STYカード取得Close();
        }
    }
    _STYカード取得中_flame++;
}
Game_Interpreter.prototype.Btl_STYカード取得Close = function () {
    _STYカード取得中Flg = false;
    for (let i = PN_BCSelectBack; i <= PN_BCSelBtn; i++) {
        this.MoveSprite(i, true, true, 5, 0, true, true);
        this.MovePict(i, true, true, 5, 0, true, true);
    }
    Alpha0SpriteSpan(PN_BCSetuWaku, PN_BCT_Setu + 2); 
}
let _STY_SetuPicNo = PN_BCSetuWaku;
Game_Interpreter.prototype.Btl_STYCard説明_Draw = function (_CardId, _CPosX, _CPosY) {
    _STY_SetuPicNo = PN_BCSetuWaku;
    let _SetuArr = this.CardSetuGet(_CardId); 
    this.SetSprite(_STY_SetuPicNo, BtlPicPath + "PN_BCSetuWaku", _CPosX, _CPosY);
    for (let j = 0; j <= _SetuArr.length - 1; j++) {
        this.SpriteStr(_STY_SetuPicNo + 1 + j, _SetuArr[j], 19, _CPosX + 8, _CPosY + 11 + (27 * j));
    }
}
Game_Interpreter.prototype.Btl_STYCard説明_DrawPict = function (_CardId, _CPosX, _CPosY) {
    _STY_SetuPicNo = PN_BCSetuWaku;
    let _SetuArr = this.CardSetuGet(_CardId); 
    this.SetPict(_STY_SetuPicNo, BtlPicPath + "PN_BCSetuWaku", _CPosX, _CPosY);
    for (let j = 0; j <= _SetuArr.length - 1; j++) {
        this.SetPicStr(_STY_SetuPicNo + 1 + j, _SetuArr[j], 19, _CPosX + 8, _CPosY + 11 + (27 * j));
    }
}
Game_Interpreter.prototype.Btl_STY戦闘終了後_データ反映 = function () {
    if (_STYデッキ追加カードid != -1) {
        $gameSystem.Btl_Chara.FDeck.push(_STYデッキ追加カードid);
    }
}
