using System;
using System.Collections.Generic;
using System.Text;
using Yanesdk.Draw;
using Yamalib.Util;

namespace Yamalib.Draw.Effect
{
    /// <summary>
    /// テクスチャのテキストを段落順に表示していきます
    /// </summary>
    public class TexutralTextDraw
    {
        #region フィールドメンバ

        private ITexture texture;
        private int tx;	// テクスチャサイズ
        private int ty;
        private int x;
        private int y;
        private int drawedLine;
        private bool drawEnd;
        private readonly RootCounter counter = new RootCounter();
        private int lineHeight;
        private readonly Rect drawRc = new Rect();

        #endregion

        /// <summary>
        /// 表示するテクスチャの設定
        /// </summary>
        public ITexture Texture
        {
            get { return texture; }
            set
            {
                texture = value;
                // テクスチャのサイズを保存
                tx = (int)texture.Width;
                ty = (int)texture.Height;
                // カウンターセット
                counter.Set(0, tx, 1);
            }
        }

        /// <summary>
        /// テキストの一行の高さを設定します
        /// </summary>
        public int LineHeight
        {
            get { return lineHeight; }
            set { lineHeight = value; }
        }

        /// <summary>
        /// 終了したか
        /// </summary>
        public bool IsFinish
        {
            get { return drawEnd; }
        }

        /// <summary>
        /// 描画位置の設定
        /// </summary>
        /// <param name="x_"></param>
        /// <param name="y_"></param>
        public void SetXY(int x_, int y_)
        {
            x = x_;
            y = y_;
        }

        /// <summary>
        /// 描画位置の取得
        /// </summary>
        /// <param name="x_"></param>
        /// <param name="y_"></param>
        public void GetXY(out int x_, out int y_)
        {
            x_ = x;
            y_ = y;
        }

        /// <summary>
        /// 表示速度の設定
        /// </summary>
        /// <param name="n"></param>
        public void SetStep(int n)
        {
            counter.StepValue = n;
        }


        /// <summary>
        /// リセット
        /// </summary>
        public void Reset()
        {
            drawEnd = false;
            drawedLine = 0;
        }

        /// 毎回呼び出すなり
        public void OnMove(IScreen screen)
        {
            if (texture == null || drawEnd) return;

            counter.incS();

            if (counter.IsLapAround)
            {
                ++drawedLine;
                // 描画は終了したか？			
                if (drawedLine * lineHeight > ty)
                {
                    drawEnd = true;
                }
                counter.Reset();
            }
        }

        /// 毎回呼び出すなり
        public void OnDraw(IScreen screen)
        {
            // テクスチャセットしてけれ
            if (texture == null) return;


            // 描画
            if (drawedLine > 0)
            {
                drawRc.Left = 0;
                drawRc.Right = tx;
                drawRc.Top = 0;
                drawRc.Bottom = drawedLine * lineHeight;

                screen.Blt(texture, x, y, drawRc);
            }

            drawRc.Top = drawedLine * lineHeight;
            drawRc.Left = 0;
            drawRc.Right = counter.Value();
            drawRc.Bottom = drawRc.Top + lineHeight;

            if (drawRc.Bottom > ty)
            {
                drawRc.Bottom = ty;
            }

            screen.Blt(texture, x, y + drawedLine * lineHeight, drawRc);
        }

    }
}
