/*:ja
 * @plugindesc 
 */
const FdEdit_PicPath = "pictures/UI_Fuzoku/Deck_Edit/";
let _FzDeckSave = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    _FzDeckSave.call(this);
    this.FzDeckSel = 0; 
    this.FzFreeDeck = []; 
};
let _FzDs_flame = 0;
let _FzDs_ViewDeckNo = 0;
let FzDs_RdoPicArr = []; 
let FzDs_TxtPicArr = []; 
Game_Interpreter.prototype.Fz_DeckSelect_View = function () {
    this.SetSpriteFIn(PN_FdBase, FdEdit_PicPath + "PN_FdBase", 0, 0, 0, 0, 5);
    this.SetSpriteFIn(PN_FdKBtn, FdEdit_PicPath + "PN_FdKBtn", 84, 721, 0, 0, 5);
    if (!$gameSwitches.value(SW_FzDeckRdo + 0)) $gameSwitches.setValue(SW_FzDeckRdo, true);
    let _ckDeck = this.FzDs_DeckListDraw(true);
    this.FzDs_EDeck_Setting();
    this.FzDs_DeckCardDraw(true); 
    fuzokuFlg = true; 
    _FzDs_flame = 0;
}
let _DeckSellCMap = [];
Game_Interpreter.prototype.FzDs_DeckListDraw = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    let _opi = 255;
    if (_SetFlg) {
        _opi = 0;
    }
    FzDs_RdoPicArr = []; 
    FzDs_TxtPicArr = []; 
    _DeckSellCMap = [];
    let _ckDeck = [];
    let _DCnt = 0;
    this.DelSprite(PN_FdSDeckDesc);
    for (let i = 0; i <= 8 - 1; i++) {
        if ($gameSwitches.value(SW_FzDeckRdo + i)) { 
            let _PsX = 41;
            let _PsY = 157 + 60 * _DCnt;
            _DeckSellCMap.push([39, 156 + (60 * _DCnt), 329, 60]);
            let _SetRdoImg = FdEdit_PicPath + "PN_FdRdo";
            let _SetDName = _Fz_DeckData[i].Name;
            let _SetDDesc = _Fz_DeckData[i].Desc;
            if ($gameSystem.FzDeckSel == i) {
                _ckDeck = _Fz_DeckData[i].Deck;
                this.SpriteStr(PN_FdSDeckStr, _SetDName, 35, 470, 125, _opi);
                this.MoveSprite(PN_FdSDeckStr, true, true, 5);
                this.SpriteStr(PN_FdSDeckDesc, SCol.Def0 + _SetDDesc, 22, 778, 112, _opi);
                this.MoveSprite(PN_FdSDeckDesc, true, true, 5);
                _SetDName = SCol.Yel + _SetDName;
                _FzDs_ViewDeckNo = i;
                _SetRdoImg += "_b";
            }
            this.SetSprite(PN_FdRdo + i, _SetRdoImg, _PsX, _PsY, _opi);
            this.MoveSprite(PN_FdRdo + i, true, true, 5);
            FzDs_RdoPicArr.push(PN_FdRdo + i);
            this.SpriteStr(PN_FdTxt + i, _SetDName, 35, _PsX + 69, _PsY + 9, _opi);
            this.MoveSprite(PN_FdTxt + i, true, true, 5);
            FzDs_TxtPicArr.push(PN_FdTxt + i);
            _DCnt++;
        }
    }
    let _cki = 9 - 1;
    if ($gameSwitches.value(SW_FzDeckRdo + _cki)) {
        let _PsX = 41;
        let _PsY = 157 + (60 * _cki);
        _DeckSellCMap.push([39, 156 + (60 * _cki), 329, 60]);
        let _SetRdoImg = FdEdit_PicPath + "PN_FdRdo";
        let _SetDName = "★自由選択";
        if ($gameSystem.FzDeckSel == _cki) {
            this.SpriteStr(PN_FdSDeckStr, _SetDName, 35, 470, 125, _opi);
            _SetDName = SCol.Yel + _SetDName;
            this.MoveSprite(PN_FdSDeckStr, true, true, 5);
            _ckDeck = $gameSystem.FzFreeDeck;
            _FzDs_ViewDeckNo = _cki;
            _SetRdoImg += "_b";
        }
        this.SetSprite(PN_FdRdo + _cki, _SetRdoImg, _PsX, _PsY, _opi);
        this.MoveSprite(PN_FdRdo + _cki, true, true, 5);
        this.SpriteStr(PN_FdTxt + _cki, _SetDName, 35, _PsX + 69, _PsY + 6, _opi);
        this.MoveSprite(PN_FdTxt + _cki, true, true, 5);
    }
    return _ckDeck;
}
let FzDs_Scroll = new N_Scroll(FdEdit_PicPath + "PN_FdScroll", 506, PN_FdScroll, 1308, 187);
FzDs_Scroll.ViewCount = 2; 
let FzDs_PicArr = [];
let FzDs_ZureY = 0;
Game_Interpreter.prototype.FzDs_DeckCardDraw = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    if ($gameSystem.FzDeckSel == 9 - 1) {
        this.SetSprite(PN_FdBase + 1, FdEdit_PicPath + "PN_FdBase_1", 0, 0, 255);
        this.DelSpriteSpan(PN_FdCard, PN_FdMaisu + 9);
    } else {
        this.DelSprite(PN_FdBase + 1);
        this.DelSpriteSpan(PN_FdCard, PN_FdMaisu + 9);
    }
    let _opi = 255;
    if (_SetFlg) {
        _opi = 0;
        FzDs_ZureY = 0;
        FzDs_Scroll.ZureNo = 0;
    }
    FzDs_PicArr = [];
    for (let i = 0; i <= EDeckArr.length - 1; i++) {
        let _PsX = 452 + ((i % 5) * 164);
        let _PsY = 229 + Math.floor(i / 5) * 237;
        let _Cardi = i + FzDs_ZureY * 5;
        this.FzDs_CardDraw(i, EDeckArr[_Cardi], _PsX, _PsY, _opi);
        if (i == 9) break;
    }
    let ItmCnt = EDeckArr.length - 1;
    FzDs_Scroll.ItemCount = Math.floor(ItmCnt / 5) + 1;
    this.ScrollView(FzDs_Scroll, 255);
}
Game_Interpreter.prototype.FzDs_CardDraw = function (_Pici, _EditDt, _PsX, _PsY, _Opi) {
    if (_EditDt == undefined) return;
    let _CardId = _EditDt._CNo;
    let _枚数 = _EditDt._Cnt;
    if (_枚数 > 10) _枚数 = 10;
    let _SelCard;
    for (let i = 0; i <= _Fz_CardData.length - 1; i++) {
        if (_Fz_CardData[i].CNo == _CardId) {
            _SelCard = _Fz_CardData[i];
        }
    }
    this.SetSprite(PN_FdCard + _Pici, Fz_CardPath + "Card_" + _CardId, _PsX, _PsY, _Opi, 60, 60);
    this.MoveSprite(PN_FdCard + _Pici, true, true, 5, 255, true, true);
    FzDs_PicArr.push(PN_FdCard + _Pici);
    this.SetSprite(PN_Fdcost + _Pici, BtlPicPath + "PN_Cost" + _SelCard.Cost + "_H",
        _PsX, _PsY, _Opi, 60, 60);
    this.MoveSprite(PN_Fdcost + _Pici, true, true, 5, 255, true, true);
    this.SetSprite(PN_FdMaisu + _Pici, CardPicPath + "wk_cnt" + _枚数,
        _PsX, _PsY + 179, _Opi, 60, 60);
    this.MoveSprite(PN_FdMaisu + _Pici, true, true, 5, 255, true, true);
    if (_枚数 == 0) { 
        this.SpriteCngColor(PN_FdCard + _Pici, [-50, -50, -50, -50]);
        this.SpriteCngColor(PN_Fdcost + _Pici, [-50, -50, -50, -50]);
        this.SpriteCngColor(PN_FdMaisu + _Pici, [-50, -50, -50, -50]);
    }
}
Game_Interpreter.prototype.Fz_DeckSelect_Update = function () {
    for (let i = 0; i <= FzDs_PicArr.length - 1; i++) {
        let _CardId = FzDs_PicArr[i] - PN_FdCard + (FzDs_ZureY * 5)
        if (EDeckArr[_CardId]._Cnt == 0) {
            this.SpriteCngColor(FzDs_PicArr[i], [-50, -50, -50, -50]);
        } else {
            this.SpriteCngColor(FzDs_PicArr[i]);
        }
    }
    let _CNo = overPointerSpCkArr(FzDs_PicArr);
    if (_CNo != -1) {
        let _CardId = _CNo - PN_FdCard + (FzDs_ZureY * 5)
        if (EDeckArr[_CardId]._Cnt != 0) {
            this.SpriteCngColor(_CNo, [50, 50, 50, 0]);
        } else {
            this.SpriteCngColor(_CNo, [-50, -50, -50, -50]);
        }
        let _Cki = _CNo - PN_FdCard;
        let _PsX = 452 + ((_Cki % 5) * 164) - 270;
        let _PsY = 229 + Math.floor(_Cki / 5) * 237;
        if (Math.floor(_Cki / 5) == 0) {
            _PsY += 217;
        } else {
            _PsY -= 370;
        }
        this.FzDs_CardSetuDraw(EDeckArr[_CardId]._CNo, _PsX, _PsY);
        if (TouchInput.isTriggered()) {
            if ($gameSystem.FzDeckSel == 9 - 1) {
                EDeckArr[_CardId]._Cnt++;
                $gameSystem.FzFreeDeck = [];
                if (EDeckArr[_CardId]._Cnt >= 4) {
                    EDeckArr[_CardId]._Cnt = 0;
                }
                for (let i = 0; i < EDeckArr.length; i++) {
                    if (EDeckArr[i]._Cnt > 0) {
                        for (let j = 0; j < EDeckArr[i]._Cnt; j++) {
                            $gameSystem.FzFreeDeck.push(EDeckArr[i]._CNo);
                        }
                    }
                }
                this.FzDs_DeckCardDraw();
            }
        }
    } else {
        this.FzDs_CardSetuClose(); 
    }
    let _CngDeckId = -1;
    for (let i = 0; i <= 8; i++) {
        this.SpriteCngColor(PN_FdRdo + i);
        this.SpriteCngColor(PN_FdTxt + i);
    }
    _CNo = overSpriteClickable(PN_FdBase, _DeckSellCMap);
    if (_CNo != -1) {
        let _cki = 9 - 1;
        this.SpriteCngColor( PN_FdRdo + _CNo , [100, 100, 0, 0]);
        this.SpriteCngColor( PN_FdTxt + _CNo , [100, 100, 0, 0]);
        if (_CNo == _DeckSellCMap.length - 1) {
            this.SpriteCngColor( PN_FdRdo + _cki , [100, 100, 0, 0]);
            this.SpriteCngColor( PN_FdTxt + _cki , [100, 100, 0, 0]);
        }
        if (TouchInput.isTriggered()) {
            let _DCnt = 0;
            for (let i = 0; i <= 8 - 1; i++) {
                if ($gameSwitches.value(SW_FzDeckRdo + i)) {
                    if (_CNo == _DCnt) {
                        _CngDeckId = i;
                    }
                    _DCnt++;
                }
            }
            if ($gameSwitches.value(SW_FzDeckRdo + _cki)) {
                if (_CNo == _DeckSellCMap.length - 1) {
                    _CngDeckId = _cki;
                }
            }
        }
    }
    if (_CngDeckId != -1 && _FzDs_ViewDeckNo != _CngDeckId) {
        _FzDs_ViewDeckNo = _CngDeckId;
        $gameSystem.FzDeckSel = _CngDeckId;
        this.FzDs_EDeck_Setting();
        SoundManager.playOk();
        this.FzDs_DeckCardDraw(true);
        this.FzDs_DeckListDraw();
    }
    let _FzDs_ZureFlg = false;
    if (TouchInput.wheelY < 0) {
        if (FzDs_ZureY > 0) {
            _FzDs_ZureFlg = true;
            FzDs_ZureY--;
        }
    }
    if (TouchInput.wheelY > 0) {
        if (FzDs_ZureY < FzDs_Scroll.ItemCount - 2) {
            _FzDs_ZureFlg = true;
            FzDs_ZureY++;
        }
    }
    if (_FzDs_ZureFlg) {
        SoundManager.playCursor();
        FzDs_Scroll.ZureNo = FzDs_ZureY;
        this.FzDs_DeckCardDraw();
    }
    this.SpriteCngColor(PN_FdKBtn);
    if (overPointerSpCk(PN_FdKBtn)) {
        this.SpriteCngColor(PN_FdKBtn, [50, 50, 50, 0]);
        if (TouchInput.isTriggered()) {
            SoundManager.playSave();
            this.Fz_DeckSelect_Close(); 
            return false;
        }
    }
    _FzDs_flame++;
    return true; 
}
Game_Interpreter.prototype.FzDs_EDeck_Setting = function () {
    let _ckDeck;
    EDeckArr = [];
    if ($gameSystem.FzDeckSel == 9 - 1) {
        _ckDeck = $gameSystem.FzFreeDeck; 
        for (let i = 0; i <= _Fz_CardData.length - 1; i++) {
            EDeckArr.push(new Deck_Edit(_Fz_CardData[i].CNo, 0));
        }
    } else {
        _ckDeck = _Fz_DeckData[$gameSystem.FzDeckSel].Deck;
    }
    for (let i = 0; i <= _ckDeck.length - 1; i++) {
        let _nAddFlg = true;
        for (let j = 0; j <= EDeckArr.length - 1; j++) {
            if (EDeckArr[j]._CNo == _ckDeck[i]) {
                _nAddFlg = false;
                EDeckArr[j]._Cnt++;
            }
        }
        if (_nAddFlg) { 
            EDeckArr.push(new Deck_Edit(_ckDeck[i]));
        }
    }
}
let _FzDs_CardId = -1;
Game_Interpreter.prototype.FzDs_CardSetuDraw = function (_CardId) {
    if (_FzDs_CardId != _CardId) {
        _FzDs_CardId = _CardId; 
    } else {
        return;
    }
    let _PsX = 0;
    let _PsY = 160;
    if (_PsX > 610) _PsX = 610;
    this.SetSpriteFIn(PN_FdSelWin, FdEdit_PicPath + "PN_FdSelWin", _PsX, _PsY, 0, 0, 5);
    let _SelCard;
    for (let i = 0; i <= _Fz_CardData.length - 1; i++) {
        if (_Fz_CardData[i].CNo == _CardId) {
            _SelCard = _Fz_CardData[i];
        }
    }
    let _CPosX = _PsX + 120;
    let _CPosY = _PsY + 45;
    this.SetSprite(PN_FdSCard, Fz_CardPath + "Card_" + _CardId, _CPosX, _CPosY, 0, 80, 80);
    this.MoveSprite(PN_FdSCard, true, true, 5, 255, true, true);
    this.SetSprite(PN_FdSCost, BtlPicPath + "PN_Cost" + _SelCard.Cost + "_H", _CPosX, _CPosY, 0, 80, 80);
    this.MoveSprite(PN_FdSCost, true, true, 5, 255, true, true);
    // this.SpriteStr(PN_FdSName, _SetDName , 35 , _PsX + 69 , _PsY + 6 , 5 , 0);
    // this.MoveSprite(PN_FdSName , true , true );
    this.SpriteStr(PN_FdSetu + 0, _SelCard.Setu1, 20, _PsX + 29, _PsY + 327, 0);
    this.MoveSprite(PN_FdSetu + 0, true, true, 5, 255, true, true);
    this.SpriteStr(PN_FdSetu + 1, _SelCard.Setu2, 20, _PsX + 29, _PsY + 327 + 30, 0);
    this.MoveSprite(PN_FdSetu + 1, true, true, 5, 255, true, true);
    let _SetuStr = "";
    if (_SelCard.FlgArr[0]) _SetuStr += "風呂";
    if (_SelCard.FlgArr[1]) {
        if (_SetuStr != "") _SetuStr += "・";
        _SetuStr += "寝室";
    }
    if (_SelCard.FlgArr[2]) {
        if (_SetuStr != "") _SetuStr += "・";
        _SetuStr += "アフター";
    }
    if (_SetuStr != "") _SetuStr = "★" + _SetuStr + "で出現";
    this.SpriteStr(PN_FdSetu + 2, _SetuStr, 20, _PsX + 29, _PsY + 423, 0);
    this.MoveSprite(PN_FdSetu + 2, true, true, 5, 255, true, true);
}
Game_Interpreter.prototype.FzDs_CardSetuClose = function () {
    _FzDs_CardId = -1;
    this.DelSpriteSpan(PN_FdSelWin, PN_FdSCost);
}
Game_Interpreter.prototype.Fz_DeckSelect_Close = function () {
    NaGCUpFlg = true; 
    this.DelSpriteSpan(PN_FdBase, PN_FdSCost);
    this.DelPict(PN_FdScroll);
    fuzokuFlg = false;
}
