using MarketData.DataAccess;
using MarketData.MarketDataModel;
using MarketData.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Media;
using Position=MarketData.Generator.CMTrend.Position;
using MarketData.Generator.Interface;

namespace TradeBlotter.UIUtils
{
  /// <summary>
  /// Interaction logic for EditPositionDialog.xaml
  /// </summary>
  public partial class EditPositionDialog : Window
  {
    private ManualResetEvent manualResetEvent=new ManualResetEvent(false);
    private String Symbol { get; set; }
    private DateTime PurchaseDate { get; set; }
    private double PurchasePrice{get;set;}
    private double TrailingStop{get;set;}
    private double InitialStop{get;set;}
    private double PositionRiskPercentDecimal{get;set;}
    private bool Result { get; set; }

    private EditPositionDialog(String title,IPosition position)
    {
      this.Background=new SolidColorBrush(Colors.AliceBlue);
      this.Owner=Application.Current.MainWindow;
      InitializeComponent();
      this.Loaded+=new RoutedEventHandler(EditPositionDialog_Loaded);
      this.Closing+=Window_Closing;

      dpPurchaseDate.SelectedDateChanged+=dpPurchaseDate_SelectionChanged;

      Title=title;
      Symbol=position.Symbol;
      PurchaseDate=position.PurchaseDate;
      PurchasePrice=position.PurchasePrice;
      TrailingStop=position.TrailingStopLimit;
      InitialStop=position.InitialStopLimit;
      PositionRiskPercentDecimal=position.PositionRiskPercentDecimal;

      txtSymbol.Text=Symbol;
      dpPurchaseDate.SelectedDate=PurchaseDate;

      lblInitialStopRecommendation.FontSize=12;

      cbSyncTrailingStop.IsChecked=true;

      UpdatePurchasePrice();
      UpdateInitialStop();
      UpdateTrailingStop();
      UpdateCompanyName();
      UpdateInitialStopRecommendation();
    }
    public ManualResetEvent Event { get { return manualResetEvent; } }

    public static IPosition Prompt(string title,IPosition position)
    {
      EditPositionDialog editPositionDialog=new EditPositionDialog(title,position);
      try { editPositionDialog.ShowDialog(); }
      catch(Exception) { return null; }
      editPositionDialog.Event.WaitOne();
      if(editPositionDialog.Result)
      {
        position.PurchaseDate=editPositionDialog.PurchaseDate;
        position.PurchasePrice=editPositionDialog.PurchasePrice;
        position.TrailingStopLimit=editPositionDialog.TrailingStop;
        position.InitialStopLimit=editPositionDialog.InitialStop;
        return position;
      }
      return null;
    }
    void EditPositionDialog_Loaded(object sender,RoutedEventArgs e)
    {
      txtPurchasePrice.Focus();
    }
    private void btnOk_Click(object sender,RoutedEventArgs e)
    {
      Result=Validate();
      if(!Result)
      {
        SystemSounds.Beep.Play();
        return;
      }
      Close();
      Event.Set();
      return;
    }
// *******************************************************************************************************************************************************************************
// ********************************************************************************** P U R C H A S E   P R I C E  ***************************************************************
// *******************************************************************************************************************************************************************************
    private void txtPurchasePrice__OnLostFocusHandler(Object sender,RoutedEventArgs routedEventArgs)
    {
      HandlePurchasePriceEvents();
    }
    private void txtPurchasePrice_OnKeyDownHandler(object sender,KeyEventArgs e)
    {
      if(e.Key==Key.Return)
      {
        HandlePurchasePriceEvents();
      }
    }
    private void HandlePurchasePriceEvents()
    {
      double value=0.00;
      value=Utility.ParseCurrency(txtPurchasePrice.Text);
      if(PurchasePrice.Equals(value)) return;
      PurchasePrice=value;
      UpdatePurchasePrice();
      UpdateInitialStopRecommendation();
    }
    private void UpdatePurchasePrice()
    {
      txtPurchasePrice.Text=Utility.FormatCurrency(PurchasePrice);
    }
// *******************************************************************************************************************************************************************************
// ********************************************************************************** I N I T I A L   S T O P   R E C O M M E N D A T I O N **************************************
// *******************************************************************************************************************************************************************************
    private void UpdateInitialStopRecommendation()
    {
      lblInitialStopRecommendation.Content="Recommended Initial Stop: "+Utility.FormatCurrency(PurchasePrice*(1.00-PositionRiskPercentDecimal),2);
    }
// *******************************************************************************************************************************************************************************
// ********************************************************************************** I N I T I A L   S T O P  *****************************************************************
// *******************************************************************************************************************************************************************************
    private void txtInitialStop__OnLostFocusHandler(Object sender,RoutedEventArgs routedEventArgs)
    {
      HandleInitialStopEvents();
    }
    private void txtInitialStop_OnKeyDownHandler(object sender,KeyEventArgs e)
    {
      if(e.Key==Key.Return)
      {
        HandleInitialStopEvents();
      }
    }
    private void HandleInitialStopEvents()
    {
      double value=0.00;
      value=Utility.ParseCurrency(txtInitialStop.Text);
      if(InitialStop.Equals(value)) return;
      InitialStop=value;
      UpdateInitialStop();
      if(null!= cbSyncTrailingStop.IsChecked && cbSyncTrailingStop.IsChecked.Value)
      {
        TrailingStop=value;
        UpdateTrailingStop();
      }
    }
    private void UpdateInitialStop()
    {
      txtInitialStop.Text=Utility.FormatCurrency(InitialStop);
    }
// *******************************************************************************************************************************************************************************
// ********************************************************************************** T R A I L I N G   S T O P  *****************************************************************
// *******************************************************************************************************************************************************************************
    private void txtTrailingStop__OnLostFocusHandler(Object sender,RoutedEventArgs routedEventArgs)
    {
      HandleTrailingStopEvents();
    }
    private void txtTrailingStop_OnKeyDownHandler(object sender,KeyEventArgs e)
    {
      if(e.Key==Key.Return)
      {
        HandleTrailingStopEvents();
      }
    }
    private void HandleTrailingStopEvents()
    {
      double value=0.00;
      value=Utility.ParseCurrency(txtTrailingStop.Text);
      if(TrailingStop.Equals(value)) return;
      TrailingStop=value;
      UpdateTrailingStop();
    }
    private void UpdateTrailingStop()
    {
      txtTrailingStop.Text=Utility.FormatCurrency(TrailingStop);
    }
// *********************************************************************************************************************************************************************************
    private void dpPurchaseDate_SelectionChanged(object sender,RoutedEventArgs e)
    {
      if(null==dpPurchaseDate.SelectedDate) return;
      PurchaseDate=dpPurchaseDate.SelectedDate.Value;
    }
// ***********************************************************************************************************************************************************************************
    private void Window_Closing(object sender,System.ComponentModel.CancelEventArgs e)
    {
      Event.Set();
    }
    private void btnCancel_Click(object sender,RoutedEventArgs e)
    {
      Result=false;
      Close();
      Event.Set();
    }
    private bool Validate()
    {
      DateGenerator dateGenerator=new DateGenerator();
      if(null==Symbol) return false;
      if(Utility.IsEpoch(PurchaseDate)) return false;
      if(!dateGenerator.IsMarketOpen(PurchaseDate))return false;
      if(double.IsNaN(PurchasePrice)) return false;
      if(double.IsNaN(TrailingStop)) return false;
      if(double.IsNaN(InitialStop)) return false;
      return true;
    }
    private void UpdateCompanyName()
    {
      CompanyProfile companyProfile=CompanyProfileDA.GetCompanyProfile(Symbol);
      if(null==companyProfile||null==companyProfile.CompanyName) return;
      lblCompanyName.Content=companyProfile.CompanyName.ToUpper();
    }
  }
}
