using apiwrappercli;
using apiwrappercli.poco.account;
using Prism.Commands;
using Prism.Mvvm;
using SPTrader.Common;
using SPTrader.Models;
using SPTrader.Views;
using SPTraderCore.BusinessLayer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows.Controls;

namespace SPTrader.ViewModels
{
    public partial class MainBarViewModel : BindableBase
    {
        #region Bindables MainBar
        private UserControl modeControl = new UserControl();
        public UserControl ModeControl
        {
            get { return modeControl; }
            set { SetProperty(ref modeControl, value); }
        }

        private int minWidth = 400;
        public int MinWidth
        {
            get { return minWidth; }
            set { SetProperty(ref minWidth, value); }
        }

        private bool isMode2 = false;
        public bool IsMode2
        {
            get { return isMode2; }
            set { SetProperty(ref isMode2, value); ChangeMode(); }
        }

        private ConnectionToolTipViewModel connectionToolTipViewModel = new ConnectionToolTipViewModel();
        public ConnectionToolTipViewModel ConnectionToolTipViewModel
        {
            get { return connectionToolTipViewModel; }
            set { SetProperty(ref connectionToolTipViewModel, value); }
        }

        private SPAccInfo accountingData = null;
        public SPAccInfo AccountingData
        {
            get { return accountingData; }
            set { SetProperty(ref accountingData, value); }
        }

        private AccountInfoViewModel accountInfoViewModel = null;
        public AccountInfoViewModel AccountInfoViewModel
        {
            get { return accountInfoViewModel; }
            set { SetProperty(ref accountInfoViewModel, value); }
        }

        private bool showAccountInfo = false;
        public bool ShowAccountInfo
        {
            get { return showAccountInfo; }
            set { SetProperty(ref showAccountInfo, value); }
        }
        #endregion

        private ModeViewModel modevm = null;

        public DelegateCommand SettingCommand { get; private set; }
        public DelegateCommand MarketCommand { get; private set; }
        public DelegateCommand ChartCommand { get; private set; }
        public DelegateCommand ClosingCommand { get; private set; }
        public DelegateCommand ConnectionCommand { get; private set; }

        public MainBarViewModel()
        {
            SettingCommand = new DelegateCommand(OnSettingCommand);
            MarketCommand = new DelegateCommand(OnMarketCommand);
            ChartCommand = new DelegateCommand(OnChartCommand);
            ClosingCommand = new DelegateCommand(OnClosingCommand);
            ConnectionCommand = new DelegateCommand(OnConnectionCommand);

            AccountInfoViewModel = new AccountInfoViewModel();
            AccountInfoViewModel.UpdateAccounting = new Action(OnUpdateAccounting);

            // should be onload?
            OnLoadModeContext();

            IsMode2 = GlobalSettings.Instance.IsMode2;
        }

        private void ChangeMode()
        {
            GlobalSettings.Instance.IsMode2 = IsMode2;

            // new view instance
            if (isMode2)
            {
                MinWidth = 830;
                modevm = new Mode2ViewModel();
                ModeControl = new Mode2(modevm);
            }
            else
            {
                MinWidth = 415;
                modevm = new Mode1ViewModel();
                ModeControl = new Mode1(modevm);
            }

            // apply content view models
            if (modevm != null)
            {
                modevm.OrderPositionViewModel = orderPositionViewModel;
                modevm.WatchListViewModel = watchListViewModel;
                modevm.PlaceOrderViewModel1 = placeOrderViewModel1;
                modevm.MainTradingViewModel = mainTradingViewModel;
            }

            // apply content sizes
            if (isMode2)
            {
                modevm.OrderPositionViewModel.Height = TradeSetting.POS_MIN_HEIGHT * 3;
                modevm.WatchListViewModel.Height = TradeSetting.WL_MIN_HEIGHT * 2;
                modevm.MainTradingViewModel.Height = TradeSetting.TRADE_MIN_HEIGHT * 2;
            }
            else
            {
                modevm.OrderPositionViewModel.Height = TradeSetting.POS_MIN_HEIGHT;
                modevm.WatchListViewModel.Height = TradeSetting.WL_MIN_HEIGHT;
                modevm.MainTradingViewModel.Height = TradeSetting.TRADE_MIN_HEIGHT;
            }

            // attempt to recover memory
            //GC.Collect();
        }

        public void OnUpdateAccounting()
        {
            if (AccountInfoViewModel != null)
            {
                AccountingData = AccountInfoViewModel.AccountingData;
                RaisePropertyChanged("AccountingData");
            }
            OnUpdatePosition();
        }

        private void OnUpdatePosition()
        {
            if (!ThrottleService.Instance.AllowSPEventThrough(AccountInfoViewModel))
                return;

            // update position data + mini account info
            if (orderPositionViewModel != null && accountInfoViewModel != null)
            {
                var accountData = accountInfoViewModel.accountData;
                orderPositionViewModel.Update(accountData);

                // update place order pos data
                if (placeOrderViewModel1 != null)
                {
                    var prod = placeOrderViewModel1.ProdCode;
                    placeOrderViewModel1.OrderPositionData = orderPositionViewModel.GetOrderPosition(prod);
                }
            }
        }

        private void OnSettingCommand()
        {
            AppSettings gs = new AppSettings();
            gs.ShowDialog();
        }

        private void OnMarketCommand()
        {
            MarketMain sp = new MarketMain();
            sp.Show();
        }

        private void OnChartCommand()
        {
            ScreenChartBigViewModel vm = new ScreenChartBigViewModel();
            vm.RenderChartForProduct(placeOrderViewModel1.ProdCode, 0);

            ScreenChartBigView vv = new ScreenChartBigView();
            vv.DataContext = vm;

            vv.Show();
        }

        private void OnClosingCommand()
        {
            AccountInfoViewModel.Close();
        }

        private void OnConnectionCommand()
        {
            ConnectionStatus cs = new ConnectionStatus();
            cs.ShowDialog();
        }
    }
}