const SaveCount = 36; 
const AutoSaveCount = 3; 
const _SvDataCnt = 12; 
let NowLoadNo = -1; 
let AutoSaveViewNo = -1; //オートセーブを表示している時のNo(-1の時は表示しない)
let SaveSelPage = 0; 
let SaveSelectNo = 0; 
let SaveOverNo = -1; 
let NLoadSuccessFlg = false; 
let SaveVar = [];
let SavePsVar = []; 
let Save_ZureX = 0; 
let Save_ZureY = -20; 
let LoadModeFlg = false; 
Game_Interpreter.prototype.SaveVarGet = function () {
    let _SetSaveStr = $gameVariables.value(VN_SaveStr);
    SaveVar = [];
    if (_SetSaveStr != 0) SaveVar = _SetSaveStr.split(',');
    SavePsVar = [];
    let _SetSaveStr_2 = $gameVariables.value(VN_SavePsStr);
    if (_SetSaveStr_2 != 0) SavePsVar = _SetSaveStr_2.split(',');
}
let SaveVarSet = function () {
    let _SetSaveStr = SaveVar.join(',');
    $gameVariables.setValue(VN_SaveStr, _SetSaveStr); 
    let _SetSaveStr_2 = SavePsVar.join(',');
    $gameVariables.setValue(VN_SavePsStr, _SetSaveStr_2); 
}
Game_Interpreter.prototype.SaveVarReset = function () {
    if (SaveVar.length == 0) { 
        let _SetSaveStrs = [];
        for (let i = 0; i <= SaveCount + AutoSaveCount - 1; i++) {
            _SetSaveStrs.push("--:--:--");     
            _SetSaveStrs.push("未使用データ");   
            _SetSaveStrs.push("SaveCharaNew"); 
            _SetSaveStrs.push("");      
            _SetSaveStrs.push("---");   
            _SetSaveStrs.push("---");   
        }
        SaveVar = _SetSaveStrs;
    }
    if (SavePsVar.length == 0) {
        for (let i = 0; i <= SaveCount + AutoSaveCount - 1; i++) {
            SavePsVar.push("");
        }
    }
    while (SaveVar[((SaveCount + AutoSaveCount) * 6)] == undefined) {
        SaveVar.push("--:--:--"); 
        SaveVar.push("未使用データ"); 
        SaveVar.push("SaveCharaNew"); 
        SaveVar.push("");   
        SaveVar.push("---"); 
        SaveVar.push("---"); 
        SavePsVar.push("");
    }
}
let _LoadOKFlg = false; 
Game_Interpreter.prototype.MenuSaveView = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    let _NotColCngFlg = false;
    ViewSpriteArr_Reset();
    if (SaveSelPage > 2) SaveSelPage = 2; 
    let _ViewSaveSelectNo = SaveSelectNo + (SaveSelPage * _SvDataCnt) + 1;
    if (AutoSaveViewNo != -1) {
        _ViewSaveSelectNo = SaveCount + AutoSaveViewNo + 1;
    }
    this.SaveVarGet();
    this.SaveVarReset();
    let _SetOpi = 255;
    if (_SetFlg) _SetOpi = 0;
    ViewSpriteArr = []; 
    this.SetSprite(PNo_SBase, MenuUIPath + "Save/S_Base", 566 + Save_ZureX, 190 + Save_ZureY, _SetOpi);
    if (!LoadModeFlg) {
        this.SetSprite(PNo_SBtn, MenuUIPath + "Save/S_SaveBtn", 663 + Save_ZureX, 633 + Save_ZureY, _SetOpi);
    }
    let _sPosl = 216;
    let _sPosr = 386;
    let _sPosY = 238;
    for (let i = 0; i <= _SvDataCnt - 1; i++) {
        let _fn = (SaveSelPage * _SvDataCnt) + i + 1;
        if (_fn > 36) continue;
        let _fr = Math.floor(i / 2);
        if (i % 2 == 0) {
            this.SetSprite(PNo_SFileBt + i, MenuUIPath + "Save/S_File" + _fn + "a",
                _sPosl + Save_ZureX, _sPosY + Save_ZureY + (81 * _fr), _SetOpi);
        } else {
            this.SetSprite(PNo_SFileBt + i, MenuUIPath + "Save/S_File" + _fn + "a",
                _sPosr + Save_ZureX, _sPosY + Save_ZureY + (81 * _fr), _SetOpi);
        }
    }
    for (let i = 0; i <= ViewSpriteArr.length - 1; i++) {
        this.MoveSprite(ViewSpriteArr[i], true, true, 10);
        this.SpriteCngColor(ViewSpriteArr[i]); 
    }
    for (let i = 0; i <= _SvDataCnt - 1; i++) {
        let fNo = 1 + i + (_SvDataCnt * SaveSelPage);
        let saveData = DataManager.loadSavefileInfo(fNo);
        if (saveData == null) { 
            this.SpriteCngColor(PNo_SFileBt + i, [-100, -100, -100, 0]); 
        }
    }
    if (LoadModeFlg) {
        for (let i = 0; i < AutoSaveCount; i++) {
            let fNo = SaveCount + i + 1; 
            let saveData = DataManager.loadSavefileInfo(fNo);
            this.SpriteCngColor(PNo_SAuto + i); 
            this.SetSprite(PNo_SAuto + i, MenuUIPath + "Save/S_AutoSave" + (i + 1) + "a", 444 + 175 * i, 699, 255);
            if (saveData == null) { 
                this.SpriteCngColor(PNo_SAuto + i, [-100, -100, -100, 0]); 
            }
            if ($gameVariables.value(VN_AutoSaveNo) == i + 1) {
                this.SetSpriteC(PNo_SAutoNew, MenuUIPath + "Save/PNo_SAutoNew",
                    444 + 86 + 175 * i, 699 + 34, 255);
            }
        }
    }
    for (let i = 0; i <= 3 - 1; i++) {
        this.SpriteCngColor(PN_SPage + i);
        let _setPar = 90;
        if (SaveSelPage == i) _setPar = 100;
        this.SetSpriteC(PN_SPage + i, MenuUIPath + "Save/S_Page" + (i + 1),
            269 + Save_ZureX + (117 * i), 210 + Save_ZureY, _SetOpi, _setPar, _setPar);
        this.MoveSprite(PN_SPage + i, true, true, 10, 255, true, true);
    }
    this.SpriteCngColor(PN_SPage + SaveSelPage, [150, 150, 150, 0]);
    if (SaveOverNo != -1 && !_NotColCngFlg) {
        this.SpriteCngColor(SaveOverNo, [100, 100, 100, 0]);
    }
    ViewSpriteArr_Reset(); 
    if (_ViewSaveSelectNo <= 0) _ViewSaveSelectNo = 1;
    if (SaveSelectNo < 0) {
        SaveSelectNo = 0;
    }
    let btnSelectNo = SaveSelectNo + 1 + (SaveSelPage * _SvDataCnt);
    let _BtnName = "Save/S_File" + btnSelectNo + "b";
    let _posX = _sPosl;
    if (SaveSelectNo % 2 == 1) _posX = _sPosr;
    let _posYc = Math.floor(SaveSelectNo / 2);
    if (AutoSaveViewNo != -1 && LoadModeFlg) {
        _BtnName = "Save/S_AutoSave" + (AutoSaveViewNo + 1) + "b";
        this.SetSprite(PNo_SAuto + AutoSaveViewNo, MenuUIPath + _BtnName,
            444 + 175 * AutoSaveViewNo, 699, _SetOpi);
    } else {
        this.SetSprite(PNo_SFileBt + SaveSelectNo, MenuUIPath + _BtnName,
            _posX + Save_ZureX, _sPosY + Save_ZureY + (81 * _posYc), _SetOpi);
    }
    if (_File_CngFlg) {
        let saveData = DataManager.loadSavefileInfo(_ViewSaveSelectNo);
        let DrawPlayTimer = "--:--:--";
        let DrawSaveTitle = "未使用データ";
        let DrawCharaImg = "SaveCharaNew";
        let DrawHintTxt = "";
        let DrawRateTxt = "---";
        let DrawHasCards = "---";
        let DrawMapName = "";
        let DrawTenki = "";
        _LoadOKFlg = false; 
        if (LoadModeFlg) this.SetSprite(PNo_SBtn, MenuUIPath + "Save/S_LoadBtnb", 663 + Save_ZureX, 633 + Save_ZureY, _SetOpi);
        if (saveData != null) { 
            _LoadOKFlg = true; 
            if (saveData.PlayTimer != undefined) DrawPlayTimer = saveData.PlayTimer;
            if (saveData.SaveTitle != undefined) DrawSaveTitle = saveData.SaveTitle;
            if (saveData.CharaImg != undefined) DrawCharaImg = saveData.CharaImg;
            if (saveData.HintTxt != undefined) DrawHintTxt = saveData.HintTxt;
            if (saveData.RateTxt != undefined) DrawRateTxt = saveData.RateTxt;
            if (saveData.HasCards != undefined) DrawHasCards = saveData.HasCards;
            if (saveData.MapName != undefined) DrawMapName = saveData.MapName;
            if (saveData.Tenki != undefined) DrawTenki = saveData.Tenki;
            if (LoadModeFlg) this.SetSprite(PNo_SBtn, MenuUIPath + "Save/S_LoadBtn", 663 + Save_ZureX, 633 + Save_ZureY, _SetOpi);
        }
        let _BtnName = "Save/S_File" + _ViewSaveSelectNo + "Str";
        if (AutoSaveViewNo != -1) _BtnName = "Save/S_AutoSave" + (AutoSaveViewNo + 1) + "Str";
        this.SetSprite(PNo_SFileP, MenuUIPath + _BtnName, 600 + Save_ZureX, 204 + Save_ZureY, _SetOpi);
        this.DrawSaveDetail(DrawPlayTimer, DrawSaveTitle, DrawCharaImg, DrawHintTxt, DrawRateTxt, DrawHasCards , DrawMapName , DrawTenki, _SetOpi);
        _File_CngFlg = false;
    }
    for (let i = 0; i <= ViewSpriteArr.length - 1; i++) {
        this.MoveSprite(ViewSpriteArr[i], true, true, 10);
    }
};
Game_Interpreter.prototype.DrawSaveDetail = function (DrawPlayTimer , DrawSaveTitle , DrawCharaImg , DrawHintTxt , DrawRateTxt , DrawHasCards , DrawMapName , DrawTenki , _SetOpi) {
    D_Text_Cng_font = "Makinas";
    this.SpriteStr(PNo_STmTxt, DrawPlayTimer, 24, 797 + Save_ZureX, 201 + Save_ZureY); 
    this.SpriteStr(PNo_STiTxt, DrawSaveTitle, 22, 621 + Save_ZureX, 255 + Save_ZureY); 
    this.PlaySpriteAnimeC(PNo_SChara, MenuUIPath + "Save/" + DrawCharaImg,
        6, 10, 759 + Save_ZureX, 405 + Save_ZureY);
    let _HintStr = DrawHintTxt; 
    let _hntRow1 = _HintStr;
    let _hntRow2 = "";
    if (_HintStr.length >= 17) {
        _hntRow1 = _HintStr.substring(0, 16);
        _hntRow2 = _HintStr.substring(16);
    }
    this.SpriteStr(PNo_SHTxt, _hntRow1, 21, 599 + Save_ZureX, 479 + Save_ZureY);
    this.SpriteStr(PNo_SHTxt + 1, _hntRow2, 21, 599 + Save_ZureX, 505 + Save_ZureY);
    this.SpriteStr(PNo_SSTxt, DrawRateTxt, 21, 822 + Save_ZureX, 556 + Save_ZureY); 
    this.SpriteStr(PNo_SCard, DrawHasCards, 21, 822 + Save_ZureX, 584 + Save_ZureY); 
    if (DrawTenki != "") {
        this.SetSprite(PN_STenki, MenuUIPath + "Save/" + DrawTenki, 566 + Save_ZureX, 190 + Save_ZureY, _SetOpi);
    } else {
        this.DelSprite(PN_STenki);
    }
    if (DrawMapName != "") {
        this.SetSpriteC(PN_SBasyo, MapPicPath + "MapName/" + DrawMapName, 760 + Save_ZureX, 330 + Save_ZureY, _SetOpi);
    } else {
        this.DelSprite(PN_SBasyo);
    }
    D_Text_Cng_font = "";
}
let _File_CngFlg = false;
Game_Interpreter.prototype.MenuSaveUpdate = function (_SetFlg) {
    let _SaveCkFileArr = [];
    for (let i = 0; i <= 11; i++) {
        _SaveCkFileArr.push(PNo_SFileBt + i); 
    }
    for (let i = 0; i <= AutoSaveCount - 1; i++) {
        _SaveCkFileArr.push(PNo_SAuto + i); 
    }
    _SaveCkFileArr.push(PNo_SBtn); 
    _SaveCkFileArr.push(PN_SPage + 0); 
    _SaveCkFileArr.push(PN_SPage + 1); 
    _SaveCkFileArr.push(PN_SPage + 2); 
    let _SaveOverFileNo = overPointerSpCkArr(_SaveCkFileArr);
    if (_SaveOverFileNo != -1) { 
        if (TouchInput.isTriggered()) { 
            switch (_SaveOverFileNo) {
                case PNo_SBtn: 
                    if (!LoadModeFlg) { 
                        this.SaveBtnClick(); 
                    } else {
                        this.LoadBtnClick(); 
                    }
                    break;
                case PN_SPage + 0: 
                case PN_SPage + 1:
                case PN_SPage + 2:
                    SaveSelPage = _SaveOverFileNo - PN_SPage;
                    SaveSelectNo = 0;
                    _File_CngFlg = true;
                    this.MenuSaveView();
                    SoundManager.playOk();
                    break;
                default:
                    SaveOverNo = -1;
                    AutoSaveViewNo = -1;
                    SaveSelectNo = _SaveOverFileNo - PNo_SFileBt;
                    _File_CngFlg = true;
                    switch (_SaveOverFileNo) { 
                        case PNo_SAuto + 0:
                        case PNo_SAuto + 1:
                        case PNo_SAuto + 2:
                            AutoSaveViewNo = _SaveOverFileNo - PNo_SAuto;
                            break;
                    }
                    this.MenuSaveView();
                    SoundManager.playOk();
                    break;
            }
        } else {
            if (SaveOverNo != _SaveOverFileNo) {
                SaveOverNo = _SaveOverFileNo;
                this.MenuSaveView();
            }
        }
    } else {
        if (SaveOverNo != -1) {
            SaveOverNo = -1;
            this.MenuSaveView();
        }
    }
};
let SaveClickOffFlame = 0;
Game_Interpreter.prototype.SaveBtnClick = function () {
    let _ViewSaveSelectNo = SaveSelectNo + (SaveSelPage * _SvDataCnt);
    this.NDataSave(_ViewSaveSelectNo); 
    if (menuAutoCloseFlame > 0) {
        menuAutoCloseFlame += 60;
    }
    _SaveWaitTimer = 15;
    SaveClickOffFlame = 20;
    _SaveFileNo = _ViewSaveSelectNo + 1;
    SaveCompAction = (gi) => {
        gi.MenuSaveView(); 
        let infoData = DataManager.makeSavefileInfo();
        gi.DrawSaveDetail(infoData.PlayTimer,
            infoData.SaveTitle,
            infoData.CharaImg,
            infoData.HintTxt,
            infoData.RateTxt,
            infoData.HasCards,
            infoData.MapName, infoData.Tenki ,255);
    }
    NowLoadNo = _ViewSaveSelectNo + 1; 
    $gameVariables.setValue(VN_BefSaveNo, _ViewSaveSelectNo + 1);
    _File_CngFlg = true; 
    this.SetSprite(PNo_SBase_Flash, MenuUIPath + "Save/S_Base_Flash", 566 + Save_ZureX, 190 + Save_ZureY, 255);
    this.MoveSprite(PNo_SBase_Flash, true, true, 10, 0);
    SoundManager.playSave();
};
Game_Interpreter.prototype.NDataSave = function (_SaveNo) {
    Live2D_Save();
    SaveVarSet(); 
}
let GetSaveHintStr = function () {
    var hintStr = $gameVariables.value(VN_HintStr);
    if (hintStr == 0) return;
    hintStr = hintStr.replace(/\\V\[(\d+)\]/gi, function() { 
        return $gameVariables.value(parseInt(arguments[1]));
    });
    return hintStr;
}
let GetSaveSijiView = function () {
    if ($gameSwitches.value(SW_SijiView)) { 
        return $gameVariables.value(VN_Sijiritu) + " %";
    }
    return "---";
}
let _autoSaveFlame = -1;
const SetAutoAutoSaveFlame = 60 * 60 * 2; 
let _AutoAutoSaveFlame = SetAutoAutoSaveFlame;
let _AutoSaveWait = 0;
Game_Interpreter.prototype.NAutoDataSave = function () {
    this.SaveVarGet();
    this.SaveVarReset();
    _autoSaveFlame = 10;
}
let _AutoSaveUpdate = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    _AutoSaveUpdate.call(this);
    if (_AutoAutoSaveFlame > 0) _AutoAutoSaveFlame--;
    if (_AutoSaveWait > 0) _AutoSaveWait--;
    if (_AutoAutoSaveFlame == 0 && _AutoSaveWait <= 0 &&
        !$gameMap.isEventRunning() && !battleflg && !RD_Flg && !MenuFlgSwich && !fuzokuFlg) {
        this.NAutoDataSave(); 
        _AutoAutoSaveFlame = SetAutoAutoSaveFlame;
    }
    if (_autoSaveFlame >= 0 && $gameVariables.value(VN_PrologueSN) >= 0) {
        if (_autoSaveFlame == 0) {
            var saveNo = $gameVariables.value(VN_AutoSaveNo);
            saveNo++;
            if (saveNo > 3) saveNo = 1;
            $gameVariables.setValue(VN_AutoSaveNo ,saveNo);
            this.NDataSave(SaveCount + saveNo - 1);
            this.GameFileSave(SaveCount + saveNo); 
            this.SetPictC(PN_AutoSaveIcon , "PN_AutoSaveIcon" , 20 , 792);
            $gameScreen.rotatePicture(PN_AutoSaveIcon, 20);
            StacWaitPict(PN_AutoSaveIcon , 30);
            StacMovePict(PN_AutoSaveIcon , true , true , 30 , 0);
            this.SetPict( PN_AutoSaveTxt , "PN_AutoSaveTxt" , 35 , 785);
            StacWaitPict(PN_AutoSaveTxt , 30);
            StacMovePict(PN_AutoSaveTxt , true , true , 30 , 0);
        }
        _autoSaveFlame--;
    }
}
let _AutoSave_performTransfer = Game_Player.prototype.performTransfer;
Game_Player.prototype.performTransfer = function () {
    _AutoSave_performTransfer.call(this); 
    _AutoSaveWait = 60;
}
Game_Interpreter.prototype.LoadBtnClick = function () {
};
