/* 汎用的に使いそうな処理
_PLステート確認(stateNo)
 */
class BtlStateData {
    constructor(_No, _Turn) {
        this._No = _No;     
        this._Turn = _Turn; 
    }
}
//■ステート更新処理：_ENo(~-1:プレイヤー側 , 0~:エネミー)■■■■■■■■■■■■■■■■■■■■■■■■■
let StateUpdateFlame = 0;
let CheckStateData; 
Game_Interpreter.prototype.StateUpdate = function (ckENo) {
    let stateData; 
    let stateName; 
    let animePosX; 
    let animePosY;
    if (ckENo <= -1) {
        stateData = NPlayer[(ckENo * -1) - 1]._NState;
        stateName = NPlayer[(ckENo * -1) - 1]._name;
        animePosX = NPlayer[(ckENo * -1) - 1]._screenX;
        animePosY = NPlayer[(ckENo * -1) - 1]._screenY;
    } else {
        stateData = NEnemy[ckENo]._NState;
        stateName = NEnemy[ckENo]._name;
        animePosX = NEnemy[ckENo]._screenX;
        animePosY = NEnemy[ckENo]._screenY;
    }
    if (StateSyoriNo >= stateData.length) {
        let outNState = [];
        for (let i = 0; i <= stateData.length - 1; i++) {
            if (stateData[i]._No == StOneType.Defense) {
                //stateData[i]._Turn = Math.floor(stateData[i]._Turn / 2); //防御値は半分にする
                stateData[i]._Turn = 0;
                continue;
            }
            stateData[i]._Turn--;
            if (stateData[i]._No == StOneType.CelDmg) {
                stateData[i]._Turn++;
            }
            if (stateData[i]._No == StOneType.SyaseiYoku) {
                $gameVariables.setValue(VN_射精欲, stateData[i]._Turn)
            }
            if (stateData[i]._Turn > 0) {
                outNState.push(stateData[i]);
            } else {
                let message4 = $dataStates[stateData[i]._No].message4;
                message4 = this.StateMessageReplace(message4, stateName, stateData[i]._No);
                if (message4 != "") {
                    this.BLogAdd(120, message4);
                }
                if (ckENo == -1) {
                    $gameActors.actor(1).removeState(stateData[i]._No);
                    switch (stateData[i]._No) {
                        case STNo_石化: 
                        case STNo_メタル化: 
                        case STNo_丸呑み: 
                            this.BtlLive2Dパラメーター更新();
                            if (stateData[i]._No == STNo_メタル化 || stateData[i]._No == STNo_石化) { 
                                this.SetSpineAnime(PN_CharacterPL, "Idl", true);
                            }
                            break;
                    }
                }
            }
        }
        Turn0StateRemove_ENo(ckENo, outNState);
        StateSyoriEnd = true;
        return; 
    }
    if (StateUpdateFlame == 1) {
        if (stateData[StateSyoriNo]._No == StOneType.CelDmg) {
            StateSyoriNo++;
            StateEffectFlame = 0; 
            StateUpdateFlame = 0; 
            return; 
        }
        this.StateEffectSet(ckENo); 
    }
    if (StateUpdateFlame >= 2) {
        this.StateDataCng(ckENo);
    }
    StateUpdateFlame++;
}
//CheckNStateInState(NPlayer[0]._NState, _StateId)
Game_Interpreter.prototype.IsPlayerメタル化 = function () {
    return CheckNStateInState(NPlayer[0]._NState, STNo_メタル化);
}
Game_Interpreter.prototype.StateEffectSet = function (ckENo) {
    if (ckENo <= -1) {
        stateData = NPlayer[(ckENo * -1) - 1]._NState;
        stateName = NPlayer[(ckENo * -1) - 1]._name;
        animePosX = NPlayer[(ckENo * -1) - 1]._screenX;
        animePosY = NPlayer[(ckENo * -1) - 1]._screenY;
    } else {
        stateData = NEnemy[ckENo]._NState;
        stateName = NEnemy[ckENo]._name;
        animePosX = NEnemy[ckENo]._screenX;
        animePosY = NEnemy[ckENo]._screenY;
    }
    CheckStateData = $dataStates[stateData[StateSyoriNo]._No];
    CheckStateData.turn = stateData[StateSyoriNo]._Turn;
    let tAnimeNo = MetaChecker(CheckStateData , "TAnime" , 0);
    tAnimeNo = Number(tAnimeNo);
    if (tAnimeNo != 0) { 
        this.PlayAnime(tAnimeNo, animePosX, animePosY);
    }
    let PtcAnime = MetaChecker(CheckStateData , "Ptc" , "");
    if (PtcAnime != "") {
        let ckArr = PtcAnime.split(',');
        let ptcName = ckArr[0];
        if (ckArr.length > 1) { 
            animePosX += Number(ckArr[1]);
        }
        if (ckArr.length > 2) { 
            animePosY += Number(ckArr[2]);
        }
        this.PlayPtcPic(ptcName , animePosX , animePosY);
    }
    let message3 = $dataStates[stateData[StateSyoriNo]._No].message3; 
    if (message3 != "") {
        message3 = this.StateMessageReplace(message3, stateName, stateData[StateSyoriNo]._No)
        this.BLogAdd(120, message3); 
    }
}
let StateEffectFlame = 0;
let StateSyoriNo = 0;
Game_Interpreter.prototype.StateDataCng = function (ENo) {
    let stateEffectFlame = Number(CheckStateData.meta["EfTime"]); 
    if (stateEffectFlame == "") stateEffectFlame = 0;
    if (StateEffectFlame == 0) { 
        for (let i = 0; i <= CheckStateData.traits.length - 1; i++) {
            if (CheckStateData.traits[i].code != 11) {
                continue; 
            }
            let stateDamage = 0;
            switch (CheckStateData.traits[i].dataId) {
                case StType.TurnDmg: 
                    stateDamage = Math.round(CheckStateData.traits[i].value * 100 - 100);
                    if (stateDamage == 677) stateDamage = -200; 
                    if (stateDamage > 0) {
                        SetEnemyDmgStac(DType.Dmg, ENo, 15, stateDamage);     
                    } else {
                        SetEnemyDmgStac(DType.HpRecv, ENo, 15, -stateDamage);  
                    }
                    break;
                case StType.CelDmg:  
                    //     stateDamage = Math.round(CheckStateData.turn);
                    //     if (stateDamage > 0) {
                    //         SetEnemyDmgStac(DType.Dmg, ENo, 15, stateDamage);     //ダメージ
                    //         SetEnemyDmgStac(DType.HpRecv, ENo, 15, -stateDamage);  //マイナスの場合回復
                    break;
            }
        }
    }
    StateEffectFlame++; 
    if (StateEffectFlame <= stateEffectFlame) {
    } else {
        StateSyoriNo++;
        StateEffectFlame = 0; 
        StateUpdateFlame = 0; 
    }
}
Game_Interpreter.prototype.StateMessageReplace = function (msgStr, cName, stateNo, stateTurnCount) { 
    stateTurnCount = typeof stateTurnCount !== 'undefined' ? stateTurnCount : 0;
    if (msgStr == null) {
        return "";
    }
    msgStr = msgStr.replace("<C>", cName);
    msgStr = msgStr.replace("<B>", stateTurnCount);
    msgStr = msgStr.replace("<I>", "\\I[" + $dataStates[stateNo].iconIndex + "]");
    let ckValue = 0;
    if ($dataStates[stateNo].traits.length >= 0) {
        for (let i = 0; i <= $dataStates[stateNo].traits.length - 1; i++) {
            if ($dataStates[stateNo].traits[i].code == 11) {
                ckValue = Math.round(($dataStates[stateNo].traits[i].value - 1) * 100);
            }
        }
    }
    if (ckValue < 0) {
        ckValue *= -1;
    }
    msgStr = msgStr.replace("<var>", Math.floor(ckValue));
    msgStr = msgStr.replace("<bairitu>", Math.floor(ckValue + 100) / 100);
    return msgStr;
}
let Turn0StateRemove_ENo = function (setENo, setNState) {
    let outArr = [];
    for (let i = 0; i <= setNState.length - 1; i++) {
        if (setNState[i]._Turn > 0) {
            outArr.push(setNState[i]);
        }
    }
    if (setENo <= -1) { 
        NPlayer[-setENo - 1]._NState = outArr;
    } else {
        NEnemy[setENo]._NState = outArr;
    }
}
let sateAddPlayerFlg = false;
let AddNState = function (setNState, setStateNo, setTurn) {
    if (sateAddPlayerFlg) { 
        sateAddPlayerFlg = false;
        if (CheckDeckInWakutin(setStateNo)) return; 
    }
    setTurn = ChangeNStateTurn(setNState, setStateNo, setTurn);
    let newstflg = true;
    let _重複Id = MetaChecker($dataStates[setStateNo], "重複ID", "-1")
    if (_重複Id != -1) {
        for (let i = 0; i <= setNState.length - 1; i++) {
            if (setNState[i]._No != setStateNo) {
                let _ck重複ID = MetaChecker($dataStates[setNState[i]._No], "重複ID", "-1");
                if (_ck重複ID == "-1") continue;
                if (_ck重複ID == _重複Id) { 
                    if ($dataStates[setStateNo].priority < $dataStates[setNState[i]._No].priority) {
                        return;
                    }
                    setNState.splice(i, 1);
                    i--; 
                }
            }
        }
    }
    for (let i = 0; i <= setNState.length - 1; i++) {
        if (setNState[i]._No == setStateNo) { 
            if (setStateNo == StOneType.Defense) {
                setNState[i]._Turn += setTurn; 
            } else {
                if (!MetaChecker($dataStates[setStateNo], "AddTurn", false)) {
                    if (setNState[i]._Turn < setTurn) {
                        setNState[i]._Turn = setTurn;
                    }
                } else {
                    setNState[i]._Turn += setTurn;
                }
            }
            newstflg = false;
        }
    }
    if (newstflg) {
        if (setStateNo == StOneType.Defense) {
            setNState.unshift(new BtlStateData(setStateNo, setTurn)); 
        } else {
            if (setStateNo == StOneType.CelDmg) {
                let insertIdx = 0;
                for (let i = 0; i <= setNState.length - 1; i++) {
                    if (setNState[i]._No == StOneType.Defense) {
                        insertIdx = i + 1;
                    }
                }
                setNState.splice(insertIdx, 0, new BtlStateData(setStateNo, setTurn));
                return;
            }
            setNState.push(new BtlStateData(setStateNo, setTurn));
        }
    }
}
let ChangeNStateTurn = function (NState, setStateNo, SetTurn) {
    switch (setStateNo) {
        case StOneType.SyaseiYoku:
            if (CheckStateInType(NState, StType.Koufun)) {
                SetTurn *= 2;
            }
            break;
    }
    return SetTurn;
}
let HandStateCng = function () {
    En_StateCardArr.forEach(handCard => {
        let getFlg = false;
        CardData.forEach(ckCardData => {
            if (ckCardData.CardData.id == handCard.CardNo) getFlg = true;
        });
        if (getFlg) { 
            $gameActors.actor(1).addState_N(handCard.StateNo);
        } else { 
            $gameActors.actor(1).removeState(handCard.StateNo);
        }
    });
}
let _PLステート解除 = function (stateNo) {
    for (let i = 0; i <= NPlayer[0]._NState.length - 1; i++) {
        if (NPlayer[0]._NState[i]._No == stateNo) {
            NPlayer[0]._NState.splice(i, 1);
            i--;
        }
    }
}
let _ステート全員付与 = function (stateNo) {
    $gameParty.members().forEach(function (actor) {
        actor.addState(stateNo);
    });
}
let _ステート全員解除 = function (stateNo) {
    $gameParty.members().forEach(function (actor) {
        actor.removeState(stateNo);
    });
}
let _PLステート確認 = function (stateNo) { 
    let rtnBool = $gameActors.actor(1).isStateAffected(stateNo);
    if (!rtnBool) {
        for (let i = 0; i <= NPlayer[0]._NState.length - 1; i++) {
            if (NPlayer[0]._NState[i]._No == stateNo) return true;
        }
    }
    return rtnBool;
}
let _手札ステート付与 = function () {
    for (let i = 0; i <= En_StCardArr.length - 1; i++) {
        let addStateFlg = false;
        let ckCardArr = En_StCardArr[i]._CardNoArr;
        for (let j = 0; j <= ckCardArr.length - 1; j++) {
            if (_手札カード存在確認(ckCardArr[j]) > 0) {
                _ステート全員付与(En_StCardArr[i]._StNo);
                addStateFlg = true;
                break;
            }
        }
        if (!addStateFlg)
            _ステート全員解除(En_StCardArr[i]._StNo);
    }
}
let _St_攻撃解除Flg; 
let StateDelCheck_ENo = function (ENo) {
    let ckNState;
    if (ENo <= -1) ckNState = NPlayer[-ENo - 1]._NState;
    if (ENo > -1) ckNState = NEnemy[ENo]._NState;
    for (let i = 0; i <= ckNState.length - 1; i++) {
        if ($dataStates[ckNState[i]._No].meta["攻撃解除"] && _St_攻撃解除Flg) {
            ckNState.splice(i, 1);
            i--;
        }
    }
    _St_攻撃解除Flg = false;
}
let GetStateSetu = function (zokuseiId, turn, value, stateData) {
    stateData = typeof stateData !== 'undefined' ? stateData : null;
    for (let i = 0; i <= ZokuData.length - 1; i++) {
        if (ZokuData[i]._id == zokuseiId) {
            let rtnStr = "";
            if (value >= 0) {
                rtnStr = ZokuData[i]._setu;
            } else {
                value *= -1;
                rtnStr = ZokuData[i]._msetu;
            }
            rtnStr = rtnStr.replace("<var>", Math.floor(value));
            rtnStr = rtnStr.replace("<bairitu>", Math.floor(value + 100) / 100);
            rtnStr = rtnStr.replace("<tn>", Math.floor(turn));
            if (stateData != null) {
                let CNo = stateData.stepsToRemove;
                if (CNo != 0) { 
                    let _CardName = $dataWeapons[CNo].name;
                    rtnStr = rtnStr.replace("<C>", _CardName);
                }
            }
            return rtnStr;
        }
    }
}
Game_Interpreter.prototype.AddState_ENo = function (setENo, stateId, setTurn) {
    if (setENo <= -1) {
        sateAddPlayerFlg = (setENo * -1) - 1 == 0;
        AddNState(NPlayer[(setENo * -1) - 1]._NState, stateId, setTurn);
        if ((setENo * -1) - 1 == 0) {
            $gameActors.actor(1).addState(stateId); 
        }
    } else {
        AddNState(NEnemy[setENo]._NState, stateId, setTurn);
    }
}
Game_Interpreter.prototype.RemoveState_ENo = function (ckENo, stateId) {
    let _CkNState = null;
    if (ckENo <= -1) {
        _CkNState = NPlayer[(ckENo * -1) - 1]._NState;
    } else {
        _CkNState = NEnemy[ckENo]._NState;
    }
    for (let si = 0; si <= _CkNState.length - 1; si++) {
        if (_CkNState[si]._No == stateId) {
            _CkNState.splice(si, 1);
            si--;
            if ((ckENo * -1) - 1 == 0) {
                $gameActors.actor(1).removeState(stateId);
            }
        }
    }
}
Game_Interpreter.prototype.Playerアクセスステート解除 = function () {
    let _CkNState = null;
    _CkNState = NPlayer[0]._NState; 
    for (let i = 0; i <= _CkNState.length - 1; i++) {
        if ($dataStates[_CkNState[i]._No].meta["アクセス"]) {
            _CkNState.splice(i, 1);
            i--;
        }
    }
}
Game_Interpreter.prototype.ClearBadStateAll_Chara = function (ckNState) {
    for (let i = 0; i <= ckNState.length - 1; i++) {
        if (!$dataStates[ckNState[i]._No].meta["有利"] && !$dataStates[ckNState[i]._No].meta["アクセス時解除不可"]) {
            ckNState.splice(i, 1);
            i--;
        }
    }
}
let CngStateTurn_ENo = function (ENo, stateNo, cngTurn) {
    let ckNState;
    if (ENo <= -1) ckNState = NPlayer[-ENo - 1]._NState;
    if (ENo > -1) ckNState = NEnemy[ENo]._NState;
    for (let i = 0; i <= ckNState.length - 1; i++) {
        if (ckNState[i]._No == stateNo) {
            ckNState[i]._Turn += cngTurn;
            if (ckNState[i]._Turn <= 0) {
                ckNState.splice(i, 1);
                break;
            }
        }
    }
    if (ENo <= -1) NPlayer[(ENo * -1) - 1]._NState = ckNState;
    if (ENo > -1) NEnemy[ENo]._NState = ckNState;
}
Game_Battler.prototype.addState_N = function (stateId) {
    if (!this.isStateAffected(stateId)) {
        if (!this.isStateAffected(stateId)) {
            this.addNewState(stateId);
            this.refresh();
        }
        this.resetStateCounts(stateId);
        this._result.pushAddedState(stateId);
    }
};
