using System;
using System.Threading.Tasks;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.Input;
using MarketData.DataAccess;
using MarketData.Generator.Interface;
using MarketData.MarketDataModel;
using MarketData.Utils;
using PortfolioManager.ViewModels;

namespace PortfolioManager.Dialogs
{
    public partial class EditPositionDialogViewModel : DialogViewModelBase
    {
        private IPosition sourcePosition;
        private bool syncTrailingStop = true;

        public EditPositionDialogViewModel(Window dialogWindow, IPosition clonedPosition)
        : base(dialogWindow)
        {
            sourcePosition = clonedPosition;
            DisplayName = "Edit Position";
        }

        public String Symbol
        {
            get { return sourcePosition.Symbol; }
        }

        public String CompanyName
        {
            get
            {
                CompanyProfile companyProfile = CompanyProfileDA.GetCompanyProfile(sourcePosition.Symbol);
                return companyProfile.CompanyName;
            }
        }

        public String PurchaseDate
        {
            get
            {
                return sourcePosition.PurchaseDate.ToShortDateString();
            }
        }

        public String PurchasePrice
        {
            get
            {
                return Utility.FormatCurrency(sourcePosition.PurchasePrice);
            }
            set
            {
                sourcePosition.PurchasePrice = Utility.ParseCurrency(value);
                base.OnPropertyChanged("PurchasePrice");
                base.OnPropertyChanged("InitialStopRecommendation");
            }
        }

        public String InitialStopLimit
        {
            get
            {
                return Utility.FormatCurrency(sourcePosition.InitialStopLimit);
            }
            set
            {
                sourcePosition.InitialStopLimit = Utility.ParseCurrency(value);
                base.OnPropertyChanged("InitialStopLimit");
                if (syncTrailingStop)
                {
                    TrailingStopLimit = Utility.FormatCurrency(sourcePosition.InitialStopLimit);
                }
            }
        }

        public bool SyncTrailingStop
        {
            get
            {
                return syncTrailingStop;
            }
            set
            {
                syncTrailingStop = value;
                base.OnPropertyChanged("SyncTrailingStop");
            }
        }

        public String InitialStopRecommendation
        {
            get
            {
                return "Recommended Initial Stop: " + Utility.FormatCurrency(sourcePosition.PurchasePrice * (1.00 - sourcePosition.PositionRiskPercentDecimal), 2);
            }
        }

        public String TrailingStopLimit
        {
            get
            {
                return Utility.FormatCurrency(sourcePosition.TrailingStopLimit);
            }
            set
            {
                if (Utility.ParseCurrency(value).Equals(sourcePosition.TrailingStopLimit)) return;
                sourcePosition.TrailingStopLimit = Utility.ParseCurrency(value);
                base.OnPropertyChanged("TrailingStopLimit");
            }
        }

        public IPosition GetResult()
        {
            return sourcePosition;
        }

         [RelayCommand]
        public async Task OkButtonClick()
        {
            IsSuccess = true;
            await Close();
        }

        [RelayCommand]
        public async Task CancelButtonClick()
        {
            await Close();
        }
    }
}