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

namespace HistoryDLL
{
    /// <summary>
    /// LiveContainer 事件傳遞與通訊
    /// </summary>
    public partial class LiveContainer : UserControl
    {
        //外掛 Delegate event for communicate with CyclicScroller (通知用)
        public delegate void LiveContainerBubbleToCyclicScroller(string ProcessCode, int index, int indexofevent, int indexofpic);
        public event LiveContainerBubbleToCyclicScroller LiveContainerBubbleToCyclicScrollerEvent;
        //外掛 Delegate event for communicate with PlaneRotateMap (通知用)
        public delegate void LiveContainerBubbleToPlaneRotateMap(string ProcessCode, int blockIndex);
        public event LiveContainerBubbleToPlaneRotateMap LiveContainerBubbleToPlaneRotateEvent;

        //通知各 CyclicScroller 函數
        private void SendInfoToCyclicScroller(string ProcessCode)
        {
            if (ProcessCode == "Load Complete")
            {
                this.Dispatcher.BeginInvoke(LiveContainerBubbleToCyclicScrollerEvent, ProcessCode, ContainerIndex, TriggerEvent, TriggerPicture);
            }
            else if (this.canControl)
            {
                this.Dispatcher.BeginInvoke(LiveContainerBubbleToCyclicScrollerEvent, ProcessCode, ContainerIndex, TriggerEvent, TriggerPicture);
            }
        }

        //接收到~來自 CyclicScroller 的通知
        void ReceiveFromCyclicScroller(string ProcessCode)
        {
            if (ProcessCode == "AutoRun")
            {
                for (int i = 0; i < MainUpSideGrid.Children.Count; i++)
                {
                    ((PlaneRotateMap)MainUpSideGrid.Children[i]).StartCountDownForRatate();
                }
            }
            else if (ProcessCode == "Manual")
            {
                for (int i = 0; i < MainUpSideGrid.Children.Count; i++)
                {
                    ((PlaneRotateMap)MainUpSideGrid.Children[i]).StopCountDownForRatate();
                }
            }
            TriggerEvent = 0;
        }

        //接收來自 PlaneRotateMap 通知的通道(確認可顯示的資料是否已經到底)
        void ReceiveFromPlaneRotateFileBlankInfo(string ProcessCode, int BlockIndex)
        {
            if (ProcessCode == "DataEnd")
            {
                #region 資料到底處理
                //被通知到有資料到底後先判斷 buffer 中有沒有可以替換的資料
                if (PlaneRotateMapList.Count <= 0)
                {
                    //Buffer 中沒有其他資料~通知繼續循環
                    this.Dispatcher.BeginInvoke(LiveContainerBubbleToPlaneRotateEvent, "Cycle", BlockIndex);
                }
                else
                {
                    //有~則通知改變
                    this.Dispatcher.BeginInvoke(LiveContainerBubbleToPlaneRotateEvent, "Change", BlockIndex);
                }
                #endregion
            }
            else if (ProcessCode == "Prepare")
            {
                int row = 0, column = 0;
                //從Buffer中亂數取得要顯示的資料更換
                PlaneRotateMap newprm = new PlaneRotateMap();
                PlaneRotateMap oldprm = new PlaneRotateMap();

                GetRowAndCol(BlockIndex, ref row, ref column);
                //從Grid中抽出舊的 prm 處理
                oldprm = (PlaneRotateMap)MainUpSideGrid.Children[BlockIndex];
                oldprm.BlockIndex = -1;
                oldprm.StopCountDownForRatate();

                //亂數取得要放入的 PlaneRotateMap (不重複)
                int rndIndex = rnd.Next(0, PlaneRotateMapList.Count);

                //抽出該PlaneRotateMap 並做設定~完後指到 newprm
                newprm = SelectprmToShow(rndIndex, BlockIndex);

                MainUpSideGrid.Children.RemoveAt(BlockIndex);
                MainUpSideGrid.Children.Insert(BlockIndex, newprm);
                newprm.StartCountDownForRatate();
                //放入指定的位置
                Grid.SetRow(PlaneRotateMapList[rndIndex], row);
                Grid.SetColumn(PlaneRotateMapList[rndIndex], column);
                //移除該prm在亂數選擇的Buffer當中 (該 Buffer 只存在沒有顯示的事件)
                PlaneRotateMapList.RemoveAt(rndIndex);
                oldprm.StopCountDownForRatate();
                PlaneRotateMapList.Add(oldprm);
                //變換資料完成
                this.Dispatcher.BeginInvoke(LiveContainerBubbleToPlaneRotateEvent, "Finish", BlockIndex);
            }
            else if (ProcessCode == "Load Complete")
            {
                SendInfoToCyclicScroller(ProcessCode);
            }
        }

        private void GetRowAndCol(int source, ref int row, ref int column)
        {
            switch (source)
            {
                case 0:
                    {
                        row = 5;
                        column = 1;
                        break;
                    }
                case 1:
                    {
                        row = 5;
                        column = 3;
                        break;
                    }
                case 2:
                    {
                        row = 3;
                        column = 1;
                        break;
                    }
                case 3:
                    {
                        row = 3;
                        column = 3;
                        break;
                    }
                case 4:
                    {
                        row = 1;
                        column = 1;
                        break;
                    }
                case 5:
                    {
                        row = 1;
                        column = 3;
                        break;
                    }
            }
        }

    }
}
