const SlvPicPath = "pictures/SlaveSelect/";
const PN_SlvBase = 181;
const PN_SlvBaseTop = PN_SlvBase + 1;
const PN_SlvBackBtn = PN_SlvBaseTop + 1;
const PN_SlvBtn = PN_SlvBackBtn + 1;
const PN_SlvSelect = PN_SlvBtn + 8;
const PN_SlvDtlBase = PN_SlvSelect + 2;
const PN_SlvDtlSelect = PN_SlvDtlBase + 1;
const PN_SlvDtlBtn = PN_SlvDtlSelect + 1;
const PN_SlvDtlBar = PN_SlvDtlBtn + 1;
const PN_SlvDtlTopTxt = PN_SlvDtlBar + 1;
const CN_Slave選択 = 351;
const VN_Slave性経験 = 351;
const VN_Slave選択No = 352;
let SlaveSelectManager;
var Slaveメイン選択Phase;
var Slave詳細選択Phase;
let slaveSaveBgm = null;
Game_Interpreter.prototype.SlaveSelect_Start = function () {
    var slaveBackPicLoad = [
        "pictures/SlaveSelect/PN_SlvBase0","pictures/SlaveSelect/PN_SlvBase1","pictures/SlaveSelect/PN_SlvBase2","pictures/SlaveSelect/PN_SlvBase3",
        "pictures/SlaveSelect/PN_SlvBase4","pictures/SlaveSelect/PN_SlvBase5","pictures/SlaveSelect/PN_SlvBase6","pictures/SlaveSelect/PN_SlvBase7"
    ];
    this.PreLoadArr(slaveBackPicLoad);
    SlaveSelectManager = new PhaseManager(this); 
    Slaveメイン選択Phase = new SlaveMainSelectPhase();
    Slave詳細選択Phase = new SlaveDetailSelectPhase();
    slaveSaveBgm = AudioManager.saveBgm();
    AudioManager.fadeOutBgm(0.5);
    SlaveSelect_UpdateFrame = 0;
}
let SlaveSelect_UpdateFrame = 0;
let SlaveSelect_UpdateEndFrame = 0;
Game_Interpreter.prototype.SlaveSelect_Update = function () {
    if (SlaveSelect_UpdateEndFrame > 0) {
        SlaveSelect_UpdateEndFrame--;
        if (SlaveSelect_UpdateEndFrame == 0) {
            this.SlaveSelect_End();
        }
        return;
    }
    if (SlaveSelect_UpdateFrame < 30) { 
        if (SlaveSelect_UpdateFrame == 0) {
            this.SetPictFIn(PN_Ensyutu , "Black" , 0 , 0 , 0 , 0 , 30);
        }
        if (SlaveSelect_UpdateFrame == 29) {
            this.MovePict(PN_Ensyutu , 0 , 0 , 30 , 0);
            this.SetPict( PN_SlvBase , SlvPicPath + "PN_SlvBase0" , 0 , 0);
            this.SetPict( PN_SlvBaseTop , SlvPicPath + "PN_SlvBaseTop" , 0 , 0);
            SlaveSelectManager.CngPhase(Slaveメイン選択Phase);
        }
        SlaveSelect_UpdateFrame++;
        return;
    }
    if (SlaveSelectManager == null) return;
    SlaveSelectManager.Update();
}
Game_Interpreter.prototype.SlaveSelect_EndStart = function () {
    SlaveSelect_UpdateEndFrame = 30;
    this.SetPictFIn(PN_Ensyutu , "Black" , 0 , 0 , 0 , 0 , 30);
}
Game_Interpreter.prototype.SlaveSelect_End = function () {
    this.DelPictSpan(PN_SlvBase , PN_SlvSelect);
    TitleSlaveMode = false;
    AudioManager.replayBgm(slaveSaveBgm);
    slaveSaveBgm = null;
    this.MovePict(PN_Ensyutu , 0 , 0 , 30 , 0);
}
Game_Interpreter.prototype.SlaveMode開放 = function (no) {
    SaveVNArrNos(VN_Slave開放Nos , no + 1);
    this.UTA_Save();
}
Game_Interpreter.prototype.SlaveMode全開放 = function () {
    for (let i = 0; i < 7; i++) {
        SaveVNArrNos(VN_Slave開放Nos , i + 1);
    }
    this.UTA_Save();
}
Game_Interpreter.prototype.SlaveMode開始 = function () {
    SlaveSelectManager = null;
    this.Title画像を全て消す();
    this.DelPictSpan(PN_SlvBase , PN_SlvDtlTopTxt);
}
Game_Interpreter.prototype.IsSlaveMode = function () {
    var ckStr = $gameVariables.value(VN_Slave開放Nos);
    if (ckStr == 0) ckStr = "";
    var strArr = ckStr.split(',');
    if (ckStr == "") strArr = [];
    return strArr.length > 0;
}
class SlaveMainSelectPhase extends PhaseBase {
    constructor() {
        super();
        this.selectNo = 0;
        this.selectOkNos = [];
        this.selectNoMax = 8;
        this.picArr = [];
    }
    Enter(gi) { 
        this.DrawSelect(gi);
        var getArr = GetVNArrNos(VN_Slave開放Nos);
        for (let i = 0; i < getArr.length; i++) {
            this.selectOkNos.push(Number(getArr[i]) - 1);
        }
    }
    Update(gi) {
        let mvFlg = false;
        if (Input.isTriggered('up')) {
            this.selectNo--;
            while (this.selectOkNos.indexOf(this.selectNo) == -1) {
                if (this.selectNo <= -1) break;
                this.selectNo--;
            }
            mvFlg = true;
        }
        if (Input.isTriggered('down')) {
            this.selectNo++;
            while (this.selectOkNos.indexOf(this.selectNo) == -1) {
                if (this.selectNo >= this.selectNoMax) break;
                this.selectNo++;
            }
            mvFlg = true;
        }
        if (Input.isTriggered('left')) {
            this.selectNo = this.selectNoMax;
            mvFlg = true;
        }
        if (Input.isTriggered('right')) {
            this.selectNo++;
            while (this.selectOkNos.indexOf(this.selectNo) == -1) {
                if (this.selectNo >= this.selectNoMax) break;
                this.selectNo++;
            }
            mvFlg = true;
        }
        var OkFlg = false;
        var CNo = overPointerCkArr(this.picArr);
        if (CNo != -1) {
            CNo -= PN_SlvBtn;
            if (CNo != this.selectNo)  {
                this.selectNo = CNo;
                if (this.selectNo != this.selectNoMax)
                    gi.SetPict( PN_SlvBase , SlvPicPath + "PN_SlvBase" + this.selectNo , 0 , 0);
                mvFlg = true;
            }
            if (TouchInput.isTriggered()) OkFlg = true;
        }
        let EndFlg = false;
        if (overPointerCk(PN_SlvBackBtn)) {
            if (this.selectNo != this.selectNoMax) {
                this.selectNo = this.selectNoMax;
                mvFlg = true;
            }
            if (TouchInput.isTriggered()) EndFlg = true;
        }
        if (Input.isTriggered('ok')) OkFlg = true;
        if (Input.isTriggered('menu') || TouchInput.isCancelled() || EndFlg) {
            SoundManager.playCancel();
            gi.SlaveSelect_EndStart(); 
            return;
        }
        if (OkFlg) {
            if (this.selectNo == this.selectNoMax) {
                SoundManager.playCancel();
                gi.SlaveSelect_EndStart(); 
                return;
            } else {
                SoundManager.playOk();
                var setParam = new SlaveDetailSelectPhaseParam();
                setParam.selectNo = this.selectNo;
                Slave詳細選択Phase.SetParam(setParam);
                SlaveSelectManager.CngPhase(Slave詳細選択Phase);
            }
        }
        if (this.selectNo < 0) this.selectNo = this.selectNoMax;
        if (this.selectNo > this.selectNoMax) this.selectNo = 0;
        if (mvFlg) {
            SoundManager.playCursor();
            this.DrawSelect(gi);
        }
    }
    Exit(gi) {
    }
    DrawSelect(gi) {
        this.picArr = [];
        var notSelectAlpha = 0.85;
        var setAlpha = 255 * notSelectAlpha;
        var getArr = GetVNArrNos(VN_Slave開放Nos);
        for (let i = 0; i < 8; i++) {
            setAlpha = 255 * notSelectAlpha;
            if (this.selectNo == i) setAlpha = 255;
            if (getArr.indexOf(String(i + 1)) != -1) {
                gi.SetPict(PN_SlvBtn + i , SlvPicPath + "PN_SlvBtn" + i , 97 + i * 53 , 100 + i * 86 , setAlpha);
                this.picArr.push(PN_SlvBtn + i);
            }
        }
        setAlpha = 255 * 0.45;
        if (this.selectNo == this.selectNoMax) {
            setAlpha = 255;
            gi.DelPict(PN_SlvSelect);
        } else {
            gi.SetPict(PN_SlvSelect , SlvPicPath + "PN_SlvSelect" , 97 + this.selectNo * 53 - 6 , 100 + this.selectNo * 86 - 6);
        }
        gi.SetPict(PN_SlvBackBtn , SlvPicPath + "PN_SlvBackBtn" , 0 , 711 , setAlpha);
    }
}
class SlaveDetailSelectPhase extends PhaseBase {
    constructor() {
        super();
        this.select性経験 = 0; this.befSelect性経験 = 0;
        this.select性経験Max = 3;
        this.btnSelect = true; this.befBtnSelect = false;
        this.btnPushFlg = false;
    }
    Enter(gi) { 
        gi.SetPict(PN_SlvDtlBase , SlvPicPath + "PN_SlvDtlBase" , 0 , 0);
        //gi.SetPict(PN_SlvDtlTopTxt , SlvPicPath + "PN_SlvDtlTopTxt" , 0 , 0);
        gi.SetPict(PN_SlvDtlBar , SlvPicPath + "PN_SlvBtn" + this.param.selectNo , 317 , 240);
        $gameVariables.setValue(VN_Slave選択No ,this.param.selectNo);
        this.btnSelect = true;
        this.btnPushFlg = false;
    }
    Update(gi) {
        //gi.SetPict(PN_SlvDtlSelect , SlvPicPath + "PN_SlvDtlSelect" , 505 + 124 * this.select性経験 , 350);
        let mvFlg = false;
        if (Input.isTriggered('up')) {
        }
        if (Input.isTriggered('down')) {
        }
        if (Input.isTriggered('left')) {
            // if (!this.btnSelect) {
            //     if (this.select性経験 < 0) this.select性経験 = 0;
        }
        if (Input.isTriggered('right')) {
            // if (!this.btnSelect) {
            //     if (this.select性経験 > this.select性経験Max) this.select性経験 = this.select性経験Max;
        }
        if (this.befSelect性経験 != this.select性経験) {
            SoundManager.playCursor();
            this.befSelect性経験 = this.select性経験;
        }
        var OkFlg = false;
        let _cbmArr = [];
        for (let i = 0; i < 4; i++) _cbmArr.push([505 + 124 * i , 350 , 109 , 69]);
        let CNo = overPicClickable(PN_SlvDtlTopTxt , _cbmArr);
        if (CNo != -1) {
            this.btnSelect = false;
            if (CNo != this.select性経験) {
                SoundManager.playCursor();
                this.select性経験 = CNo;
            }
        }
        gi.SetPict(PN_SlvDtlBtn , SlvPicPath + "PN_SlvDtlBtn" , 588 , 419 , setAlpha);
        gi.PicCngColor(PN_SlvDtlBtn);
        if (overPointerCk(PN_SlvDtlBtn)) {
            this.btnSelect = true;
            gi.PicCngColor(PN_SlvDtlBtn , [50,50,50,0]);
            if (TouchInput.isTriggered()) OkFlg = true;
        }
        if (this.befBtnSelect != this.btnSelect) {
            this.befBtnSelect = this.btnSelect;
            SoundManager.playCursor();
        }
        if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
            SoundManager.playCancel();
            SlaveSelectManager.CngPhase(Slaveメイン選択Phase);
            return;
        }
        if (this.btnSelect && Input.isTriggered('ok')) OkFlg = true;
        if (OkFlg && !this.btnPushFlg) {
            this.btnPushFlg = true;
            gi.PlaySe("Title_Gun");
            TitleFlg = false;
            $gameVariables.setValue(VN_屈服度上昇率 ,100);
            $gameVariables.setValue(VN_開発度上昇率 ,100);
            $gameVariables.setValue(VN_Slave性経験 , this.select性経験);
            gi.SetCmnEventSc(CN_Slave選択);
        }
        var setAlpha = 255 * 0.65;
        if (this.btnSelect) {
            setAlpha = 255;
        } else {
            gi.PicCngColor(PN_SlvDtlSelect , [0 , -255 , 0]);
        }
    }
    Exit(gi) {
        gi.DelPictSpan(PN_SlvDtlBase , PN_SlvDtlTopTxt);
    }
}
class SlaveDetailSelectPhaseParam {
    constructor() {
        this.selectNo = 0;
    }
}
