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 Navigator.Models;
using Navigator.Views;
using Xamarin.Forms;

namespace Navigator.ViewModels
{
    public class CompanyProfileViewModel : WorkspaceViewModel 
    {
      private PositionWithDescription selectedPosition;
      private ObservableCollection<PositionWithDescription> positions = null;
      private RelayCommand refreshCommand;
      private bool isRefreshing = false;
      private bool showActivity = true;

      public CompanyProfileViewModel()
      {
        Title="Company Profile";
        PropertyChanged += OnViewModelPropertyChanged;
        Initialize();
      }


      public void Initialize()
      {
        Task workerTask = Task.Factory.StartNew(() =>
        {
          HandlePositionsRefresh();
        });
        workerTask.ContinueWith((continuation) =>
        {
        });
      }

      private void OnViewModelPropertyChanged(object sender,PropertyChangedEventArgs eventArgs)
      {
        if(eventArgs.PropertyName.Equals("SelectedPosition"))
        {
          HandleSelectedPosition();
        }
      }

    private void HandleSelectedPosition()
    {
      if (null == selectedPosition || string.IsNullOrEmpty(selectedPosition.Symbol)) return;
      DataStore.AddItem(new Item("BollingerBandViewModelSymbol", selectedPosition.Symbol, "Symbol"));
      Application.Current.MainPage.Navigation.PushModalAsync(new BollingerBandPage());
      SelectedPosition = null;
    }

    public PositionWithDescription SelectedPosition
      {
        get { return selectedPosition; }
        set { SetProperty(ref selectedPosition, value); }
      }

      public ObservableCollection<PositionWithDescription> Positions
      {
        get { return positions; }
        set { SetProperty(ref positions, value); }
      }

      public void HandlePositionsRefresh()
      {
        IsBusy=true;
        ShowActivity=!IsRefreshing;
        Task workerTask = Task.Factory.StartNew(() =>
        {
          ServiceResult serviceResult = MarketDataServiceClient.GetInstance().GetPositionsWithDescription();
          if (serviceResult.Success)
          {
            List<PositionWithDescription> positionWithDescriptions = (List<PositionWithDescription>)serviceResult.ContextSpecificResult;
            if (null != positionWithDescriptions && positionWithDescriptions.Count > 0)
            {
              Positions = new ObservableCollection<PositionWithDescription>(positionWithDescriptions);
            }
          }
        });
        workerTask.ContinueWith((continuation) => 
        {
          IsBusy=false;
          IsRefreshing=false;
          ShowActivity=!IsRefreshing;
        });
      }

       public ICommand RefreshCommand
      {
        get
        {
          if(null==refreshCommand)
          {
            refreshCommand=new RelayCommand(param=>
            {
              HandlePositionsRefresh();
 //             IsRefreshing=false;
            },param=>{return !IsRefreshing;});
          }
          return refreshCommand;
        }
      }

      // If the Refreshing Indicstor is showing we don't want to show the ControlTemplate indicator... but we do want the screen to shade while the operation runs
      public bool ShowActivity
      {
        get {return showActivity;}
        set {SetProperty(ref showActivity,value);}
      }

      public bool IsRefreshing
      {
        get{return isRefreshing;}
        set{SetProperty(ref isRefreshing,value);}
      }
    }
  }