const GalPicPath = "pictures/Gallery/";
const PN_GalBase = 181;
const PN_GalBtn = PN_GalBase + 1;
const PN_GalBackBtn = PN_GalBtn + 7;
const PN_GalPageSelecter = PN_GalBackBtn + 1;
const PN_GalEvent = PN_GalPageSelecter + 1;
const PN_GalEventName = PN_GalEvent + 16;
const PN_GalEventSelct = PN_GalEventName + 1;
const PN_GalDtlWin = PN_GalEventSelct + 1;
const PN_GalDtlTxt = PN_GalDtlWin + 1;
const PN_GalDtlBtn = PN_GalDtlTxt + 1;
const PN_GalDtlSelect = PN_GalDtlBtn + 2;
const CN_Gallary選択 = 350;
const VN_Gallary選択No = 350;
const SW_GallaryCGのみ = 350;
let GallaryManager;
let gallaryページ選択Phase;
let gallaryイベント選択Phase;
let gallary詳細選択Phase;
let gallaryCG閲覧Phase;
let gallaryリプレイPhase;
let gallarySaveBgm = null;
Game_Interpreter.prototype.Gallary_Start = function () {
    GallaryManager = new PhaseManager(this); 
    gallaryページ選択Phase = new GallaryPageSelectPhase(this);
    gallaryイベント選択Phase = new GallaryEventSelectPhase(this);
    gallary詳細選択Phase = new GallaryDetailSelectPhase(this);
    gallaryCG閲覧Phase = new GallaryCgViewPhase(this);
    gallaryリプレイPhase = new GallaryReplayPhase(this);
    gallarySaveBgm = AudioManager.saveBgm();
    AudioManager.fadeOutBgm(0.5);
    Gallary_UpdateFrame = 0;
}
let Gallary_UpdateFrame = 0;
let Gallary_UpdateEndFrame = 0;
let Not_Gallary_UpdateFrame = 0;
Game_Interpreter.prototype.Gallary_Update = function () {
    if (Gallary_UpdateEndFrame > 0) {
        Gallary_UpdateEndFrame--;
        if (Gallary_UpdateEndFrame == 0) {
            this.Gallary_End();
        }
        return;
    }
    if (Gallary_UpdateFrame < 30) { 
        if (Gallary_UpdateFrame == 0) {
            this.SetPictFIn(PN_Ensyutu , "Black" , 0 , 0 , 0 , 0 , 30);
        }
        if (Gallary_UpdateFrame == 29) {
            this.SetPict(PN_GalBase , GalPicPath + "PN_GalBase" , 0 , 0);
            GallaryManager.CngPhase(gallaryページ選択Phase);
            this.MovePict(PN_Ensyutu , 0 , 0 , 30 , 0);
        }
        Gallary_UpdateFrame++;
        return;
    }
    GallaryManager.Update(); 
    if (Not_Gallary_UpdateFrame > 0) {
        Not_Gallary_UpdateFrame--;
        return;
    }
    if (GallaryManager.doPhase == gallary詳細選択Phase) return;
    if (GallaryManager.doPhase == gallaryCG閲覧Phase) return;
    if (GallaryManager.doPhase == gallaryリプレイPhase) return;
    var setPhase = null;
    var CNo = -1;
    var pageCnt = Math.floor($Nupu_GameData.gallaryDatas.length / 16);
    var ckPicArr = [];
    for (let i = 0; i <= pageCnt; i++) {
        ckPicArr.push(PN_GalBtn + i);
    }
    ckPicArr.push(PN_GalBackBtn);
    CNo = overPointerCkArr_GetNo(ckPicArr);
    if (CNo != -1) setPhase = gallaryページ選択Phase;
    let picArr = [];
    for (let i = 0; i < 4; i++) {
        for (let j = 0; j < 4; j++) {
            let picNo = i * 4 + j;
            picArr.push(PN_GalEvent + picNo)
        }
    }
    CNo = overPointerCkArr_GetNo(picArr);
    if (CNo != -1) setPhase = gallaryイベント選択Phase;
    if (GallaryManager.doPhase != setPhase) {
        switch (setPhase) {
            case gallaryページ選択Phase:
                GallaryManager.CngPhase(gallaryページ選択Phase);
                break;
            case gallaryイベント選択Phase:
                GallaryManager.CngPhase(gallaryイベント選択Phase);
                break;
        }
    }
    if (TouchInput.isCancelled()) {
        SoundManager.playCancel();
        return;
    }
}
Game_Interpreter.prototype.Gallary_EndStart = function () {
    Gallary_UpdateEndFrame = 30;
    this.SetPictFIn(PN_Ensyutu , "Black" , 0 , 0 , 0 , 0 , 30);
}
Game_Interpreter.prototype.Gallary_End = function () {
    TitleGallaryMode = false; 
    this.DelPictSpan(PN_GalBase , PN_GalDtlSelect);
    AudioManager.replayBgm(gallarySaveBgm);
    gallarySaveBgm = null;
    this.MovePict(PN_Ensyutu , 0 , 0 , 30 , 0);
}
Game_Interpreter.prototype.GallaryMode開放 = function (no) {
    SaveVNArrNos(VN_Gallary開放Nos , no);
    this.UTA_Save();
}
Game_Interpreter.prototype.GallaryMode全開放 = function () {
    for (let i = 0; i < $Nupu_GameData.gallaryDatas.length; i++) {
        SaveVNArrNos(VN_Gallary開放Nos , i + 1);
    }
    this.UTA_Save();
}
Game_Interpreter.prototype.GallaryModeリセット = function () {
    $gameVariables.setValue(VN_Gallary開放Nos ,0);
    $gameVariables.setValue(VN_Slave開放Nos ,0);
    this.UTA_Save();
}
Game_Interpreter.prototype.IsGallaryMode = function () {
    var ckStr = $gameVariables.value(VN_Gallary開放Nos);
    if (ckStr == 0) ckStr = "";
    var strArr = ckStr.split(',');
    if (ckStr == "") strArr = [];
    return strArr.length > 0;
}
let gallaryPageNo = 0;
let gallaryNotSelectFrame = 0;
class GallaryPageSelectPhase extends PhaseBase {
    constructor() {
        super();
        this.selectNo = 0;
        this.drawSelectNo = 0;
        this.selectMaxNo = 3;
    }
    Enter(gi) { 
        this.DrawEventPage(gi); 
        this.selectMaxNo = Math.floor($Nupu_GameData.gallaryDatas.length / 16) + 1;
        gi.DelPict(PN_GalEventName);
    }
    Update(gi) {
        if (gallaryNotSelectFrame > 0) {
            gallaryNotSelectFrame--;
            return;
        }
        let mvFlg = false;
        if (Input.isTriggered('up')) {
            this.selectNo--;
            mvFlg = true;
        }
        if (Input.isTriggered('down')) {
            this.selectNo++;
            mvFlg = true;
        }
        if (this.selectNo < 0) this.selectNo = this.selectMaxNo;
        if (this.selectNo > this.selectMaxNo) this.selectNo = 0;
        var OkFlg = false;
        var pageCnt = Math.floor($Nupu_GameData.gallaryDatas.length / 16);
        var ckPicArr = [];
        for (let i = 0; i <= pageCnt; i++) {
            ckPicArr.push(PN_GalBtn + i);
        }
        ckPicArr.push(PN_GalBackBtn);
        var CNo = overPointerCkArr_GetNo(ckPicArr);
        if (CNo != -1) {
            if (CNo != this.selectNo)  {
                this.selectNo = CNo; mvFlg = true;
            }
            if (TouchInput.isTriggered()) OkFlg = true;
        }
        if (Input.isTriggered('ok')) OkFlg = true;
        if (this.selectNo != this.selectMaxNo) this.drawSelectNo = this.selectNo;
        if (mvFlg) {
            SoundManager.playCursor();
            this.DrawEventPage(gi);
            gallaryイベント選択Phase.Reset();
        }
        if (Input.isTriggered('right')) {
            this.selectNo = this.drawSelectNo;
            GallaryManager.CngPhase(gallaryイベント選択Phase);
        }
        if (Input.isTriggered('menu')) {
            SoundManager.playCancel();
            gi.Gallary_EndStart();
            return;
        }
        if (OkFlg) {
            if (this.selectNo == this.selectMaxNo) { 
                SoundManager.playCursor();
                gi.Gallary_EndStart();
                return;
            } else {
                SoundManager.playOk();
                GallaryManager.CngPhase(gallaryイベント選択Phase);
                SoundManager.playOk();
            }
        }
    }
    Exit(gi) {
        let backAlpha = 255 * 0.44;
        gi.SetPict( PN_GalBackBtn , GalPicPath + "PN_GalBackBtn" , 0 , 698 , backAlpha);
    }
    DrawEventPage(gi) {
        let notSelectAlpha = 0.44;
        var pageCnt = Math.floor($Nupu_GameData.gallaryDatas.length / 16);
        for (let i = 0; i <= pageCnt; i++) {
            let setAlpah = 255 * notSelectAlpha;
            if (this.drawSelectNo == i) {
                setAlpah = 255;
                gi.SetPict( PN_GalPageSelecter , GalPicPath + "PN_GalPageSelecter" , 188 , 155 + i * 61);
            }
            gi.SetPict( PN_GalBtn + i , GalPicPath + "PN_GalBtn" + i , 43 , 141 + i * 61 , setAlpah);
        }
        let backAlpha = 255 * notSelectAlpha;
        if (this.selectNo == this.selectMaxNo) {
            backAlpha = 255;
            //i.DelPict(PN_GalPageSelecter);
        }
        gallaryPageNo = this.drawSelectNo;
        gi.SetPict( PN_GalBackBtn , GalPicPath + "PN_GalBackBtn" , 0 , 698 , backAlpha);
        var openEventArr = GetVNArrNos(VN_Gallary開放Nos);
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                let addPicNo = i * 4 + j;
                let eventNo = addPicNo + this.drawSelectNo * 16 + 1;
                let picPosX = 229 + (250 / 2) + j * 299;
                let picPosY = 104 + (142 / 2) + i * 169;
                if ($Nupu_GameData.gallaryDatas.length <= eventNo) {
                    gi.DelPict(PN_GalEvent + addPicNo);
                } else {
                    var setEventPicName = "Event" + eventNo;
                    if (openEventArr.indexOf(String(eventNo)) == -1) setEventPicName = "NotEvent";
                    gi.SetPictC(PN_GalEvent + addPicNo , GalPicPath + "Image/" + setEventPicName ,
                        picPosX , picPosY);
                }
            }
        }
    }
}
class GallaryEventSelectPhase extends PhaseBase {
    constructor() {
        super();
        this.selectNoX = 0; this.selectNoXMax = 3; this.befSelectNoX = 0;
        this.selectNoY = 0; this.selectNoYMax = 3; this.befSelectNoY = 0;
    }
    Reset() {
        this.selectNoX = 0;
        this.selectNoY = 0;
    }
    Enter(gi) { 
    }
    Update(gi) {
        let picPosX = 229 + (250 / 2) + this.selectNoX * 299;
        let picPosY = 104 + (142 / 2) + this.selectNoY * 169;
        if (Input.isTriggered('up')) {
            this.selectNoY--;
            if (this.selectNoY < 0) this.selectNoY = this.selectNoYMax;
        }
        if (Input.isTriggered('down')) {
            this.selectNoY++;
            if (this.selectNoY > this.selectNoYMax) this.selectNoY = 0;
        }
        if (Input.isTriggered('left')) {
            this.selectNoX--;
            if (this.selectNoX < 0) {
                GallaryManager.CngPhase(gallaryページ選択Phase); 
                this.selectNoX = 0;
                return;
            }
        }
        if (Input.isTriggered('right')) {
            this.selectNoX++;
            if (this.selectNoX > this.selectNoXMax) this.selectNoX = this.selectNoXMax;
        }
        var picArr = [];
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                let picNo = i * 4 + j;
                picArr.push(PN_GalEvent + picNo)
            }
        }
        var OkFlg = false;
        var CNo = overPointerCkArr_GetNo(picArr);
        if (CNo != -1) {
            if (CNo != this.selectNo)  {
                this.selectNoY = Math.floor(CNo / 4);
                this.selectNoX = CNo % 4;
            }
            if (TouchInput.isTriggered()) OkFlg = true;
        }
        if (Input.isTriggered('ok')) OkFlg = true;
        if (this.selectNoX != this.befSelectNoX || this.selectNoY != this.befSelectNoY) {
            var openEventArr = GetVNArrNos(VN_Gallary開放Nos);
            this.befSelectNoX = this.selectNoX;
            this.befSelectNoY = this.selectNoY;
            D_Text_Cng_font = "Makinas"; 
            var selectEventNo = this.selectNoY * 4 + this.selectNoX + gallaryPageNo * 16;
            var setName = $Nupu_GameData.gallaryDatas[selectEventNo].name;
            var openEventArr = GetVNArrNos(VN_Gallary開放Nos);
            if (openEventArr.indexOf(String(selectEventNo + 1)) == -1) setName = "Undiscovered";
            gi.SetPicStrC( PN_GalEventName , setName , 30 , 945 , 60);
            D_Text_Cng_font = ""; 
            SoundManager.playCursor();
        }
        if (OkFlg) {
            let setparam = new GallaryDetailSelectPhaseParam();
            setparam.selectNoX = this.selectNoX;
            setparam.selectNoY = this.selectNoY;
            var selectEventNo = this.selectNoY * 4 + this.selectNoX + gallaryPageNo * 16;
            var openEventArr = GetVNArrNos(VN_Gallary開放Nos);
            if (openEventArr.indexOf(String(selectEventNo + 1)) == -1) return;
            setparam.gallaryData = $Nupu_GameData.gallaryDatas[selectEventNo];
            $gameVariables.setValue(VN_Gallary選択No ,selectEventNo);
            SoundManager.playOk();
            gallary詳細選択Phase.SetParam(setparam);
            GallaryManager.CngPhase(gallary詳細選択Phase);
            return;
        }
        if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
            SoundManager.playCancel();
            GallaryManager.CngPhase(gallaryページ選択Phase); 
            return;
        }
        gi.SetPictC(PN_GalEventSelct , GalPicPath + "PN_GalEventSelct" , picPosX , picPosY);
    }
    Exit(gi) {
        gi.DelPict(PN_GalEventSelct);
    }
}
class GallaryDetailSelectPhase extends PhaseBase {
    constructor() {
        super();
        this.selectNo = 0; this.befSelectNo = 0;
        this.selectNoMax = 1;
    }
    Enter(gi) { 
        let picPosX = 229 + (250 / 2) + this.param.selectNoX * 299;
        let picPosY = 104 + (142 / 2) + this.param.selectNoY * 169;
        gi.SetPictC(PN_GalDtlWin , GalPicPath + "PN_GalDtlWin" , picPosX , picPosY);
        if (this.param.gallaryData.replayOnlyFlg) {
            gi.SetPictC(PN_GalDtlBtn + 1 , GalPicPath + "PN_GalDtlBtn2" , picPosX , picPosY - 33);
        } else if(this.param.gallaryData.cgOnlyFlg){
            gi.SetPictC(PN_GalDtlBtn + 0 , GalPicPath + "PN_GalDtlBtn1" , picPosX , picPosY - 33);
        } else {
            gi.SetPictC(PN_GalDtlBtn + 0 , GalPicPath + "PN_GalDtlBtn1" , picPosX , picPosY - 33);
            gi.SetPictC(PN_GalDtlBtn + 1 , GalPicPath + "PN_GalDtlBtn2" , picPosX , picPosY + 33);
        }
    }
    Update(gi) {
        if (Input.isTriggered('up')) {
            this.selectNo--;
            if (this.selectNo < 0) this.selectNo = this.selectNoMax;
        }
        if (Input.isTriggered('down')) {
            this.selectNo++;
            if (this.selectNo > this.selectNoMax) this.selectNo = 0;
        }
        var OkFlg = false;
        var CNo = overPointerCkArr_GetNo([PN_GalDtlBtn + 0 , PN_GalDtlBtn + 1]);
        if (CNo != -1) {
            if (CNo != this.selectNo)  {
                this.selectNo = CNo;
            }
            if (TouchInput.isTriggered()) OkFlg = true;
        }
        if (this.param.gallaryData.replayOnlyFlg || this.param.gallaryData.cgOnlyFlg) this.selectNo = 0;
        if (Input.isTriggered('ok')) OkFlg = true;
        if (OkFlg) {
            let setParam = new GallaryCgViewPhaseParam();
            if (this.param.gallaryData.replayOnlyFlg) {
                gi.PlaySe("Title_Gun");
                GallaryManager.CngPhase(gallaryリプレイPhase);
            } else if (this.param.gallaryData.cgOnlyFlg) {
                setParam.viewCgs = this.param.gallaryData.cgDatas;
                if (this.param.gallaryData.cgBack != null) setParam.backCg = this.param.gallaryData.cgBack;
                gallaryCG閲覧Phase.SetParam(setParam);
                SoundManager.playOk();
                GallaryManager.CngPhase(gallaryCG閲覧Phase);
            } else {
                if (this.selectNo == 0) { 
                    setParam.viewCgs = this.param.gallaryData.cgDatas;
                    if (this.param.gallaryData.cgBack != null) setParam.backCg = this.param.gallaryData.cgBack;
                    gallaryCG閲覧Phase.SetParam(setParam);
                    SoundManager.playOk();
                    GallaryManager.CngPhase(gallaryCG閲覧Phase);
                } else {
                    gi.PlaySe("Title_Gun");
                    GallaryManager.CngPhase(gallaryリプレイPhase);
                }
            }
            return;
        }
        if (this.befSelectNo != this.selectNo) {
            SoundManager.playCancel();
            this.befSelectNo = this.selectNo;
        }
        if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
            SoundManager.playCancel();
            Not_Gallary_UpdateFrame = 2;
            GallaryManager.CngPhase(gallaryイベント選択Phase); 
            return;
        }
        let picPosX = 229 + (250 / 2) + this.param.selectNoX * 299;
        let picPosY = 104 + (142 / 2) + this.param.selectNoY * 169 - 33 + 66 * this.selectNo;
        gi.SetPictC(PN_GalDtlSelect , GalPicPath + "PN_GalDtlSelect" , picPosX , picPosY);
    }
    Exit(gi) {
        gi.DelPictSpan(PN_GalDtlWin , PN_GalDtlSelect);
    }
}
class GallaryDetailSelectPhaseParam {
    constructor() {
        this.selectNoX = 0;
        this.selectNoY = 0;
        this.gallaryData = null;
    }
}
const PN_GalBack = 458;
const PN_GalCgView = PN_GalBack + 1;
class GallaryCgViewPhase extends PhaseBase {
    constructor() {
        super();
        this.selectNo = 0;
    }
    Enter(gi) { 
        this.selectNo = 0;
        this.CGDraw(gi);
        if (this.param.backCg != "") gi.SetPict(PN_GalBack , L0背景Path + this.param.backCg);
        var preLoadArr = [];
        for (let i = 0; i < this.param.viewCgs.length; i++) {
            preLoadArr.push("pictures/" + this.param.viewCgs[i]);
        }
        gi.PreLoadArr(preLoadArr);
    }
    Update(gi) {
        gi.SetPict( PN_GalRtnBtn , GalPicPath + "PN_GalRtnBtn" , PN_GalRtnBtnX , PN_GalRtnBtnY);
        if (overPointerCk(PN_GalRtnBtn)) {
            gi.PicCngColor( PN_GalRtnBtn , [50,50,50]);
            if (TouchInput.isTriggered() || Input.isTriggered('ok')) {
                SoundManager.playCursor();
                GallaryManager.CngPhase(gallaryイベント選択Phase); 
            }
        } else {
            if (TouchInput.isTriggered() || Input.isTriggered('ok')) {
                SoundManager.playCursor();
                this.selectNo++;
                if (this.param.viewCgs.length == this.selectNo) {
                    gi.DelPict(PN_GalBack);
                    gi.DelPict(PN_GalCgView);
                    gallaryNotSelectFrame = 5;
                    GallaryReturnボタン表示 = false;
                    gi.DelPict(PN_GalRtnBtn);
                    GallaryManager.CngPhase(gallaryイベント選択Phase); 
                }
                else {
                    this.CGDraw(gi);
                }
            }
        }
    }
    Exit(gi) {
        gi.DelPictSpan(PN_GalBack , PN_GalRtnBtn);
    }
    CGDraw(gi) {
        gi.SetPict( PN_GalCgView , this.param.viewCgs[this.selectNo] , 0 , 0);
    }
}
class GallaryCgViewPhaseParam {
    constructor() {
        this.viewCgs = [];
        this.backCg = "";
    }
}
class GallaryReplayPhase extends PhaseBase {
    constructor() {
        super();
    }
    Enter(gi) { 
        gi.SetCmnEventSc(CN_Gallary選択); 
    }
    Update(gi) {
    }
    Exit(gi) {
    }
}
class GallaryReplayPhaseParam {
    constructor() {
        this.viewCgs = [];
    }
}
Game_Interpreter.prototype.Title画像を全て消す = function () {
    this.ClearPtcAll(); 
    L2dFadeMdl("Title","out",1,""); 
    this.DelPictSpan(PN_sBack , PN_GalDtlSelect);
}
let gallaryリプレイEndFlg = false;
Game_Interpreter.prototype.GallaryリプレイEnd = function () {
    gallaryリプレイEndFlg = true;
    GallaryReturnボタン表示 = false;
    this.DelPict(PN_GalRtnBtn);
    this.InstantLoad(); 
}
Game_Interpreter.prototype.Gallaryリプレイ戻り = function () {
    gallaryリプレイEndFlg = false;
    Not_Gallary_UpdateFrame = 2;
    GallaryManager.CngPhase(gallaryイベント選択Phase); 
    L2dFadeMdl("Title","in",1,"1yure" , 1, 1 ,100); 
}
const PN_GalRtnBtn = 410;
let GallaryEventData = null;
let PN_GalRtnBtnX = 1280; let PN_GalRtnBtnY = 750;
let GallaryReturnボタン表示 = false;
let Gallaryリプレイ_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    Gallaryリプレイ_NUpdateSc.call(this);
    if (GallaryReturnボタン表示) {
        this.SetPict( PN_GalRtnBtn , GalPicPath + "PN_GalRtnBtn" , PN_GalRtnBtnX , PN_GalRtnBtnY);
        if (overPointerCk(PN_GalRtnBtn)) {
            this.PicCngColor( PN_GalRtnBtn , [50,50,50]);
            if (TouchInput.isTriggered()) {
                this._index = this._list.length;
                this.GallaryリプレイEnd();
            }
        }
    }
}
