using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HistoryDLL
{
    public partial class CyclicScroller : UserControl
    {
        //OpacityMask Setting
        LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
        GradientStopCollection myGradientStopCollection = new GradientStopCollection();
        GradientStop LeftLineLeftPointStopFirst = new GradientStop();
        GradientStop LeftLineRightPointStopFirst = new GradientStop();
        GradientStop RightLineLeftPointStopFirst = new GradientStop();
        GradientStop RightLineRightPointStopFirst = new GradientStop();

        GradientStop LeftLineLeftPointStopSecond = new GradientStop();
        GradientStop LeftLineRightPointStopSecond = new GradientStop();
        GradientStop RightLineLeftPointStopSecond = new GradientStop();
        GradientStop RightLineRightPointStopSecond = new GradientStop();

        GradientStop LeftLineLeftPointStopThree = new GradientStop();
        GradientStop LeftLineRightPointStopThree = new GradientStop();
        GradientStop RightLineLeftPointStopThree = new GradientStop();
        GradientStop RightLineRightPointStopThree = new GradientStop();

        bool IsUsingFirst = false, IsUsingSecond = false, IsUsingThree = false;
        /// <summary>
        /// 拖動隱藏區塊顯示設定
        /// </summary>
        private void SetDisplayPart()
        {
            #region First
            LeftLineLeftPointStopFirst.Offset = 0.0;
            Color c1 = new Color();
            c1 = GetColorRGB(true);
            LeftLineLeftPointStopFirst.Color = c1;

            LeftLineRightPointStopFirst.Offset = 0.0;
            Color c2 = new Color();
            c2 = GetColorRGB(false);
            LeftLineRightPointStopFirst.Color = c2;

            RightLineLeftPointStopFirst.Offset = 0.0;
            Color c3 = new Color();
            c3 = GetColorRGB(false);
            RightLineLeftPointStopFirst.Color = c3;

            RightLineRightPointStopFirst.Offset = 0.0;
            Color c4 = new Color();
            c4 = GetColorRGB(true);
            RightLineRightPointStopFirst.Color = c4;
            #endregion 

            #region Second
            LeftLineLeftPointStopSecond.Offset = 0.0;
            Color c5 = new Color();
            c5 = GetColorRGB(true);
            LeftLineLeftPointStopSecond.Color = c5;

            LeftLineRightPointStopSecond.Offset = 0.0;
            Color c6 = new Color();
            c6 = GetColorRGB(false);
            LeftLineRightPointStopSecond.Color = c6;

            RightLineLeftPointStopSecond.Offset = 0.0;
            Color c7 = new Color();
            c7 = GetColorRGB(false);
            RightLineLeftPointStopSecond.Color = c7;

            RightLineRightPointStopSecond.Offset = 0.0;
            Color c8 = new Color();
            c8 = GetColorRGB(true);
            RightLineRightPointStopSecond.Color = c8;
            #endregion

            #region Three
            LeftLineLeftPointStopThree.Offset = 0.0;
            Color c9 = new Color();
            c9 = GetColorRGB(true);
            LeftLineLeftPointStopThree.Color = c9;

            LeftLineRightPointStopThree.Offset = 0.0;
            Color c10 = new Color();
            c10 = GetColorRGB(false);
            LeftLineRightPointStopThree.Color = c10;

            RightLineLeftPointStopThree.Offset = 0.0;
            Color c11 = new Color();
            c11 = GetColorRGB(false);
            RightLineLeftPointStopThree.Color = c11;

            RightLineRightPointStopThree.Offset = 0.0;
            Color c12 = new Color();
            c12 = GetColorRGB(true);
            RightLineRightPointStopThree.Color = c12;
            #endregion

            #region add GradientStop into Collection
            myGradientStopCollection.Add(LeftLineLeftPointStopThree);
            myGradientStopCollection.Add(LeftLineRightPointStopThree);
            myGradientStopCollection.Add(RightLineLeftPointStopThree);
            myGradientStopCollection.Add(RightLineRightPointStopThree);
            myGradientStopCollection.Add(LeftLineLeftPointStopSecond);
            myGradientStopCollection.Add(LeftLineRightPointStopSecond);
            myGradientStopCollection.Add(RightLineLeftPointStopSecond);
            myGradientStopCollection.Add(RightLineRightPointStopSecond);
            myGradientStopCollection.Add(LeftLineLeftPointStopFirst);
            myGradientStopCollection.Add(LeftLineRightPointStopFirst);
            myGradientStopCollection.Add(RightLineLeftPointStopFirst);
            myGradientStopCollection.Add(RightLineRightPointStopFirst);
            #endregion

            myLinearGradientBrush.StartPoint = new Point(0, 0);
            myLinearGradientBrush.EndPoint = new Point(1, 0);
            myLinearGradientBrush.GradientStops = myGradientStopCollection;

            uxCyclicGrid.OpacityMask = myLinearGradientBrush;
        }

        private Color GetColorRGB(bool Show)
        {
            Color result = new Color();

            result.R = 255;
            result.G = 255;
            result.B = 255;
            if (Show)
            {
                result.A = 255;
            }
            else
            {
                result.A = 0;
            }

            return result;
        }

        //判斷允許後面顯示的區域~
        //不管螢幕個數~最多為三個可畫分的區塊~在SetDisplayPart 函數中~已經將工具加入
        //判斷方式 : 取用時~必須判斷為連續幾個 isusing 後 ~ 為一個區塊將計算值放到第一個區塊中~
        //一般來說一個螢幕指會需要用到一個該工具~如果今天是雙或者三螢幕
        //則必須按照第一個取用方式~去設定第二個甚至第三個~
        private void CheckDisplayRange()
        {
            int startindex = -1, endindex = -1;
            int useTools = 1;
            bool HaveUsing = false;
            double startvalue = 0;
            double endvalue = 0;

            IsUsingFirst = IsUsingSecond = IsUsingThree = false;

            for (int i = 0; i < ExtraUnitGridList.Count; i++)
            {
                if (ExtraUnitGridList[i].IsUsing != 0 && !HaveUsing)
                {
                    #region GradientStop 的起始位置
                    HaveUsing = true;
                    startindex = i;
                    startvalue = 0;
                    double Leftwidth = 0;
                    //計算到此左邊的寬度
                    for (int index = 0; index < startindex; index++)
                    {
                        if (index == 0)
                        {
                            //主要為最左邊的寬度~判斷最左邊區塊寬度~為縮小或者為正常大小
                            if (ExtraUnitGridList[index].Width <= MainEventWidth - (ExtraUnitGridList[index].ShrinkValueOne))
                            {
                                //縮小 : 原始大小 - 隱藏的大小 - 右邊被縮小的距離
                                Leftwidth += MainEventWidth - NoneUseRange - (ExtraUnitGridList[index].ShrinkValueOne);
                            }
                            else
                            {
                                //正常大小計算方式 : 原始大小 - 隱藏的大小
                                Leftwidth += MainEventWidth - NoneUseRange;
                            }
                        }
                        else
                        {
                            Leftwidth += ExtraUnitGridList[index].Width;
                        }
                    }
                    startvalue = Leftwidth / (double)ResolutionValueWidth;
                    //Console.WriteLine("起點 : " + Leftwidth);
                    #endregion
                }
                else
                {
                    if (HaveUsing && ExtraUnitGridList[i].IsUsing == 0)
                    {
                        #region GradientStop 的停止位置 (還沒找到最後一個)
                        endindex = i;
                        HaveUsing = false;
                        endvalue = 0;
                        double Leftwidth = 0;
                        //計算到此左邊的寬度
                        for (int index = 0; index < endindex; index++)
                        {
                            if (index == 0)
                            {
                                if (ExtraUnitGridList[index].Width < MainEventWidth)
                                {
                                    //因為左邊縮小的話只有右邊 - ExtraUnitGridList[index].ShrinkValueOne 顧~不用除以2
                                    Leftwidth += MainEventWidth - NoneUseRange - (ExtraUnitGridList[index].ShrinkValueOne);
                                }
                                else
                                {
                                    Leftwidth += MainEventWidth - NoneUseRange;
                                }
                            }
                            else
                            {
                                Leftwidth += ExtraUnitGridList[index].Width;
                            }
                        }
                        endvalue = Leftwidth / (double)ResolutionValueWidth;
                        //Console.WriteLine("終點 : " + Leftwidth);

                        if (useTools == 1)
                        {
                            LeftLineLeftPointStopFirst.Offset = startvalue;
                            LeftLineRightPointStopFirst.Offset = startvalue;
                            RightLineLeftPointStopFirst.Offset = endvalue;
                            RightLineRightPointStopFirst.Offset = endvalue;

                            IsUsingFirst = true;
                            useTools++;
                        }
                        else if (useTools == 2)
                        {
                            LeftLineLeftPointStopSecond.Offset = startvalue;
                            LeftLineRightPointStopSecond.Offset = startvalue;
                            RightLineLeftPointStopSecond.Offset = endvalue;
                            RightLineRightPointStopSecond.Offset = endvalue;

                            IsUsingSecond = true;
                            useTools++;
                        }
                        else if (useTools == 3)
                        {
                            LeftLineLeftPointStopThree.Offset = startvalue;
                            LeftLineRightPointStopThree.Offset = startvalue;
                            RightLineLeftPointStopThree.Offset = endvalue;
                            RightLineRightPointStopThree.Offset = endvalue;

                            IsUsingThree = true;
                            useTools = 1;
                        }
                        #endregion
                    }
                    //或者最後一個Grid
                    else if (HaveUsing && i + 1 == ExtraUnitGridList.Count)
                    {
                        #region GradientStop 的停止位置 (最後一個的話)
                        HaveUsing = false;
                        endvalue = 1;

                        if (useTools == 1)
                        {
                            LeftLineLeftPointStopFirst.Offset = startvalue;
                            LeftLineRightPointStopFirst.Offset = startvalue;
                            RightLineLeftPointStopFirst.Offset = endvalue;
                            RightLineRightPointStopFirst.Offset = endvalue;

                            IsUsingFirst = true;
                            useTools++;
                        }
                        else if (useTools == 2)
                        {
                            LeftLineLeftPointStopSecond.Offset = startvalue;
                            LeftLineRightPointStopSecond.Offset = startvalue;
                            RightLineLeftPointStopSecond.Offset = endvalue;
                            RightLineRightPointStopSecond.Offset = endvalue;

                            IsUsingSecond = true;
                            useTools++;
                        }
                        else if (useTools == 3)
                        {
                            LeftLineLeftPointStopThree.Offset = startvalue;
                            LeftLineRightPointStopThree.Offset = startvalue;
                            RightLineLeftPointStopThree.Offset = endvalue;
                            RightLineRightPointStopThree.Offset = endvalue;

                            IsUsingThree = true;
                            useTools = 1;
                        }
                        #endregion
                    }
                }
            }
            if (!IsUsingFirst)
            {
                LeftLineLeftPointStopFirst.Offset = 0;
                LeftLineRightPointStopFirst.Offset = 0;
                RightLineLeftPointStopFirst.Offset = 0;
                RightLineRightPointStopFirst.Offset = 0;
            }
            if (!IsUsingSecond)
            {
                LeftLineLeftPointStopSecond.Offset = 0;
                LeftLineRightPointStopSecond.Offset = 0;
                RightLineLeftPointStopSecond.Offset = 0;
                RightLineRightPointStopSecond.Offset = 0;
            }
            if (!IsUsingThree)
            {
                LeftLineLeftPointStopThree.Offset = 0;
                LeftLineRightPointStopThree.Offset = 0;
                RightLineLeftPointStopThree.Offset = 0;
                RightLineRightPointStopThree.Offset = 0;
            }
        }
    }
}
