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

namespace Yamalib.Draw.Effect
{
    /// <summary>
    /// 簡易的なノイズ描画クラス
    /// </summary>
    public static class NoiseBltter
    {
        #region フィールドメンバ

        private static ITexture noiseTex;
        private static Rand rand;
        private static RootCounter move_d;
        private static RootCounter intervalCounter;
        private static RootCounter effectingCounter;
        private static bool effecting;

        #endregion

        /// <summary>
        /// 静的コンストラクタ
        /// </summary>
        static NoiseBltter()
        {
            rand = new Rand();
            rand.Randomize();
            move_d = new RootCounter(0, 480, 60);
            intervalCounter = new RootCounter(0, 180, 1);
            effectingCounter = new RootCounter(0, 120, 1);
        }

        /// <summary>
        /// ノイズテクスチャの設定
        /// </summary>
        public static ITexture NoiseTexture
        {
            get { return NoiseBltter.noiseTex; }
            set { NoiseBltter.noiseTex = value; }
        }

        /// <summary>
        /// 垂直非同期ノイズ
        /// </summary>
        /// <param name="screen"></param>
        /// <param name="src"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        public static void BltUnVerticalSync(IScreen screen, ITexture src, int x, int y)
        {
            if (screen == null || src == null) return;

            // 描画矩形
            int sx, sy;
            int tx, ty;
            sx = screen.Width;
            sy = screen.Height;
            tx = (int)src.Width;
            ty = (int)src.Height;

            // 通常描画
            if (!effecting)
            {
                screen.Blt(src, 0, 0);
            }
            else
            {
                Rect rc1 = new Rect();
                Rect rc2 = new Rect();
                rc1.SetRect(0, move_d.Value(), 640, move_d.EndValue);
                rc2.SetRect(0, 0, 640, move_d.Value());

                screen.Blt(src, 0, 0, rc1);
                screen.Blt(src, 0, (int)rc1.Height, rc2);
            }

            if (!effecting)
            {
                intervalCounter++;
                // 一周した？
                if (intervalCounter.Value() == 0)
                {
                    effectingCounter.EndValue = 60 + rand.GetRand(60);
                    effecting = true;
                }
            }
            else
            {
                move_d++;
                effectingCounter++;

                if (effectingCounter.Value() == 0)
                {
                    intervalCounter.EndValue = 100 + rand.GetRand(60);
                    effecting = false;
                }
            }
        }

        /// <summary>
        /// ノイズテクスチャよりノイズを描画します
        /// </summary>
        /// <param name="screen"></param>
        public static void BltNoise(IScreen screen)
        {
            if (screen == null || NoiseTexture == null) return;

            screen.Blt(NoiseTexture, -rand.GetRand(370), -rand.GetRand(250));
        }
    }
}
