using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System.Text;
using System.Windows.Input;
using MarketData;
using MarketData.Utils;
using MarketData.MarketDataModel;
using MarketData.Generator;
using MarketData.DataAccess;
using TradeBlotter.DataAccess;
using TradeBlotter.Command;
using TradeBlotter.Model;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using TradeBlotter.Cache;

namespace TradeBlotter.ViewModels
{
  public class ResistanceAndSupportViewModel : WorkspaceViewModel
  {
    private List<ResistanceSupport> resistanceAndSupportList;
    private String symbol;
    private String companyName;
    private Prices prices = null;
    private List<String> symbols;
    private List<String> watchLists;
    private String selectedWatchList;
    private RelayCommand refreshCommand;
    private List<Int32> dayCounts;
    private Int32 selectedDayCount;
    private bool isLegendVisible = true;
    private bool busyIndicator = false;
    private String busyContent = "";

    public ResistanceAndSupportViewModel()
    {
      base.DisplayName = "ResistanceAndSupportView";
      watchLists = WatchListDA.GetWatchLists();
      watchLists.Insert(0, Constants.CONST_ALL);
      selectedWatchList = watchLists.Find(x => x.Equals("Valuations"));
      symbols = WatchListDA.GetWatchList(selectedWatchList);
      dayCounts = new List<Int32>();
      dayCounts.Add(5);
      dayCounts.Add(10);
      dayCounts.Add(30);
      dayCounts.Add(60);
      dayCounts.Add(90);
      dayCounts.Add(180);
      dayCounts.Add(360);
      dayCounts.Add(720);
      dayCounts.Add(1440);
      dayCounts.Add(3600);
      selectedDayCount = dayCounts[1];
      PropertyChanged += OnResistanceAndSupportViewModelPropertyChanged;
    }
    public override SaveParameters GetSaveParameters()
    {
      return null;
    }
    public override void SetSaveParameters(SaveParameters saveParameters)
    {
    }
    public override bool CanPersist()
    {
      return false;
    }
    private void OnResistanceAndSupportViewModelPropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
    {
      if (eventArgs.PropertyName.Equals("SelectedSymbol") || (eventArgs.PropertyName.Equals("SelectedDayCount") && null != symbol))
      {
        Task workerTask = Task.Factory.StartNew(() =>
        {
          SetBusyState(true, "Loading Prices...");
          prices = PricingDA.GetPrices(symbol, selectedDayCount);
          companyName = PricingDA.GetNameForSymbol(symbol);
          resistanceAndSupportList = new List<ResistanceSupport>();
          SetBusyState(true, "Generating Resistance and Support Bands...");
          foreach (Price price in prices) resistanceAndSupportList.Add(new ResistanceSupport(price));
        });
        workerTask.ContinueWith((continuation)=>
        {
          SetBusyState(false);
          base.OnPropertyChanged("R1");
          base.OnPropertyChanged("S1");
          base.OnPropertyChanged("R2");
          base.OnPropertyChanged("S2");
          base.OnPropertyChanged("High");
          base.OnPropertyChanged("Low");
          base.OnPropertyChanged("Close");
          base.OnPropertyChanged("Title");
        });
      }
      else if (eventArgs.PropertyName.Equals("SelectedWatchList"))
      {
//        if (selectedWatchList.Equals(Constants.CONST_ALL)) symbols = PricingDA.GetSymbols();
        if (selectedWatchList.Equals(Constants.CONST_ALL)) symbols = SymbolCache.GetInstance().GetSymbols();
        else symbols = WatchListDA.GetWatchList(selectedWatchList);
        base.OnPropertyChanged("Symbols");
      }
    }
    public Boolean CheckBoxLegendVisible
    {
      get
      {
        return isLegendVisible;
      }
      set
      {
        isLegendVisible = value;
        base.OnPropertyChanged("CheckBoxLegendVisible");
        base.OnPropertyChanged("LegendVisible");
      }
    }
    public String LegendVisible
    {
      get
      {
        if (isLegendVisible) return "true";
        return "false";
      }
      set
      {
        isLegendVisible = Boolean.Parse(value);
        base.OnPropertyChanged("LegendVisible");
      }
    }
    public void SetBusyState(bool isBusy, String busyContent=null)
    {
      BusyIndicator = isBusy;
      if (isBusy)BusyContent=busyContent;
      else busyContent=null;
    }
    public bool BusyIndicator
    {
      get { return busyIndicator; }
      set
      {
        busyIndicator = value;
        base.OnPropertyChanged("BusyIndicator");
      }
    }
    public String BusyContent
    {
      get { return busyContent; }
      set 
      { 
        busyContent = value;
        base.OnPropertyChanged("BusyContent");
      }
    }
    public CompositeDataSource R1
    {
      get
      {
        CompositeDataSource compositeDataSource = ResistanceAndSupportModel.R1(resistanceAndSupportList);
        return compositeDataSource;
      }
    }
    public CompositeDataSource S1
    {
      get
      {
        CompositeDataSource compositeDataSource = ResistanceAndSupportModel.S1(resistanceAndSupportList);
        return compositeDataSource;
      }
    }
    public CompositeDataSource R2
    {
      get
      {
        CompositeDataSource compositeDataSource = ResistanceAndSupportModel.R2(resistanceAndSupportList);
        return compositeDataSource;
      }
    }
    public CompositeDataSource S2
    {
      get
      {
        CompositeDataSource compositeDataSource = ResistanceAndSupportModel.S2(resistanceAndSupportList);
        return compositeDataSource;
      }
    }
    public CompositeDataSource High
    {
      get
      {
        CompositeDataSource compositeDataSource = ResistanceAndSupportModel.High(resistanceAndSupportList);
        return compositeDataSource;
      }
    }
    public CompositeDataSource Low
    {
      get
      {
        CompositeDataSource compositeDataSource = ResistanceAndSupportModel.Low(resistanceAndSupportList);
        return compositeDataSource;
      }
    }
    public CompositeDataSource Close
    {
      get
      {
        CompositeDataSource compositeDataSource = ResistanceAndSupportModel.Close(resistanceAndSupportList);
        return compositeDataSource;
      }
    }
    public override String Title
    {
      get
      {
        if (null == companyName || null == prices || 0 == prices.Count) return "";
        return companyName + " (" + symbol + ") " + Utility.DateTimeToStringMMHDDHYYYY(prices[prices.Count - 1].Date) + " Thru " + Utility.DateTimeToStringMMHDDHYYYY(prices[0].Date);
      }
    }
    public Boolean Visibility
    {
      get { return false; }
    }
    public List<Int32> DayCounts
    {
      get { return dayCounts; }
    }
    public Int32 SelectedDayCount
    {
      get { return selectedDayCount; }
      set { selectedDayCount = value; base.OnPropertyChanged("SelectedDayCount"); }
    }
    public List<String> Symbols
    {
      get
      {
        return symbols;
      }
    }
    public String SelectedSymbol
    {
      get { return symbol; }
      set
      {
        if (value == symbol || String.IsNullOrEmpty(value)) return;
        symbol = value;
        base.OnPropertyChanged("SelectedSymbol");
      }
    }
    public List<String> WatchListNames
    {
      get
      {
        return watchLists;
      }
      set { ;}
    }
    public String SelectedWatchList
    {
      get { return selectedWatchList; }
      set { selectedWatchList = value; base.OnPropertyChanged("SelectedWatchList"); }
    }
    private void Refresh()
    {
      base.OnPropertyChanged("SelectedSymbol");
    }
    private bool CanRefresh
    {
      get { return true; }
    }
    public ICommand RefreshCommand
    {
      get
      {
        if (refreshCommand == null)
        {
          refreshCommand = new RelayCommand(param => this.Refresh(), param => this.CanRefresh);
        }
        return refreshCommand;
      }
    }
  }
}
