using FLY.Thick.FilmCasting.Common;
using FLY.Thick.FilmCasting.IService;
using LiveCharts;
using LiveCharts.Configurations;
using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Linq;
using System.Windows.Media;
using System.Windows.Threading;

namespace FLY.Thick.FilmCasting.UI.UiModule
{
    public class TrendGraph2Vm : TrendGraphVm 
    {
        const int NULL_VALUE = -100000;

        #region 界面统计值

        /// <summary>
        /// 选择的位置 单位:分区序号
        /// </summary>
        public int SelectedIndex { get; private set; } = NULL_VALUE;

        public long SelectedId { get; private set; }

        public bool CanSelected { get; private set; }
        #endregion


        public override void Init(
            TrendGraphItemParam graphparam,
            IBulkDbService bulkDb,
            FilmCastingProfileParam profileParam)
        {
            base.Init(graphparam, bulkDb, profileParam);

            this.PropertyChanged += TrendGraph2Vm_PropertyChanged;
        }

        private void TrendGraph2Vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Id)) {

                CanSelected = Values.Count() > 0;

                //画面被更新，移动SelectedIndex
                if (SelectedIndex >= 0 && SelectedIndex < Values.Count())
                {
                    //找到对应的Id
                    for (int i = 0; i < Values.Count(); i++) {
                        if (Values[i].ScanDataID == SelectedId)
                        {
                            SelectedIndex = i;
                            break;
                        }
                    }
                    //找不到，只能清除
                    ClearSelected();
                }
            }
        }

        public void SetSelected(int index) 
        {
            SelectedIndex = index;

            if (SelectedIndex >= 0 && SelectedIndex < Values.Count()) {
                //找到对应的Id
                SelectedId = Values[SelectedIndex].ScanDataID;
            }
        }
        public void ClearSelected() {
            SelectedIndex = NULL_VALUE;
        }

        public void PreSelected() 
        {
            if (SelectedIndex > 0 && SelectedIndex <= Values.Count() - 1)
            {
                SetSelected(SelectedIndex - 1);
            }
            else {
                if(Values.Count()>0)
                    SetSelected(Values.Count()-1);
            }
        }
        public void NextSelected()
        {
            if (SelectedIndex >= 0 && SelectedIndex < Values.Count()-1)
            {
                SetSelected(SelectedIndex + 1);
            }
            else
            {
                if (Values.Count() > 0)
                    SetSelected(0);
            }
        }
    }


}
