using FlyADBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace Flyad7_WPF
{
    public class UnityExt
    {

        public static UnityContainer Container;
        static UnityExt()
        {
            Init();
        }
        static void Init()
        {
            Container = new UnityContainer();

            Container.RegisterType<DebugAppParam>(
                new Unity.Lifetime.ContainerControlledLifetimeManager());

            Container.RegisterFactory<FlyAD7>((unity) =>
            {
                FlyAD7 flyAd = new FlyAD7();
                flyAd.Load();
                return flyAd;
            }, new Unity.Lifetime.ContainerControlledLifetimeManager());

            Container.RegisterFactory<AutoForwBackw>((unity) =>
            {
                var flyad = Container.Resolve<FlyAD7>();
                AutoForwBackw autofb = new AutoForwBackw(flyad);
                var param = unity.Resolve<DebugAppParam>();
                Misc.BindingOperations.SetBinding(param, nameof(param.FB_Pos1), autofb, nameof(autofb.FB_Pos1), Misc.BindingOperations.BindingMode.TwoWay);
                Misc.BindingOperations.SetBinding(param, nameof(param.FB_Pos2), autofb, nameof(autofb.FB_Pos2), Misc.BindingOperations.BindingMode.TwoWay);
                return autofb;
            }, new Unity.Lifetime.ContainerControlledLifetimeManager());

            Container.RegisterType<FlyADClientUI>(
                new Unity.Lifetime.ContainerControlledLifetimeManager());

            Container.RegisterType<AutoGetGridAdv>(
                new Unity.Lifetime.ContainerControlledLifetimeManager());

            Container.RegisterType<AutoSync>(
                new Unity.Lifetime.ContainerControlledLifetimeManager());

            Container.RegisterFactory<GridAdvVm>((unity) =>
            {
                var flyad = Container.Resolve<FlyAD7>();
                GridAdvVm gridAdvVm = new GridAdvVm(flyad);
                var param = unity.Resolve<DebugAppParam>();
                Misc.BindingOperations.SetBinding(param, nameof(param.HasGridAdv), gridAdvVm, nameof(gridAdvVm.HasGridAdv), Misc.BindingOperations.BindingMode.TwoWay);
                return gridAdvVm;
            }, new Unity.Lifetime.ContainerControlledLifetimeManager());
        }

    }
}
