/// <summary>
    /// 実際にモードの変更のためのプロセスが進行される関数(to 音楽モード)
    /// </summary>
    /// <param name="viaRemote"></param>
    private void MusicMode_realFunc(bool viaRemote)
    {
        if (PulseMain.instance.Get_PlayStateIsMusic == true)
        {
            return;
        }

        _ModePanel.SetActive(false);
        _MusicPanel.SetActive(true);

        PulseMain.instance.MusicTurnChange(true);

        if (_CurrentMoveState == PTN_STATE.PTN_CUSTOM)
        {
            BackUpCellList();
            for (int i = 0; i < _AdvancedOpCellObjects.Count; ++i)
            {
                Destroy(_AdvancedOpCellObjects[i]);
            }
            _PrevCustomModeOn = true;
        }
        else
        {
            if (_CurrentMoveState != PTN_STATE.PTN_MUSIC &&
                _CurrentMoveState != PTN_STATE.PTN_CUSTOM)
            {
                _PrevNormalState = _CurrentMoveState;
            }

            if (_CurrentMoveState == PTN_STATE.PTN_RANDOM)
            {
                _FavLastIndex = _CellList[_CurrentModeIndex].Mode;
            }
            else
            {
                _FavLastIndex = _CurrentModeIndex;
            }
        }

        _CurrentMoveState = PTN_STATE.PTN_MUSIC;
        ForcedOffCoroutine();
        if (viaRemote == true)
        {
            bluetoothMono.instance.SendEcho();
        }
        else
        {
            if (bluetoothMono.instance.Connect == true)
            {
                byte[] msg = new byte[1010];
                msg[0] = 3;
                msg[1] = 0;

                MsgHandler.instance.GenerateMsg(bluetoothMono.DATA_TYPE.DT_NM_MODE,
                                                bluetoothMono.DATA_ACTION.DA_NO_REQUIRE_ACTION, msg);
            }
        }
    }
    /// <summary>
    /// お気に入りからロードされるデフォルトモード
    /// </summary>
    /// <param name="state">モード</param>
    /// <param name="lastNumber">保存されていたパターンナンバー</param>
    public void SettingModeFromFav(PTN_STATE state, int lastNumber)
    {
        if (_PlayCo != null)
        {
            ForcedOffCoroutine();
        }

        if (PulseMain.instance.Get_PlayStateIsMusic == true)
        {
            //音楽モード時、デフォルトモードに戻す
            NormalMode(true);
        }

        //詳細モード時、デフォルトモードに戻す
        if (_CurrentMoveState == PTN_STATE.PTN_CUSTOM)
        {
            NormalAdvancedToggle();
        }

        //ランダムモードなら別にしてくれるとする
        if (state == PTN_STATE.PTN_RANDOM)
        {
            RandomModeSet(lastNumber);
        }
        else
        {
            NormalActivationStateChange((int)state);
            _CurrentModeIndex = lastNumber;
        }

        Set_ModeDisplayText(_CellList[_CurrentModeIndex].Mode);

        //トグル変更により値が変わらないようにCritical Sectionを指定する
        _RadioCriticalSection = true;

        _NormalPanelToggles[(int)state - 1].isOn = true;
        _NormalPanelToggleGroup.NotifyToggleOn(_NormalPanelToggles[(int)state - 1]);

        _NormalMusicToggles[1].isOn = true;
        _NormalMusicToggleGroup.NotifyToggleOn(_NormalMusicToggles[1]);

        _RadioCriticalSection = false;
    }
    /// <summary>
    /// お気に入りに設定が変わった後、進行中の設定を一時的に保管
    /// </summary>
    /// <param name="state">進行中のモード</param>
    /// <param name="pattern">進行中のパターンナンバー</param>
    public void PrevSettingsAfterFavAccept(int state, int pattern)
    {
        if (state == (int)PTN_STATE.PTN_CUSTOM)
        {
            _PrevCustomModeOn  = true;
            _IsNormalPtnOption = false;
            _FavLastIndex      = 0;
        }
        else
        {
            if ((PTN_STATE)state != PTN_STATE.PTN_MUSIC ||
                (PTN_STATE)state != PTN_STATE.PTN_CUSTOM)
            {
                _PrevNormalState = (PTN_STATE)state;
            }

            _IsNormalPtnOption = true;
            _FavLastIndex      = pattern;
        }
    }
    /// <summary>
    /// デフォルトモードの再生オプションを設定してくれる
    /// </summary>
    /// <param name="state">デフォルトモードのモードindex</param>
    /// <param name="isRemote">関数がリモートコントロールアプリケーション関連の関数で実行されたか</param>
    public void NormalActivationStateChange(int state, bool isRemote)
    {
        if (state == (int)_CurrentMoveState && _CurrentMoveState != PTN_STATE.PTN_RANDOM)
        {
            return;
        }

        ForcedOffCoroutine();

        int rndIndex = 0;
        {
            //Rnd配置後に++になってしまうと、既存の私の再生していたIndexを見つけなくなってしまうので
            //あらかじめIndexを探してくれるようである
            if (_RndToggleInput == true && (PTN_STATE)state == PTN_STATE.PTN_STOP)
            {
                if (_CurrentPlayMode <= 0)
                {
                    int temp = 0;
                    if (_Initialize == false)
                    {
                        if (_CurrentModeIndex == 0)
                        {
                            temp = _CellList.Count - 1;
                        }
                        else
                        {
                            temp = _CurrentModeIndex - 1;
                        }

                        rndIndex = _CellList[temp].Mode;
                    }
                    else
                    {
                        rndIndex = _CellList[0].Mode;
                    }
                }
                else
                {
                    rndIndex = _CurrentPlayMode;
                }
            }
        }
        bool comefromAuto3 = false;
        bool comefromAuto4 = false;
        bool comefromAuto5 = false;
        {
            //PTN_STOPの場合に適用してくれるもの
            //もしPTN_AUTO3から来た場合には、従来のIndexに10をよりヘジュオヤする
            if (_CurrentMoveState == PTN_STATE.PTN_AUTO_3)
            {
                comefromAuto3 = true;
            }
            else if (_CurrentMoveState == PTN_STATE.PTN_AUTO_4) //21から開始
            {
                comefromAuto4 = true;
            }
            else if (_CurrentMoveState == PTN_STATE.PTN_AUTO_5) //31から開始
            {
                comefromAuto5 = true;
            }
        }

        int tempIndex = 0;

        if (_CurrentMoveState != PTN_STATE.PTN_STOP &&
            (PTN_STATE)state == PTN_STATE.PTN_STOP)
        {
            tempIndex = _CurrentModeIndex;
        }

        _CurrentMoveState = (PTN_STATE)state;

        ListSetting();

        if (tempIndex > 0)
        {
            _CurrentModeIndex = tempIndex;
        }

        if (_CurrentMoveState == PTN_STATE.PTN_STOP)
        {
            if (_RndToggleInput == true)
            {
                _CurrentModeIndex = rndIndex - 1;
                _RndToggleInput   = false;
            }

            if (comefromAuto3 == true)
            {
                _CurrentModeIndex += 10;
            }

            if (comefromAuto4 == true)
            {
                _CurrentModeIndex += 20;
            }

            if (comefromAuto5 == true)
            {
                _CurrentModeIndex += 30;
            }
        }

        Set_ModeDisplayText(_CellList[_CurrentModeIndex].Mode);

        if (isRemote == false)
        {
            byte[] msg = new byte[1010];
            msg[0] = (byte)_CurrentMoveState;
            msg[1] = (byte)_CellList[_CurrentModeIndex].Mode;

            MsgHandler.instance.GenerateMsg(bluetoothMono.DATA_TYPE.DT_MODE_TOGGLE, bluetoothMono.DATA_ACTION.DA_NO_REQUIRE_ACTION, msg);
        }
        else
        {
            SendPtrmsg(_CellList[_CurrentModeIndex].Mode);
        }

        if (_PlayPatternNow == false)
        {
        }
        else
        {
            PlayCell();
        }
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="FirstNumber"></param>
    public void RandomModeSet(int FirstNumber = 0)
    {
        if (FirstNumber == 0)
        {
            if (_CurrentMoveState != PTN_STATE.PTN_RANDOM)
            {
                return;
            }
        }
        else
        {
            //First Number가 0이 넘어서 들어오는 경우는 반드시 Random모드로 들어오도록 한다
            _CurrentMoveState = PTN_STATE.PTN_RANDOM;
            _FavLastIndex     = -1;
        }

        //TextOutLogMgr.Instance.LogError("RandomModeSet");
        _CellList.Clear();

        List <int> tempList = new List <int>();

        for (int i = 1; i <= PulseMain.instance.MaxPatternLv; ++i)
        {
            tempList.Add(i);
        }

        int      iSet        = 0;
        int      iStartPoint = 1;
        PTN_CELL ptn;

        if (FirstNumber != 0)
        {
            //TextOutLogMgr.Instance.LogError("FirstNumber : " + FirstNumber);
            tempList.RemoveAt(FirstNumber - 1);
            ptn = new PTN_CELL();
            ptn.SetCell(FirstNumber, _NormalStatePatternTick);
            _CellList.Add(ptn);
            ++iStartPoint;
        }
        else if (_FavLastIndex > 0)
        {
            tempList.RemoveAt(_FavLastIndex - 1);
            ptn = new PTN_CELL();
            ptn.SetCell(_FavLastIndex, _NormalStatePatternTick);
            _CellList.Add(ptn);
            ++iStartPoint;

            _FavLastIndex = -1;
        }

        for (int i = iStartPoint; i <= PulseMain.instance.MaxPatternLv; ++i)
        {
            iSet = UnityEngine.Random.Range(0, tempList.Count);
            ptn  = new PTN_CELL();
            ptn.SetCell(tempList[iSet], _NormalStatePatternTick);

            _CellList.Add(ptn);
            tempList.RemoveAt(iSet);
        }

        _CurrentModeIndex = 0;
    }
    /// <summary>
    /// 指定されたデフォルトのモードを設定してくれる関数
    /// </summary>
    /// <param name="setState">変更されるデフォルトのモード</param>
    /// <param name="LastPatternNumber">最後に再生されたパターンナンバー</param>
    public void AutoModeSet(PTN_STATE setState, int LastPatternNumber = 0)
    {
        if (setState == PTN_STATE.PTN_RANDOM ||
            setState == PTN_STATE.PTN_CUSTOM)
        {
            return;
        }

        _CellList.Clear();
        int iStart = 0;
        int iNum   = 0;

        switch (setState)
        {
        case PTN_STATE.PTN_AUTO_1:
            iStart = 1;
            iNum   = 40;
            break;

        case PTN_STATE.PTN_AUTO_2:
            iStart = 1;
            iNum   = 10;
            break;

        case PTN_STATE.PTN_AUTO_3:
            iStart = 11;
            iNum   = 10;
            break;

        case PTN_STATE.PTN_AUTO_4:
            iStart = 21;
            iNum   = 10;
            break;

        case PTN_STATE.PTN_AUTO_5:
            iStart = 31;
            iNum   = 10;
            break;
        }

        PTN_CELL ptn;

        for (int i = 0; i < iNum; ++i)
        {
            ptn = new PTN_CELL();
            ptn.SetCell(iStart, _NormalStatePatternTick);
            _CellList.Add(ptn);
            ++iStart;
        }

        if (LastPatternNumber > 0)
        {
            _CurrentModeIndex = LastPatternNumber;
        }
        else
        {
            if (_FavLastIndex > 0)
            {
                _CurrentModeIndex = _FavLastIndex;
                _FavLastIndex     = -1;
            }
            else
            {
                _CurrentModeIndex = 0;
            }
        }
    }
    /// <summary>
    /// 実際にモードの変更のためのプロセスが進行される関数(to デフォルト/詳細モード)
    /// </summary>
    /// <param name="fromFav"></param>
    private void NormalMode_realFunc(bool fromFav)
    {
        if (PulseMain.instance.Get_PlayStateIsMusic == false)
        {
            return;
        }

        _ModePanel.SetActive(true);
        _MusicPanel.SetActive(false);

        PulseMain.instance.MusicTurnChange(false);

        if (fromFav == false)
        {
            if (_PrevCustomModeOn == false)
            {
                _CurrentMoveState = _PrevNormalState;
            }
            else
            {
                _CurrentMoveState = PTN_STATE.PTN_CUSTOM;
                _PrevCustomModeOn = false;
            }
        }

        if (_CurrentMoveState == PTN_STATE.PTN_CUSTOM)
        {
            if (_IsNormalPtnOption == true)
            {
                NormalAdvancedToggle();
            }
            if (fromFav == false)
            {
                if (_AdvancedCellBackUp.Count >= 1)
                {
                    LoadBackUp();
                    AdvancedListLoad();
                }
                else
                {
                    AddCell();
                }
            }
        }
        else
        {
            if (_IsNormalPtnOption == false)
            {
                if (_CurrentMoveState != PTN_STATE.PTN_MUSIC &&
                    _CurrentMoveState != PTN_STATE.PTN_CUSTOM)
                {
                    _PrevNormalState = _CurrentMoveState;
                }
                NormalAdvancedToggle();
            }
            ListSetting();
            if (_CurrentMoveState <= PTN_STATE.PTN_STOP && _CurrentMoveState >= PTN_STATE.PTN_AUTO_1)
            {
                _NormalPanelToggles[(int)_CurrentMoveState - 1].isOn = true;
                _NormalPanelToggleGroup.NotifyToggleOn(_NormalPanelToggles[(int)_CurrentMoveState - 1]);
            }
        }

        if (_CellList.Count > 0)
        {
            Set_ModeDisplayText(_CellList[_CurrentModeIndex].Mode);
        }
        else
        {
            Set_ModeDisplayText(1);
        }

        if (bluetoothMono.instance.Connect == true)
        {
            //本来なら動作中に、別々に吹き飛ばしてくれるのが原則だが、
            //どうせ飛んで行くことがメッセージであるため、のように入れて送ってくれる
            byte[] msg = new byte[1010];
            if (_CurrentMoveState != PTN_STATE.PTN_MUSIC)
            {
                msg[0] = (byte)(_IsNormalPtnOption == true ? 1 : 2);
                msg[1] = (byte)(_IsNormalPtnOption == true ? _CurrentMoveState : 0);
                if (_CellList.Count > 0)
                {
                    Debug.LogError(_CurrentModeIndex);
                    msg[2] = (byte)_CellList[_CurrentModeIndex].Mode;
                }
                else
                {
                    msg[2] = 1;     //これFavから飛んでくる場合には、時々適用だめ飛ん行く場合がありますので、このようにしてくれる
                }
            }
            else
            {
                msg[0] = 3;
                msg[1] = 0;
                msg[2] = 0;
            }

            MsgHandler.instance.GenerateMsg(bluetoothMono.DATA_TYPE.DT_NM_MODE,
                                            bluetoothMono.DATA_ACTION.DA_NO_REQUIRE_ACTION, msg);
        }
    }
    /// <summary>
    /// 基本モードと詳細モードを切り替えてくれる関数
    /// </summary>
    /// <param name="viaRemote">リモコンアプリケーションから来たかどうか</param>
    public void NormalAdvancedToggle(bool viaRemote = false)
    {
        if (PulseMain.instance.Get_PlayStateIsMusic == true)
        {
            return;
        }

        if (_PlayPatternNow == true)
        {
            PulseMain.instance.TurnOff();
        }

        if (_IsNormalPtnOption == true)
        {
            _NormalPanel.SetActive(false);
            _AdvancedPanel.SetActive(true);

            _OptionToggleBtnLabel.text       = FontSystem.instance.DataTransfer(FontSystem.FONT_ID.FID_BT_NOROPTION);
            _OptionToggleBtnImage.localScale = new Vector3(-1f, 1f, 1f);
            _IsNormalPtnOption = false;

            if (_CurrentMoveState != PTN_STATE.PTN_MUSIC &&
                _CurrentMoveState != PTN_STATE.PTN_CUSTOM)
            {
                _PrevNormalState = _CurrentMoveState;
            }

            if (_CurrentMoveState == PTN_STATE.PTN_STOP)
            {
                _FavLastIndex = _CurrentModeIndex;
            }
            else if (_CurrentMoveState == PTN_STATE.PTN_RANDOM)
            {
                _FavLastIndex = _CellList[_CurrentModeIndex].Mode;
            }
            else
            {
                _FavLastIndex = _CurrentModeIndex;
            }

            _CurrentMoveState = PTN_STATE.PTN_CUSTOM;

            //Clear後CustomLoadがあることを確認した後、ある場合引き渡すようしよう
            if (_CellList.Count > 0)
            {
                _CellList.Clear();
            }


            if (_AdvancedCellBackUp.Count > 0)
            {
                //バックアップしたものを読み込む
                LoadBackUp();
                AdvancedListLoad();
                _AdvancedCellBackUp.Clear();        //バグの原因になることもあり、何があるかわからないので、一度呼んで来たバックアップセルは飛ばしてくれる
            }
            else
            {
                //1つ新しく作成入れる
                AddCell();

                //1番目のセルのモードで初期化させてくれる
                _CurrentModeIndex = 0;
                Set_ModeDisplayText(_CellList[_CurrentModeIndex].Mode);
            }
        }
        else
        {
            _NormalPanel.SetActive(true);
            _AdvancedPanel.SetActive(false);

            _OptionToggleBtnLabel.text       = FontSystem.instance.DataTransfer(FontSystem.FONT_ID.FID_BT_ADVOPTION);
            _OptionToggleBtnImage.localScale = new Vector3(1f, 1f, 1f);
            _IsNormalPtnOption = true;

            _CurrentMoveState = _PrevNormalState;

            for (int i = 0; i < _AdvancedOpCellObjects.Count; ++i)
            {
                Destroy(_AdvancedOpCellObjects[i]);
            }

            _AdvancedOpCellObjects.Clear();
            _Content.GetComponent <RectTransform>().sizeDelta = new Vector2(0f, 350f);
            _CellAddButton.transform.localPosition            = new Vector3(135, -21);

            if (_CellList.Count > 0)
            {
                //詳細モードセルのバックアップ
                BackUpCellList();
            }

            ListSetting();

            _RadioCriticalSection = true;
            _NormalPanelToggles[(int)_CurrentMoveState - 1].isOn = true;
            _NormalPanelToggleGroup.NotifyToggleOn(_NormalPanelToggles[(int)_CurrentMoveState - 1]);
            _RadioCriticalSection = false;
        }

        Set_ModeDisplayText(_CellList[_CurrentModeIndex].Mode);

        if (bluetoothMono.instance.Connect == true)
        {
            byte[] msg = new byte[1010];
            msg[0] = (byte)(_IsNormalPtnOption == true ? 1 : 2);
            msg[1] = (byte)((int)_CurrentMoveState);
            msg[2] = (byte)_CellList[_CurrentModeIndex].Mode;

            MsgHandler.instance.GenerateMsg(bluetoothMono.DATA_TYPE.DT_NA_MODE, bluetoothMono.DATA_ACTION.DA_NO_REQUIRE_ACTION, msg);
        }
    }