/*:
 * @plugindesc 
 * @author ぬぷ竜
 */
let _Game_System_initialize_dgnsel = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    _Game_System_initialize_dgnsel.call(this);
    this.rdnClear_List = []; 
    this.subDgnOpens = [];  
    this.subDgnClears = [];  
};
const DgnSelUIPath = "pictures/UI_DgnSel/";
const VN_RdnStr = 41; 
const VN_ダンジョンサポーターNo = 1094;
const SW_ダンジョン選択４章状態 = 1376; 
let dgnsel_flame = 0;   
let dgnsel_SelNo = 0;   
let dgnsel_Zure = 0;    
let dgnSel_RdId = -1;   
let _dgnExSc = new N_Scroll(DgnSelUIPath + "PN_dgsBase_Ex_Bar" , 560 , PN_dgsScBar , -1, -1);
_dgnExSc.ViewCount = 9; 
_dgnExSc.ItemCount = 1; 
let dgnExScReset = function () {
    _dgnExSc = new N_Scroll(DgnSelUIPath + "PN_dgsBase_Ex_Bar" , 560 , PN_dgsScBar , -1, -1);
    _dgnExSc.ViewCount = 9; 
    _dgnExSc.ItemCount = 1; 
}
let _dgnPicArr = [];    
for (let i = 0; i <= 8; i++) {
    _dgnPicArr.push(PN_dgsSelBs + i); 
}
_dgnPicArr.push(PN_dgsClose);  
_dgnPicArr.push(PN_dgsDBtn);   
let dngnSel_initialize = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    dngnSel_initialize.call(this);
    this.ClearDngn = []; 
    this.ClearExDngn = []; 
};
let DgnClearCheck = function (_id) {
    if($gameSystem.ClearDngn == undefined) $gameSystem.ClearDngn = [];
    return ($gameSystem.ClearDngn.indexOf(_id) != -1);
}
let dgnselL2DMdl = "";
let DgnSelModelSet = function (_mdlName) {
    dgnselL2DMdl = _mdlName;
}
let DgnView_List = []; 
let DgnView_Set = function (_DgnId, _SWFlg) {
    if (DgnView_List.indexOf(_DgnId) == -1)
        DgnView_List.push(_DgnId); 
};
let DgnView_ReSet = function() {
    DgnView_List = [];
}
let PS_DgnSel_Start = function () {
    dgnExScReset();
    dgnsel_flame = 0;
    dgnsel_SelNo = 0;
    dgnsel_Zure = 0;
    dgnSel_RdId = -1;
    DgnSelVList = [];
    DgnView_List.forEach(setDgnId => {
        var rdData = RD_MasterData_Get(setDgnId);
        if (rdData !== undefined) {
            if (rdData.mainFlg) {
                DgnSelVList.unshift(RD_MasterData_Get(setDgnId));
            } else {
                DgnSelVList.push(RD_MasterData_Get(setDgnId));
            }
        }
    });
};
let ExCharaName = "";
Game_Interpreter.prototype.PS_DngnSel = function () {
    if (DgnSelVList.length == 0) return false; 
    if (dgnsel_flame == 0) { 
        if (dgnselL2DMdl != "") {
            L2dFadeMdl(dgnselL2DMdl, "lleft", 10, "", null, null, -100);
        }
        this.PS_DngnSel_View(true)
    }
    if (dgnsel_flame > 30) { 
        this.PicCngColorArr(_dgnPicArr); 
        let _ckNo = overPointerCkArr(_dgnPicArr);
        if (_ckNo != -1) { 
            switch (_ckNo) {
                case PN_dgsClose: 
                    this.PicCngColor(_ckNo, [100, 100, 100, 0]);
                    if (TouchInput.isTriggered()) {
                        dgnSel_RdId = -1;
                        this.PS_DngnSel_Close();
                        return false;
                    }
                    break;
                case PN_dgsDBtn: 
                    if (RD_受注ボタンOFFFlg) break;
                    this.PicCngColor(_ckNo, [100, 100, 100, 0]);
                    if (TouchInput.isTriggered()) {
                        dgnSel_RdId = ViewDgnDatas[dgnsel_SelNo].id;
                        $gameVariables.setValue(VN_RdnStr , ViewDgnDatas[dgnsel_SelNo].name);
                        $gameVariables.setValue(VN_ダンジョンサポーターNo ,-1);
                        $gameSystem.Dngn_AiboArr.forEach(aiboData => {
                            if (aiboData.DngnNo == dgnSel_RdId) {
                                if (aiboData.AiboArr.length > 0) {
                                    $gameVariables.setValue(VN_ダンジョンサポーターNo, aiboData.AiboArr[0]);
                                }
                            }
                        });
                        this.PS_DngnSel_Close(); 
                        return false;
                    }
                    break;
                default: 
                    this.PicCngColor(_ckNo, [100, 100, 100, 0]);
                    if (TouchInput.isTriggered()) {
                        dgnsel_SelNo = _ckNo - PN_dgsSelBs;
                        let _posX = 406;
                        let _posY = 132 + (64 * dgnsel_SelNo);
                        this.MovePict(PN_dgsHikari , _posX , _posY , 3);
                        this.DelPict(PN_dgsScBar);
                        this.DelSprite(PN_dgsScBar);
                        this.PS_DngnSel_DtView(true);
                    }
                    break;
            }
        }
        if (TouchInput.wheelY < 0) { 
            if(_dgnExSc.ZureNo > 0) {
                _dgnExSc.ZureAdd(-1); 
                dgnsel_SelNo++;
                if(dgnsel_SelNo > _dgnExSc.ViewCount - 1) dgnsel_SelNo = _dgnExSc.ViewCount - 1;
                this.PS_DngnSel_View();
            }
        }
        if (TouchInput.wheelY > 0) {
            if(_dgnExSc.ZureNo < DgnView_List.length - _dgnExSc.ViewCount) {
                _dgnExSc.ZureAdd(1); 
                dgnsel_SelNo--;
                if(dgnsel_SelNo < 0) dgnsel_SelNo = 0;
                this.PS_DngnSel_View();
            }
        }
        if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
            dgnSel_RdId = -1; 
            this.PS_DngnSel_Close();
            return false; 
        }
    }
    dgnsel_flame++;
    return true; 
};
Game_Interpreter.prototype.PS_DngnSel_Close = function () {
    L2D_SetZure(dgnselL2DMdl);
    this.DelPictSpan(PN_spBack , PN_dgsGold);
    this.DelPict(PN_dgsScBar);
    this.DelSprite(PN_dgsScBar);
    this.DelPictSpan(PN_dgsAiboA , PN_dgsAiboA + 8);
}
Game_Interpreter.prototype.PS_DngnSel_DgnStart = function () {
    this.RD_Start(dgnSel_RdId); 
}
let PS_DngnSel_OK = function () {
    return dgnSel_RdId != -1; 
}
class Dngn_AiboSet { 
    constructor(_DngnNo , _AiboArr){
        this.DngnNo = _DngnNo;
        this.AiboArr = _AiboArr;
    }
}
let Dngn_AiboSave = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    Dngn_AiboSave.call(this);
    this.Dngn_AiboArr = []; 
};
Game_Interpreter.prototype.Dngn_AiboClear = function(){
    $gameSystem.Dngn_AiboArr = [];
}
Game_Interpreter.prototype.Dngn_AiboSet = function(_DngnNo , _AiboNoA , _AiboNoB){
    _AiboNoA = typeof _AiboNoA !== 'undefined' ? _AiboNoA : 0;
    _AiboNoB = typeof _AiboNoB !== 'undefined' ? _AiboNoB : 0;
    $gameSystem.Dngn_AiboArr.push(new Dngn_AiboSet(_DngnNo , [_AiboNoA , _AiboNoB]));
}
let DgnSelVList = [];
let ViewDgnDatas = []; 
let BtlDngnFlg = false; 
Game_Interpreter.prototype.PS_DngnSel_View = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    dgnsel_Zure = _dgnExSc.ZureNo;
    this.DelPict(PN_dgsTDHika_T);
    this.DelPict(PN_dgsTDHika_B);
    if(dgnsel_Zure > 0) this.SetPict(PN_dgsTDHika_T, DgnSelUIPath + "PN_dgsTDHika_T", 400, 120);
    if(dgnsel_Zure < DgnView_List.length - _dgnExSc.ViewCount){
        this.SetPict(PN_dgsTDHika_B, DgnSelUIPath + "PN_dgsTDHika_B", 400, 717);
    }
    let _opi = 255;
    if (_SetFlg) _opi = 0;
    let _BasePicStr = "PN_dgsBase";
    if (BtlDngnFlg) _BasePicStr += "_Ex";
    this.SetPict(PN_dgsBase, DgnSelUIPath + _BasePicStr, 373, 27, _opi);
    this.MovePict(PN_dgsBase, true, true, 10);
    let viewCnt = 0;
    ViewDgnDatas = [];
    for (let i = dgnsel_Zure; i < DgnSelVList.length; i++) {
        if (viewCnt == 9) break;
        let dgnData = DgnSelVList[i];
        if (DgnClearCheck(dgnData.id) && dgnData.onePlayFlg && dgnData.mainFlg) continue;
        ViewDgnDatas.push(dgnData);
        viewCnt++;
    }
    for (let i = 0; i <= ViewDgnDatas.length - 1; i++) {
        let _DSlPs = new Pos2D(406 , 145 + (64 * i));
        var rdData = RD_MasterData_Get(ViewDgnDatas[i].id);
        let dgsSelPicStr = "PN_dgsSelBs"; 
        if (!rdData.mainFlg) dgsSelPicStr += "_Ex";
        if (BtlDngnFlg) dgsSelPicStr = "PN_dgsSelBs_Btl";
        this.MovePict(PN_dgsClear + i, true, true, 2 , 0);
        if (DgnClearCheck(ViewDgnDatas[i].id)) {
            this.SetPict(PN_dgsClear + i, DgnSelUIPath + "PN_dgsClear", _DSlPs._x, _DSlPs._y , _opi);
            this.MovePict(PN_dgsClear + i, true, true, 10);
            if (!rdData.mainFlg && rdData.onePlayFlg) dgsSelPicStr = "PN_dgsSelBs_ExClear";
        }
        if (rdData.dengerFlg) {
            this.SetPict(PN_dgsDengerMark + i, DgnSelUIPath + "PN_dgsDengerMark", _DSlPs._x + 230, _DSlPs._y + 20, _opi);
            this.MovePict(PN_dgsDengerMark + i, true, true, 10);
        }
        this.SetPict(PN_dgsSelBs + i , DgnSelUIPath + dgsSelPicStr, _DSlPs._x, _DSlPs._y, _opi);
        this.MovePict(PN_dgsSelBs + i, true, true, 10);
        let _owSet = "\\ow[0]"
        this.SetPicStr(PN_dgsSelTxt + i, _owSet + ViewDgnDatas[i].name, 23,
            _DSlPs._x + 23, _DSlPs._y + 18, _opi);
        this.MovePict(PN_dgsSelTxt + i, true, true, 10);
        let _AiboCnt = 0;
        $gameSystem.Dngn_AiboArr.forEach( _AiboData => {
            if(_AiboData.DngnNo == ViewDgnDatas[i].id){
                for (let j = 0; j <= _AiboData.AiboArr.length - 1; j++) {
                    if(_AiboData.AiboArr[j] != 0) {
                        let _APicNo = PN_dgsAiboA + i + (_AiboCnt * 9);
                        this.SetPictFInC(_APicNo , DgnSelUIPath + "sup_" + _AiboData.AiboArr[j],
                        _DSlPs._x + 285 + (_AiboCnt * -63), _DSlPs._y + 36 , 0 , 0 , 15);
                        _AiboCnt++;
                    }
                }
            }
        });
    }
    let dgsClosePicStr = "PN_dgsClose";
    if (BtlDngnFlg) dgsClosePicStr += "_Ex";
    this.SetPict(PN_dgsClose, DgnSelUIPath + dgsClosePicStr, 680, 44, _opi);
    this.MovePict(PN_dgsClose, true, true, 10);
    this.PS_DngnSel_DtView(true);
};
let RD_受注ボタンOFFFlg = false;
Game_Interpreter.prototype.PS_DngnSel_DtView = function (fadeInFlg) {
    fadeInFlg = typeof fadeInFlg !== 'undefined' ? fadeInFlg : false;
    if (DgnSelVList.length == 0) return;
    let opacity = 255;
    if (fadeInFlg) opacity = 0;
    this.SetPict(PN_dgsHikari, DgnSelUIPath + "PN_dgsHikari", 406, 132 + (64 * dgnsel_SelNo), opacity);
    this.MovePict(PN_dgsHikari, true, true, 10);
    _dgnExSc.posX = 373 + 372;
    _dgnExSc.posY = 27 + 127;
    _dgnExSc.ItemCount = DgnView_List.length;
    this.ScView(_dgnExSc);
    if (dgnSel_RdId == ViewDgnDatas[dgnsel_SelNo].id) {
        this.SetPict(PN_dgsHikari, DgnSelUIPath + "PN_dgsHikari", 406, 132 + (64 * dgnsel_SelNo));
        return;
    }
    let _ckId = ViewDgnDatas[dgnsel_SelNo].id; 
    dgnSel_RdId = _ckId;
    let _BtnStr = "PN_dgsDBtn";
    if (BtlDngnFlg) _BtnStr += "_Ex";
    RD_受注ボタンOFFFlg = false;
    if (DgnClearCheck(ViewDgnDatas[dgnsel_SelNo].id)
        && !ViewDgnDatas[dgnsel_SelNo].mainFlg
        && ViewDgnDatas[dgnsel_SelNo].onePlayFlg) {
        _BtnStr = "PN_dgsDBtn_Clear";
        RD_受注ボタンOFFFlg = true;
    }
    let _baseX = 786; let _baseY = 98;
    let _PN_dgsDBaseStr = "PN_dgsDBase";
    if (BtlDngnFlg) _PN_dgsDBaseStr += "_Ex";
    this.SetPict(PN_dgsDBase, DgnSelUIPath + _PN_dgsDBaseStr, 759, 27, 255);
    if (ViewDgnDatas[dgnsel_SelNo].dengerFlg) {
        this.SetPict(PN_dgsDBase + 1, DgnSelUIPath + "PN_dgsDBaseDenger", 759, 27, 255);
    }
    let _PN_dgsBaseStr = "Detail/PN_dgsBase";
    if (BtlDngnFlg) _PN_dgsBaseStr += "_Ex";
    this.SetPict(PN_dgsDData, DgnSelUIPath + _PN_dgsBaseStr, 786, 98, 255);
    this.SetPict(PN_dgrDgnPhoto, DgnSelUIPath + "Detail/PN_dgrDgnPhoto_" + _ckId, _baseX + 71, _baseY + 83, opacity);
    this.MovePict(PN_dgsDData, true, true, 10);
    this.SetPicStr(PN_dgrTitleTxt , ViewDgnDatas[dgnsel_SelNo].name , 24 , _baseX + 42 , _baseY + 24 , opacity);
    DText_NRZureX = 3;
    this.SetPicStrR(PN_dgsAreaTxt , ViewDgnDatas[dgnsel_SelNo].areaMemo , 24 , 1396 , 99 , opacity);
    DText_NRZureX = 0;
    let drawNaiyoTxt = ViewDgnDatas[dgnsel_SelNo].naiyoTxt;
    let drawJohoTxt = ViewDgnDatas[dgnsel_SelNo].johoTxt;
    if ($gameSwitches.value(SW_ダンジョン選択４章状態)) {
        if (ViewDgnDatas[dgnsel_SelNo].naiyoTxtEx != "" && ViewDgnDatas[dgnsel_SelNo].naiyoTxtEx != null) {
            drawNaiyoTxt = ViewDgnDatas[dgnsel_SelNo].naiyoTxtEx;
        }
        if (ViewDgnDatas[dgnsel_SelNo].johoTxtEx != "" && ViewDgnDatas[dgnsel_SelNo].johoTxtEx != null) {
            drawJohoTxt = ViewDgnDatas[dgnsel_SelNo].johoTxtEx;
        }
    }
    this.SetPicStr(PN_dgrNinmuTxt  , drawNaiyoTxt , 20 , _baseX + 39 , _baseY + 326 , opacity);
    this.SetPicStr(PN_dgrTasseiTxt  , drawJohoTxt , 20 , _baseX + 39 , _baseY + 461 , opacity);
    if(ViewDgnDatas[dgnsel_SelNo].hosyuTxt == null) ViewDgnDatas[dgnsel_SelNo].hosyuTxt = "";
    if (ViewDgnDatas[dgnsel_SelNo].hosyuTxt != "") {
        this.SetPicStrR(PN_dgrHousyuTxt  , "報酬：" + ViewDgnDatas[dgnsel_SelNo].hosyuTxt , 24 , _baseX + 588 , _baseY + 556 , opacity);
    }
    else
    {
        if (ViewDgnDatas[dgnsel_SelNo].hosyuGold != 0) {
            this.SetPicStrR(PN_dgrHousyuTxt  , "報酬：" + ViewDgnDatas[dgnsel_SelNo].hosyuGold + " C" , 21 , _baseX + 578 , _baseY + 556 , opacity);
        }
    }
    if(ViewDgnDatas[dgnsel_SelNo].firstHosyuTxt == null) ViewDgnDatas[dgnsel_SelNo].firstHosyuTxt = "";
    if (ViewDgnDatas[dgnsel_SelNo].firstHosyuTxt != "" && !DgnClearCheck(ViewDgnDatas[dgnsel_SelNo].id)) {
        let fontSize = 21; let zureY = 0;
        if (ViewDgnDatas[dgnsel_SelNo].firstHosyuTxt.length > 10) {
            fontSize = 17; zureY = 4;
        }
        this.SetPicStr(PN_dgrFstHousyuTxt  , SCol.Yel + "初回報酬：" + ViewDgnDatas[dgnsel_SelNo].firstHosyuTxt ,
            fontSize , _baseX + 39 , _baseY + 556 + zureY , opacity);
    }
    else {
        this.DelPict(PN_dgrFstHousyuTxt);
    }
    for (let i = PN_dgsDData; i <= PN_dgrFstHousyuTxt; i++) {
        this.MovePict(i , true , true , 10 , 255 , true , true);
    }
    this.MovePict(PN_dgsDBase, true, true, 10);
    this.SetPictC(PN_dgsDBtn, DgnSelUIPath + _BtnStr, 1096, 741, opacity);
    this.MovePict(PN_dgsDBtn, true, true, 10);
}
let 表示ダンジョン数確認 = function(){
    if (DgnView_List.length > 0) return true;
    return false;
}
let SubDgn開放 = function (dgnId) {
    if ($gameSystem.subDgnOpens == undefined) $gameSystem.subDgnOpens = [];
    if ($gameSystem.subDgnOpens.indexOf(dgnId) == -1) {
        var rdData = RD_MasterData_Get(dgnId);
        if (!rdData.mainFlg) $gameSystem.subDgnOpens.push(dgnId);
    }
}
let SubDgnAuto_Set = function () {
    for (let i = 0; i < $gameSystem.subDgnOpens.length; i++) {
        DgnView_Set($gameSystem.subDgnOpens[i]);
    }
}
