using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Input;
using MarketData.MarketDataModel;
using MarketData.Service;
using Navigator.Core;
using DataDisplay.Renderers;
using Navigator.MarketDataModel;
using Navigator.Renderers;
using System.Threading;

namespace Navigator.ViewModels
{
    public class ConsumerPriceIndexViewModel : WorkspaceViewModel 
    {
      private RelayCommand refreshCommand;
    private bool isRefreshing = false;
    private ConsumerPriceIndexRenderer consumerPriceIndexRenderer=new ConsumerPriceIndexRenderer();
    private String selectedIndex;
    private PriceIndices priceIndices;
    private String chartTitle;
    private ObservableCollection<String> indexCollection;
    private bool showActivity = true;

    public ConsumerPriceIndexViewModel()
      {
				Title = "Consumer Price Index";
        Initialize();
			  PropertyChanged += OnViewModelPropertyChanged;
      }
      private void OnViewModelPropertyChanged(object sender,PropertyChangedEventArgs eventArgs)
      {
      if (eventArgs.PropertyName.Equals("SelectedIndex"))
      {
        if (null == selectedIndex) return;
        HandleSelectedIndexRequest();
      }
    }
      private void Initialize()
      {
        Task workerTask=Task.Factory.StartNew(()=>
        {
          IEnumerable<String> indices=null;
          ServiceResult serviceResult=MarketDataServiceClient.GetInstance().GetDistinctConsumerPriceIndices();
          if(serviceResult.Success)indices=(List<String>)serviceResult.ContextSpecificResult;
          IndexCollection=new ObservableCollection<string>(indices);
        });
        workerTask.ContinueWith((continuation)=>
        {
        });
      }

    public void HandleSelectedIndexRequest()
    {
      ServiceResult serviceResult = null;
      IsBusy=true;
      ShowActivity=!IsRefreshing;
      Task workerTask = Task.Factory.StartNew(() =>
      {
        serviceResult = MarketDataServiceClient.GetInstance().GetGetConsumerPriceIndex(selectedIndex);
        if (serviceResult.Success)
        {
          ChartTitle = selectedIndex;
          priceIndices = new PriceIndices((List<PriceIndex>)serviceResult.ContextSpecificResult);
          consumerPriceIndexRenderer.ConsumerPriceIndexElements=ConsumerPriceIndexModel.Elements(priceIndices);
          consumerPriceIndexRenderer.SetColorBlack();
          consumerPriceIndexRenderer.Refresh();
        }
      });
      workerTask.ContinueWith((TaskContinuationOptions) =>
      {
        IsBusy=false;
        IsRefreshing=false;
        ShowActivity=!IsRefreshing;
      });
    }
    public ObservableCollection<String> IndexCollection
    {
      get { return indexCollection; }
      set { SetProperty(ref indexCollection, value); }
    }
    public IRenderer Renderer
    {
      get{return consumerPriceIndexRenderer;}
    }

    public String SelectedIndex
    {
      get { return selectedIndex; }
      set { SetProperty(ref selectedIndex, value); }
    }
    public String ChartTitle
    {
      get { return chartTitle; }
      set { SetProperty(ref chartTitle, value); }
    }
      public bool IsRefreshing
      {
        get{return isRefreshing;}
        set{SetProperty(ref isRefreshing,value);}
      }
      public bool ShowActivity
      {
        get {return showActivity;}
        set {SetProperty(ref showActivity,value);}
      }
      public ICommand RefreshCommand
      {
        get
        {
          if(null==refreshCommand)
          {
            refreshCommand=new RelayCommand(param=>
            {
              HandleSelectedIndexRequest();
            },param=>{return !IsRefreshing;});
          }
          return refreshCommand;
        }
      }
    }
}
