/*:ja
 * @plugindesc 
 * @author Nupuryu
 *
 * @help 
 */
const Talk_WinPicNo = 480; 
let Talk_WinSetY = 0; 
let TxtName = "";
let TxtNameSet = function (_name) {
    TxtName = _name;
}
let MobName = "";
let MobNameSet = function (_name) {
    MobName = _name;
}
let btlFaceImg = ""; 
Game_Interpreter.prototype.btlFaceSet = function (_SetImgStr) {
    btlFaceImg = _SetImgStr;
    if (btlFaceImg.indexOf('roma_Face') != -1) {
        switch ($gameVariables.value(VN_TL2DKami)) {
            case 30: 
                btlFaceImg += "_l"
                break;
        }
    }
}
let EnFadeInFlg = false;
Game_Interpreter.prototype.EnFadeIN = function (_PicName, _Opi, _Time, _PicNo) {
    _Opi = typeof _Opi !== 'undefined' ? _Opi : 255;
    _Time = typeof _Time !== 'undefined' ? _Time : 25;
    _PicNo = typeof _PicNo !== 'undefined' ? _PicNo : PNo_Ensyutu;
    if (!EnFadeInFlg) {
        EnFadeInFlg = true;
        this.SetPict(_PicNo, _PicName, 0, 0, 0);
        this.MovePict(_PicNo, 0, 0, _Time, _Opi);
        this.wait(_Time);
    }
}
Game_Interpreter.prototype.EnFadeOut = function (_Time, _PicNo) {
    _Time = typeof _Time !== 'undefined' ? _Time : 25;
    _PicNo = typeof _PicNo !== 'undefined' ? _PicNo : PNo_Ensyutu;
    this.MovePict(_PicNo, 0, 0, _Time, 0);
    EnFadeInFlg = false;
}
let EnDFadeInFlg = false;
Game_Interpreter.prototype.EnDFadeIN = function (_PicName, _Opi, _Time , _PicNo) {
    _Opi = typeof _Opi !== 'undefined' ? _Opi : 255;
    _Time = typeof _Time !== 'undefined' ? _Time : 25;
    _PicNo = typeof _PicNo !== 'undefined' ? _PicNo : PN_EnsyutuD;
    if (!EnDFadeInFlg) {
        this.EnFadeIN(_PicName, _Opi, _Time, _PicNo);
    }
}
Game_Interpreter.prototype.EnDFadeOut = function (_Time , _PicNo) {
    _Time = typeof _Time !== 'undefined' ? _Time : 25;
    _PicNo = typeof _PicNo !== 'undefined' ? _PicNo : PN_EnsyutuD;
    this.EnFadeOut(_Time, _PicNo);
    EnDFadeInFlg = false;
}
let MdlTxtName = ""; 
let AutoIconPos = 2; 
let MobYobiFaceImg = "";
let Nupu_command101 = Game_Interpreter.prototype.command101;
Game_Interpreter.prototype.command101 = function () {
    AutoIconPos = this._params[3];
    if (setBtlTalkDialogData != null) {
        Nupu_command101.call(this); 
    } else {
        let WinWaku = "";
        switch (this._params[3]) { 
            case 0: 
                Talk_WinSetY = 420 - 400;
                break;
            case 1: 
                Talk_WinSetY = 420 - 146;
                break;
            case 2: 
                Talk_WinSetY = 420 + 125;
                break;
        }
        switch (this._params[2]) { 
            case 0: 
                WinWaku = this._params[2];
                if ((battleflg || TalkBtlFlg) && !LastEffectUISelectUpdate) {
                    this.SetPict(Talk_WinPicNo, "system/BTalk_Win", 0, Talk_WinSetY); 
                    if (btlFaceImg == "") {
                        this.SetPict(Talk_WinPicNo, "system/BTalk_Win_Base", 0, Talk_WinSetY); 
                    } else {
                        this.SetPict(Talk_WinPicNo - 1, "pictures/Btl_Face/" + btlFaceImg, 131, Talk_WinSetY + 13); 
                    }
                    DelSpriteSpan(PN_BSelfWin , PN_BSelfTxt + 1);
                } else {
                    this.SetPict(Talk_WinPicNo, "system/Talk_Win", 0, Talk_WinSetY); 
                }
                break;
            case 1: 
                WinWaku = this._params[2];
                if ((battleflg || TalkBtlFlg) && !LastEffectUISelectUpdate) {
                    this.SetPict(Talk_WinPicNo, "system/BTalk_Win2", 0, Talk_WinSetY); 
                } else {
                    this.SetPict(Talk_WinPicNo, "system/Talk_Win_black", 0, Talk_WinSetY - 10); 
                }
                break;
        }
        let expimgT = this._params[0].indexOf('_ExpImg'); 
        let ImgCOunt = Number(this._params[0].slice(-1));
        let YobFaceImg = this._params[0]; 
        if (MobYobiFaceImg != "") {
            YobFaceImg = MobYobiFaceImg;
            MobYobiFaceImg = ""; 
        }
        if (expimgT != -1) {
            let ck_Live2DName = this._params[0].substr(0, expimgT); 
            MdlTxtName = modelSetting_NameGet(ck_Live2DName);
            L2dTalkTop(ck_Live2DName); 
            this.MobAllBack(); 
            this._params[0] = "";
            Exp_No = this._params[1] + 1 + ((ImgCOunt - 1) * 8);
            for (let i = 0; i <= expSetting.length - 1; i++) {
                if (expSetting[i]._MdlName == ck_Live2DName) {
                    if (expSetting[i]._FaceNo == Exp_No) {
                        L2dSetMotion(ck_Live2DName, expSetting[i]._MtnName);
                    }
                }
            }
        } else {
            MdlTxtName = ""; 
        }
        this._params[2] = 2; 
        Nupu_command101.call(this);
        if ($gameMessage._texts.length > 0) {
            if (TxtName != "") { 
                $gameMessage._texts[0] = "\\n<" + TxtName + ">" + $gameMessage._texts[0];
            } else {
                if (YobFaceImg.indexOf("_Tatie") != -1 && MobName != "") {
                    $gameMessage._texts[0] = "\\n<" + MobName + ">" + $gameMessage._texts[0];
                } else {
                    if (MdlTxtName != "") {
                        $gameMessage._texts[0] = "\\n<" + MdlTxtName + ">" + $gameMessage._texts[0];
                    }
                    MdlTxtName = ""; 
                }
            }
        }
        if($gameMessage._texts.length > 0) {
            if ($gameMessage._texts[0].indexOf("\\n<") == 0 || $gameMessage._texts[0].indexOf("\\N<") == 0){
                if ($gameMessage._texts[0].slice(-1) == ">" && $gameMessage._texts.length > 1){ 
                    for (let i = 1; i <= $gameMessage._texts.length - 1; i++){
                        if (i == 1){
                            $gameMessage._texts[i - 1] += $gameMessage._texts[i];
                        } else {
                            $gameMessage._texts[i - 1] = $gameMessage._texts[i];
                        }
                    }
                    $gameMessage._texts.pop(); 
                }
            }
            this._params[0] = YobFaceImg;
            this._params[2] = WinWaku;
        }
    }
};
let Nupu_Msg_close = Window_Message.prototype.close;
Window_Message.prototype.close = function () {
    Nupu_Msg_close.call(this);
    $gameScreen.erasePicture(Talk_WinPicNo); 
    $gameScreen.erasePicture(Talk_WinPicNo - 1); 
};
var Live2D_updatePlacement = Window_Message.prototype.updatePlacement;
Window_Message.prototype.updatePlacement = function () {
    Live2D_updatePlacement.apply(this, arguments);
    this._positionType = $gameMessage.positionType();
    this.y = this._positionType * (Graphics.boxHeight - this.height) / 2;
    this._goldWindow.y = this.y > 0 ? 0 : Graphics.boxHeight - this._goldWindow.height;
    this.x -= 10;
    switch (this._positionType){
        case 0: 
            this.y += 75;
            break;
        case 1: 
            break;
        case 2: 
            this.y -= 55;
            break;
    }
    if ((battleflg || TalkBtlFlg) && !LastEffectUISelectUpdate) { 
        this.x += 210;
    }
};
let Nupu_Msg_adjustPositionY = Window_NameBox.prototype.adjustPositionY;
Window_NameBox.prototype.adjustPositionY = function () {
    Nupu_Msg_adjustPositionY.call(this);
    switch ($gameMessage.positionType()) {
        case 0: 
            this.y -= 210;
            break;
        case 1: 
            this.y -= 0;
            break;
        case 2: 
            this.y -= 0;
            break;
    }
}
let LightMdlSetting = [];
class _L2dLightMdl {
    constructor(_MdlName , _LightMdlName) {
        this._MdlName = _MdlName;
        this._LightMdlName = _LightMdlName;
    }
}
let SettingLightModel = function () {
    LightMdlSetting.forEach( LightMdlData => {
        let BaseMdlName = LightMdlData._MdlName;
        let CngMdlName = LightMdlData._LightMdlName;
        modelSetting.some(_ckModelData => {
            if(_ckModelData._MdlName == BaseMdlName) {
                let _AddFlg = true;
                modelSetting.some(_ckModelData_2 => {
                    if(_ckModelData_2._MdlName == CngMdlName){
                        _ckModelData_2._Name = _ckModelData._Name;           
                        _ckModelData_2._StartLoad = _ckModelData._StartLoad; 
                        _ckModelData_2._ZureX = _ckModelData._ZureX;         
                        _ckModelData_2._ZureY = _ckModelData._ZureY;         
                    }
                    _AddFlg = false;
                    return true;
                });
                if (_AddFlg) {
                    modelSetting.push(
                        new _L2dDtlSet(
                            CngMdlName,
                            _ckModelData._Name,
                            _ckModelData._StartLoad,
                            _ckModelData._ZureX,
                            _ckModelData._ZureY
                        )
                    );
                }
                return true;
            }
        });
        L2D_MosaicDataArr.some(_ckMosData => {
            if(_ckMosData._MdlName == BaseMdlName) {
                let _AddFlg = true;
                L2D_MosaicDataArr.some(_ckMosData_2 => {
                    if(_ckMosData_2._Model == CngMdlName &&
                        _ckMosData_2._ArtMesh == _ckMosData._ArtMesh){
                        _ckMosData_2._MosPx = _ckMosData._MosPx;
                    }
                    _AddFlg = false;
                    return true;
                });
                if (_AddFlg) {
                    L2D_MosaicMdlAdd(CngMdlName , _ckMosData._ArtMesh , _ckMosData._MosPx);
                }
                return true;
            }
        });
        expSetting.some(_ckExpData => {
            if(_ckExpData._MdlName == BaseMdlName) {
                let _AddFlg = true;
                expSetting.some(_ckExpData_2 => {
                    if(_ckExpData_2._MdlName == CngMdlName &&
                        _ckExpData_2._MtnName == _ckExpData._MtnName){
                        _ckExpData_2._FaceNo = _ckExpData._FaceNo;
                        _ckExpData_2._LEyeMax = _ckExpData._LEyeMax;
                        _ckExpData_2._REyeMax = _ckExpData._REyeMax;
                    }
                    _AddFlg = false;
                    return true;
                });
                if (_AddFlg) {
                    expSetting.push(new _L2dExpSet(
                        CngMdlName,
                        _ckExpData._MtnName,
                        _ckExpData._FaceNo,
                        _ckExpData._LEyeMax,
                        _ckExpData._REyeMax
                    ));
                }
                return true;
            }
        });
        seSetting.some(_ckSeData => {
            if(seSetting._MdlName == BaseMdlName) {
                let _AddFlg = true;
                seSetting.some(_ckSeData_2 => {
                    if(_ckSeData_2._MdlName == CngMdlName &&
                        _ckSeData_2._SeName == _ckSeData._SeName){}
                    _AddFlg = false;
                    return true;
                });
                if (_AddFlg) {
                    seSetting.push(new _L2dSeSet(
                        CngMdlName,
                        _ckSeData._SeName
                    ));
                }
                return true;
            }
        });
    });
    for (let i = 0; i <= LightMdlSetting.length - 1; i++) {
        for (let j = 0; j <= modelSetting.length - 1; j++) {
            if (modelSetting[j]._MdlName == LightMdlSetting[i]._MdlName) {
                let _AddFlg = true;
                for (let k = 0; k <= modelSetting.length - 1; k++) {
                    if(modelSetting[k]._MdlName == LightMdlSetting[i]._LightMdlName) {
                        _AddFlg = false;
                        modelSetting[k]._Name = modelSetting[j]._Name;
                        modelSetting[k]._StartLoad = modelSetting[j]._StartLoad;
                        modelSetting[k]._ZureX = modelSetting[j]._ZureX;
                        modelSetting[k]._ZureY = modelSetting[j]._ZureY;
                    }
                }
                if(_AddFlg) {
                    modelSetting.push(
                        new _L2dDtlSet(
                            LightMdlSetting[i]._LightMdlName,
                            modelSetting[j]._Name,
                            modelSetting[j]._StartLoad,
                            modelSetting[j]._ZureX,
                            modelSetting[j]._ZureY
                        )
                    )
                }
                break;
            }
        }
    }
}
let L2D_LightMdlCk = function (_MdlName) {
    if($gameSwitches.value(SW_Live2DLight)) {
        for (let i = 0; i <= LightMdlSetting.length - 1; i++) {
            if(_MdlName == LightMdlSetting[i]._MdlName) {
                return LightMdlSetting[i]._LightMdlName;
            }
        }
    }
    return _MdlName;
}
let LMdl_L2D_MdlNameCus = L2D_MdlNameCus;
L2D_MdlNameCus = function(_MdlName) {
    _MdlName = L2D_LightMdlCk(_MdlName);
    return LMdl_L2D_MdlNameCus.apply(this, arguments); 
}
