const PN_L2D_CkBase = 5;
const PN_L2DNameTxt = 155;
const PN_L2DCkMtnTxt = PN_L2DNameTxt + 60;
const PN_L2DCkExpTxt = PN_L2DCkMtnTxt + 60;
const PN_L2DCkPos = PN_L2DCkExpTxt + 60;
const PN_L2DCkHntn = PN_L2DCkPos + 4;
const PN_L2DCkEvBtn = PN_L2DCkHntn + 2;
const PN_L2DCkExpTrackTxt = PN_L2DCkEvBtn + 15;
const PN_L2DMtnLoopTxt = PN_L2DCkExpTrackTxt + 11;
let SelectMotionTrackId = 0;    
let SelectExpTrackId = 0;       
let L2D_CkZureY = 0;
let L2D_CkSelNo = 0;
let L2D_CkMode = 0;
let _Live2D_Check_flame = 0;
let L2D_CkPicArr = [];
Game_Interpreter.prototype.Live2D_Checker = function () {
    if(_Live2D_Check_flame == 0){
        this.SetPict(PN_L2D_CkBase , "Live2D_Checker" , 0 , 0);
        this.Live2D_Ck_Draw();
    }
    this.Live2D_Checker_Update();
    _Live2D_Check_flame++;
    return true; 
}
let L2DEvBtnArr = [];
let L2D_CkPosNo = 0;
Game_Interpreter.prototype.Live2D_Ck_Draw = function () {
    let _Cnt = 0;
    this.DelSpriteSpan(PN_L2DNameTxt , PN_L2DNameTxt + 59);
    L2D_CkPicArr = [];
    for (let i = L2D_CkZureY; i <= modelSetting.length - 1; i++) {
        if(L2D_CkSelNo == i) this.SetPicStrBG("#336699");
        this.SpriteStr(PN_L2DNameTxt + _Cnt , modelSetting[i]._MdlName , 18 , 20 , 40 + 25 * _Cnt);
        L2D_CkPicArr.push(PN_L2DNameTxt + _Cnt);
        _Cnt++
        if(_Cnt == 28) break;
    }
    for (let i = 0; i <= 4 - 1; i++) {
        this.SetSprite(PN_L2DCkEvBtn + i , "Live2D_Checker_Btn" , 948 + (i * 73) , 6);
        L2DEvBtnArr.push(PN_L2DCkEvBtn + i);
        this.SpriteStr(PN_L2DCkEvBtn + i + 4 , "Ev" + (i + 1) , 25 , 948 + (i * 73) + 10 , 6 + 3);
    }
}
let L2D_Ck反転Flg = false;
let L2DCk_PsPicArr = [];
let L2DCk_HntnPicArr = [];
Game_Interpreter.prototype.Live2D_Ck_PosDraw = function () {
    this.DelSpriteSpan(PN_L2DCkPos , PN_L2DCkHntn + 1);
    L2DCk_PsPicArr = [];
    let _SetPsStr = "";
    for (let i = 0; i <= 3 - 1; i++) {
        switch(i){
            case 0: _SetPsStr = "right"; break;
            case 1: _SetPsStr = "center"; break;
            case 2: _SetPsStr = "left"; break;
        }
        if(i == L2D_CkPosNo) this.SetPicStrBG("#336699");
        this.SpriteStr(PN_L2DCkPos + i , _SetPsStr , 18 , 727 , 40 + 25 * i);
        L2DCk_PsPicArr.push(PN_L2DCkPos + i);
    }
    L2DCk_HntnPicArr = [PN_L2DCkHntn + 0 , PN_L2DCkHntn + 1];
    if(!L2D_Ck反転Flg) this.SetPicStrBG("#336699");
    this.SpriteStr(PN_L2DCkHntn + 0 , "反転させない" , 18 , 727 , 144 + (25 * 0));
    if(L2D_Ck反転Flg) this.SetPicStrBG("#336699");
    this.SpriteStr(PN_L2DCkHntn + 1 , "反転させる" , 18 , 727 , 144 + (25 * 1));
}
let L2D_CkMdlName = "";
let L2D_CkMtnDrawFlg = false;
let L2D_JsonData = null;    
let L2D_CkMtnPicArr = [];
let L2D_CkExpPicArr = [];
let L2D_CkExpTrackPicArr = [];
let L2D_Ck_MtnNo = -1;
let L2D_Ck_MtnName = "";
let L2D_Ck_ExpNo = -1;
let L2D_Ck_ExpName = [];
for (let i = 0; i <= 10 - 1; i++) L2D_Ck_ExpName.push("");
let L2DCk_CBMap = [
    [0,0,236,810],
    [237,0,236,810]
]
Game_Interpreter.prototype.Live2D_Checker_Update = function () {
    let _CNo = overPointerSpCkArr(L2D_CkPicArr);
    if(_CNo != -1){
        if(L2D_CkSelNo != _CNo + L2D_CkZureY - PN_L2DNameTxt){
            L2D_CkSelNo = _CNo + L2D_CkZureY - PN_L2DNameTxt;
            this.Live2D_Ck_Draw();
        }
        if(TouchInput.isTriggered()){
            SelectExpTrackId = 0;
            for (let i = 0; i <= L2D_Ck_ExpName.length - 1; i++) {
                L2D_Ck_ExpName[i] = "";
            }
            L2D_Ck_ExpNo = -1;
            L2D_CkMode = 1;
            _L2D_MtnZureY = L2D_Ck_MtnNo = 0;
            L2ModelLoadMdl(modelSetting[L2D_CkSelNo]._MdlName);
            L2D_CkMdlName = modelSetting[L2D_CkSelNo]._MdlName;
            L2D_CkMtnDrawFlg = false;
            this.Live2D_Ck_PosDraw();
            for (let i = 0; i <= L2D_ViewSaveArr.length - 1; i++) {
                if(L2D_ViewSaveArr[i]._MdlName == modelSetting[L2D_CkSelNo]._MdlName){
                    for (let j = 0; j <= L2D_JsonData.Motions[""].length - 1; j++) {
                        if(L2D_JsonData.Motions[""][j].Name == L2D_ViewSaveArr[i]._IdleMtn){
                            L2D_Ck_MtnNo = j;
                        }
                    }
                    for (let j = 0; j <= L2D_ViewSaveArr[i]._ExpTrack.length - 1; j++) {
                        if (L2D_ViewSaveArr[i]._ExpTrack[j] != null) {
                            L2D_Ck_ExpName[j] = L2D_ViewSaveArr[i]._ExpTrack[j];
                        }
                    }
                    break;
                }
            }
        }
    }
    let _CBNo = overPicClickable( PN_L2D_CkBase , L2DCk_CBMap);
    if(TouchInput.wheelY < 0){
        if(_CBNo == 0){
            L2D_CkZureY--;
            if(L2D_CkZureY < 0) L2D_CkZureY = 0;
            this.Live2D_Ck_Draw();
        }
        if(_CBNo == 1){
            _L2D_MtnZureY--;
            if(_L2D_MtnZureY < 0) _L2D_MtnZureY = 0;
            this.Live2D_Ck_MtnListDraw();
        }
    }
    if(TouchInput.wheelY > 0){
        if(_CBNo == 0){
            L2D_CkZureY++;
            this.Live2D_Ck_Draw();
        }
        if(_CBNo == 1){
            _L2D_MtnZureY++;
            this.Live2D_Ck_MtnListDraw();
        }
    }
    if(L2D_CkMode == 1){
        if(!L2D_CkMtnDrawFlg){
            L2D_JsonData = Live2D_JsonKeep['live2d data'][L2D_CkMdlName + "/" + L2D_CkMdlName];
            this.Live2D_Ck_MtnDraw();
            L2D_CkMtnDrawFlg = true;
        }
        let _CNo = overPointerSpCkArr(L2D_CkMtnPicArr);
        if(_CNo != -1){
            if(L2D_Ck_MtnNo != _CNo - PN_L2DCkMtnTxt + _L2D_MtnZureY){
                if(TouchInput.isTriggered()){
                    L2D_Ck_MtnNo = _CNo - PN_L2DCkMtnTxt + _L2D_MtnZureY;
                    this.Live2D_Ck_MtnDraw();
                }
            }
        }
        if (overPointerSpCk(PN_L2DMtnLoopTxt) && TouchInput.isTriggered()) {
            L2DCk_LoopFlg = !L2DCk_LoopFlg;
            this.Live2D_Ck_MtnDraw();
        }
        _CNo = overPointerSpCkArr(L2D_CkExpPicArr);
        if(_CNo != -1){
            if(L2D_Ck_ExpNo != _CNo - PN_L2DCkExpTxt){
                if(TouchInput.isTriggered()){
                    L2D_Ck_ExpNo = _CNo - PN_L2DCkExpTxt;
                    this.Live2D_Ck_MtnDraw();
                }
            }
        }
        _CNo = overPointerSpCkArr(L2D_CkExpTrackPicArr);
        if(_CNo != -1){
            if(SelectExpTrackId != _CNo - PN_L2DCkExpTrackTxt){
                if(TouchInput.isTriggered()){
                    SelectExpTrackId = _CNo - PN_L2DCkExpTrackTxt;
                    L2D_Ck_ExpNo =　L2D_CkExpTrack_GetNo(); 
                    this.Live2D_Ck_MtnDraw();
                }
            }
        }
        _CNo = overPointerSpCkArr(L2DCk_PsPicArr);
        if(_CNo != -1){
            if(TouchInput.isTriggered()){
                if(L2D_CkPosNo != _CNo - PN_L2DCkPos){
                    L2D_CkPosNo = _CNo - PN_L2DCkPos;
                }
                this.Live2D_Ck_PosDraw();
                this.Live2D_Ck_MtnDraw();
            } 
        }
        _CNo = overPointerSpCkArr(L2DCk_HntnPicArr);
        if(_CNo != -1){
            if(TouchInput.isTriggered()){
                if(_CNo == PN_L2DCkHntn + 0 ) L2D_Ck反転Flg = false;
                if(_CNo == PN_L2DCkHntn + 1 ) L2D_Ck反転Flg = true;
                this.Live2D_Ck_PosDraw();
                this.Live2D_Ck_MtnDraw();
            }
        }
        _CNo = overPointerSpCkArr(L2DEvBtnArr);
        if(_CNo != -1){
            if(TouchInput.isTriggered()){
                let _EvNo = _CNo - PN_L2DCkEvBtn + 2;
                this.EvStart(_EvNo); 
            }
        }
    }
}
let _L2D_MtnZureY = 0;
Game_Interpreter.prototype.Live2D_Ck_MtnDraw = function () {
    this.Live2D_Ck_MtnListDraw();   
    this.Live2D_Ck_ExpListDraw();     
    L2dAllFadeOut(2);
    let _SetInStr = "";
    switch(L2D_CkPosNo) {
        case 0: _SetInStr = "rightin"; break;
        case 1: _SetInStr = "centerin"; break;
        case 2: _SetInStr = "leftin"; break;
    }
    L2dTop(L2D_CkMdlName);
    let _L2D_XSize = 1;
    if(L2D_Ck反転Flg) _L2D_XSize = -1;
    if(SelectMotionTrackId == 0){
        L2dFadeMdl(L2D_CkMdlName  ,_SetInStr , 15 , L2D_Ck_MtnName , _L2D_XSize);
    }
    if(L2DCk_LoopFlg) {
        this.SpriteStr(PN_L2DMtnLoopTxt , "　■Loop　" , 18 , 366 , 8);
    } else {
        this.SpriteStr(PN_L2DMtnLoopTxt , "　□Loop　" , 18 , 366 , 8);
    }
    if(L2DCk_LoopFlg){
        L2dIdleStart(L2D_CkMdlName , L2D_Ck_MtnName);
    }
    for (let i = 0; i <= L2D_Ck_ExpName.length - 1; i++) {
        if(L2D_Ck_ExpName[i] != ""){
            L2dSetExp2(L2D_CkMdlName, L2D_Ck_ExpName[i] , i);
        }
    }
}
let L2DCk_LoopFlg = false;
Game_Interpreter.prototype.Live2D_Ck_MtnListDraw = function () {
    this.DelSpriteSpan(PN_L2DCkMtnTxt , PN_L2DCkMtnTxt + 59); 
    L2D_CkMtnPicArr = [];
    let _Cnt = 0;
    for (let i = 0; i <= L2D_JsonData.Motions[""].length - 1; i++) {
        let _MtnName = L2D_JsonData.Motions[""][i].Name;
        if(L2D_Ck_MtnNo == i) {
            this.SetPicStrBG("#336699");
            L2D_Ck_MtnName = _MtnName;
        }
        this.SpriteStr(PN_L2DCkMtnTxt + _Cnt , _MtnName , 18 , 250 , 40 + 25 * _Cnt);
        L2D_CkMtnPicArr.push(PN_L2DCkMtnTxt + _Cnt);
        _Cnt++;
        if(_Cnt == 28) break;
    }
}
Game_Interpreter.prototype.Live2D_Ck_ExpListDraw = function () {
    this.DelSpriteSpan(PN_L2DCkExpTxt , PN_L2DCkExpTxt + 59);
    L2D_CkExpPicArr = [];
    if(L2D_JsonData.Expressions == null) return;
    for (let i = 0; i <= L2D_JsonData.Expressions.length - 1; i++) {
        let _ExpName = L2D_JsonData.Expressions[i].Name;
        for (let j = 0; j <= L2D_Ck_ExpName.length - 1; j++) {
            if(j == SelectExpTrackId) continue;
            if(_ExpName == L2D_Ck_ExpName[j]){
                this.SetPicStrBG("#999933");
            }
        }
        if(L2D_Ck_ExpNo == i) {
            this.SetPicStrBG("#336699");
            L2D_Ck_ExpName[SelectExpTrackId] = _ExpName;
        }
        let _ckXZure = Math.floor(i / 28);
        let _ckYPosI = i - (28 * _ckXZure);
        this.SpriteStr(PN_L2DCkExpTxt + i , _ExpName , 18 , 480 + _ckXZure * 50 , 40 + 25 * _ckYPosI);
        L2D_CkExpPicArr.push(PN_L2DCkExpTxt + i);
    }
    this.DelSpriteSpan(PN_L2DCkExpTrackTxt , PN_L2DCkExpTrackTxt + 10);
    L2D_CkExpTrackPicArr = [];
    for (let i = 0; i <= 10 - 1; i++) {
        if(L2D_Ck_ExpName[i] != ""){
            this.SetPicStrBG("#999933");
        }
        if(SelectExpTrackId == i) {
            this.SetPicStrBG("#336699");
        }
        this.SpriteStr(PN_L2DCkExpTrackTxt + i , "0" + i , 18 , 667 , 25 + 25 * i);
        L2D_CkExpTrackPicArr.push(PN_L2DCkExpTrackTxt + i);
    }
}
let L2D_CkExpTrack_GetNo = function (){
    let _SetExpName = L2D_Ck_ExpName[SelectExpTrackId];
    if(_SetExpName == "") return -1;
    for (let i = 0; i <= L2D_JsonData.Expressions.length - 1; i++) {
        if(L2D_JsonData.Expressions[i].Name == _SetExpName) {
            return  i;
        }
    }
}
