using System;
using System.Collections.Generic;
using System.Text;
using Kyojin.Model;
using Yanesdk.Ytl;
using Yanesdk.Draw;
using Yanesdk.Input;
using Yamalib.Draw;
using Yanesdk.System;
using Yamalib.Input;
using Yamalib.Scenario;
using Yamalib.Scenario.YaneScript;

namespace Kyojin.Component.Scenario
{
    public class KyojinScenarioModel : YaneScenarioModel
    {

        #region メンバフィールド

        private UserStateView userStateView;
        private UserInfo userInfo;
        private int pageNo = 1;
        private ThumbnailDraw thumbnailDraw;
        private ThumbnailCreator thumbnailCreator;
        private bool showThumbnailDraw = false;

        #endregion


        /// <summary>
        /// コンストラクタ
        /// </summary>
        public KyojinScenarioModel()
        {
        }

        public ThumbnailCreator ThumbnailCreator
        {
            get { return thumbnailCreator; }
            set { thumbnailCreator = value; }
        }

        public bool IsShowThumbnailDraw
        {
            get { return showThumbnailDraw; }
            set { showThumbnailDraw = value; }
        }

        public ThumbnailDraw ThumbnailDraw
        {
            get { return thumbnailDraw; }
            set { thumbnailDraw = value; }
        }

        /// <summary>
        /// ユーザ情報表示オブジェクト
        /// </summary>
        public UserStateView UserStateView
        {
            get { return userStateView; }
            set { userStateView = value; }
        }

        /// <summary>
        /// ユーザ情報
        /// </summary>
        public UserInfo UserInfo
        {
            get { return userInfo; }
            set { userInfo = value; }
        }

        public int PageNo
        {
            get { return pageNo; }
            set { pageNo = value; }
        }

        /// <summary>
        /// 移動処理
        /// </summary>
        /// <param name="screen"></param>
        /// <returns></returns>
        public override YanesdkResult OnMove(IScreen screen)
        {
            if (null != userStateView)
            {
                userStateView.OnMove(screen);
            }

            return base.OnMove(screen);
        }


        /// <summary>
        /// 次のページを解析する
        /// </summary>
        protected override void UpdateText()
        {
            base.UpdateText();
            // ページ番号を増やす
            PageNo++;

            // 去人では 常に BGM_STOP -> BGM_PLAY というタグになっている
            // これだとクロスフェードが使えない
            Dictionary<int, int> stopPlayCmd = new Dictionary<int, int>();
            List<int> removeTagIndex = new List<int>();
            int i = 0;
            foreach (ICommandHandler cmd in currentPageTags)
            {
                if (cmd as BgmStopCommandHandler != null)
                {
                    stopPlayCmd.Add(cmd.ExecPosition, i);
                }
                if (cmd as BgmPlayCommandHandler != null)
                {
                    if (stopPlayCmd.ContainsKey(cmd.ExecPosition))
                    {
                        removeTagIndex.Add(stopPlayCmd[cmd.ExecPosition]);
                    }
                }
                ++i;
            }
            for (int j = 0; j < removeTagIndex.Count; j++)
            {
                currentPageTags[removeTagIndex[j]] = NullCommandHandler.Instance;
            }

        }


    }

    /// <summary>
    /// 去人シナリオ用のView
    /// </summary>
    class KyojinScenarioView : YaneScenarioView, IDisposable
    {
        #region メンバフィールド

        private IMouseInput mouse;
        private bool showBg = true;
        private bool showText = true;
        private bool showCharacter = true;

        #endregion

        /// <summary>
        /// マウス
        /// </summary>
        public IMouseInput Mouse
        {
            get { return mouse; }
            set { mouse = value; }
        }

        #region IDisposable メンバ

        public void Dispose()
        {
        }

        #endregion

        /// <summary>
        /// 背景を表示するか
        /// </summary>
        public bool IsShowBG
        {
            get { return showBg; }
            set { showBg = value; }
        }
        /// <summary>
        /// テキストを表示するか
        /// </summary>
        public bool IsShowText
        {
            get { return showText; }
            set { showText = value; }
        }
        /// <summary>
        /// キャラクターを表示するか
        /// </summary>
        public bool IsShowCharacter
        {
            get { return showCharacter; }
            set { showCharacter = value; }
        }

        /// <summary>
        /// 描画処理
        /// </summary>
        /// <param name="screen"></param>
        /// <returns></returns>
        public override YanesdkResult OnPaint(IScreen screen)
        {
            KyojinScenarioModel model = Model as KyojinScenarioModel;
            List<IViewLayler> layers = Model.ViewLayler;

            if (IsShowBG)
            {
                OnPaintBG(screen);
                if (null != PostPaintBG)
                {
                    PostPaintBG(screen, this);
                }
            }

            if (IsShowCharacter)
            {
                OnPaintCharacter(screen);
                if (null != PostPaintCharacter)
                {
                    PostPaintCharacter(screen, this);
                }
            }

            // ユーザ情報表示
            model.UserStateView.OnPaint(screen);

            if (IsShowText)
            {
                OnPaintText(screen);
                if (null != PostPaintText)
                {
                    PostPaintText(screen, this);
                }
            }

            if (model.IsShowThumbnailDraw)
            {
                model.ThumbnailDraw.OnMove(screen);
                model.ThumbnailDraw.OnDraw(screen);
            }


            return YanesdkResult.NoError;
        }

    }

}
