using FLY.Thick.Base.IService;
using FLY.Thick.Blowing.IService;
using MathNet.Numerics;
using MultiLayout;
using MultiLayout.UiModule;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Navigation;
using Unity;

namespace FLY.Thick.Blowing.UI.UiModule
{
    /// <summary>
    /// DynAreaBlowing.xaml 的交互逻辑
    /// </summary>
    public partial class DynAreaBlowing : UserControl
    {
        IUnityContainer container;
        DynAreaBlowingVm viewModel;
        public DynAreaBlowing()
        {
            InitializeComponent();
        }

        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IBlowingService blowingFixService,
            IBlowingDetectService blowingDetectService)
        {
            this.container = container;

            viewModel = new DynAreaBlowingVm();
            viewModel.Init(blowingFixService, blowingDetectService);
            this.DataContext = viewModel;
        }

        private void Border_Blowing_Click(object sender, RoutedEventArgs e)
        {
            var p = container.Resolve<PgBlowing>();
            MultiLayout.FlyLayoutManager.NavigationService.Navigate(p);
        }
    }
    public class DynAreaBlowingVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public double Angle { get; set; } = -170.2;

        public double IconAngle { get; set; } = 170.2;

        //顺时针方向
        public bool IsCW { get; set; } = true;


        public TimeSpan RenZiJiaPeriod { get; set; } = TimeSpan.FromMinutes(12.3);

        public TimeSpan PastTime { get; set; } = TimeSpan.FromMinutes(3.3);

        public double FilmVelocity { get; set; } = 50.1;


        IBlowingService blowingFixService;
        IBlowingDetectService blowingDetectService;

        public void Init(
            IBlowingService blowingFixService,
            IBlowingDetectService blowingDetectService)
        {
            this.blowingFixService = blowingFixService;
            this.blowingDetectService = blowingDetectService;

            Misc.BindingOperations.SetBinding(this.blowingDetectService, nameof(IBlowingDetectService.RenZiJiaPeriod), this, nameof(RenZiJiaPeriod));
            Misc.BindingOperations.SetBinding(this.blowingDetectService, nameof(IBlowingDetectService.PastTime), this, nameof(PastTime));
            Misc.BindingOperations.SetBinding(this.blowingDetectService, nameof(IBlowingDetectService.FilmVelocity), this, nameof(FilmVelocity));
            Misc.BindingOperations.SetBinding(this.blowingDetectService, nameof(IBlowingDetectService.Angle), this, nameof(Angle));


            Misc.BindingOperations.SetBinding(this.blowingDetectService, nameof(IBlowingDetectService.Angle), updateIconAngle);
            Misc.BindingOperations.SetBinding(this.blowingDetectService, nameof(IBlowingDetectService.Direction), updateIsCW);


            Misc.BindingOperations.SetBinding(this.blowingFixService, nameof(IBlowingService.IsForwCW), ()=> {
                updateIconAngle();
                updateIsCW();
            });
        }
        void updateIconAngle() 
        {
            if (blowingFixService.IsForwCW)
            {
                IconAngle = blowingDetectService.Angle;
            }
            else 
            {
                IconAngle = -blowingDetectService.Angle;
            }
        }
        void updateIsCW() 
        {
            if (blowingFixService.IsForwCW)
            {
                IsCW = blowingDetectService.Direction == Misc.DIRECTION.FORWARD;
            }
            else
            {
                IsCW = blowingDetectService.Direction != Misc.DIRECTION.FORWARD;
            }
        }
    }

    public class UiModule2_DynAreaBlowing : MultiLayout.UiModule.IUiModule2
    {
        /// <summary>
        /// 控件标题
        /// 它的值取决于culture
        /// </summary>
        public string Title => "测厚.吹膜状态";
        public ComponentType Type => ComponentType.DynArea;
        public bool IsUnique => true;

        /// <summary>
        /// 控件
        /// 创建时,需要给它唯一ID,让加载自己的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FrameworkElement GetComponent(int id, IUnityContainer container)
        {
            return container.Resolve<DynAreaBlowing>();
        }

        /// <summary>
        /// 控件缩略图,用于编辑界面时,大致看看
        /// 创建时,需要给它唯一ID,让加载自己的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FrameworkElement GetThumbnail()
        {
            return new System.Windows.Controls.Grid();
        }


        /// <summary>
        /// 给出全部控件ID, 控件自行删除没有的参数
        /// </summary>
        /// <param name="IDs"></param>
        public void MatchParam(int[] IDs)
        {

        }
    }
    }