using C1.Xaml;
using StockPortfolio.Common;
using StockPortfolio.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace StockPortfolio
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    
    public sealed partial class HistoricalTradeMainPage : Page
    {
        private readonly NavigationHelper navigationHelper;
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        public HistoricalTradeMainPage()
        {
            this.InitializeComponent();
            
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
            this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
        }

        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data        
           await LoadHistoricalTradeData();

        }

        private async Task LoadHistoricalTradeData()
        {
            holded.Text = "0";
            holding.Text = "0";
            var user = SampleDataSource.GetCurrentUser();

            List<HistoricalTradeStockModel> list = new List<HistoricalTradeStockModel>();
            List<string> historicalStocks = new List<string>();
            foreach (var item in user.MyStocks)
            {
                var his = new HistoricalTradeStockModel();
                his.StockSymbol = item.StockSymbol;
                his.HeaderBrush = new SolidColorBrush(Color.FromArgb(0xff, 0xd6, 0x00, 0x00));
                his.HistoricalStocks = user.HistoricalTradeStock.Where(x => x.Stock.StockSymbol == item.StockSymbol).OrderBy(x => x.TradeTime).ToList();
                list.Add(his);
                historicalStocks.Add(his.StockSymbol);
            }
            holding.Text = user.MyStocks.Count.ToString();
            foreach (var item in user.HistoricalTradeStock)
            {
                if (!historicalStocks.Contains(item.Stock.StockSymbol))
                {
                    var his = new HistoricalTradeStockModel();
                    his.StockSymbol = item.Stock.StockSymbol;
                    his.HeaderBrush = new SolidColorBrush(Color.FromArgb(0xff, 0xf8, 0xe0, 0x00));
                    his.HistoricalStocks = user.HistoricalTradeStock.Where(x => x.Stock.StockSymbol == item.Stock.StockSymbol).OrderBy(x => x.TradeTime).ToList();
                    list.Add(his);
                    historicalStocks.Add(item.Stock.StockSymbol);
                }
            }
            holded.Text = (list.Count - user.MyStocks.Count).ToString();
            
            HistoricalLists.ItemsSource = list;
            if (list.Count == 0)
            {
                bool? result = true;
                List<IUICommand> commands = new List<IUICommand>();
                commands.Add(new UICommand("Cancel", new UICommandInvokedHandler((cmd) => result = false)));
                await Util.ShowDialog("No records for user stock historical trade?", "Message", commands);

            }
            else
            {
                HistoricalLists.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param>
        /// <param name="e">Event data that provides an empty dictionary to be populated with
        /// serializable state.</param>
        private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
            // TODO: Save the unique state of the page here.
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);

            
            
        }



        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedFrom(e);
        }

        private void HistoricalLists_SelectionChanged(object sender, EventArgs e)
        {
            if (HistoricalLists.SelectedItem!=null)
            {
                var stockSymbol = HistoricalLists.SelectedItem.ToString();

                var user = SampleDataSource.GetCurrentUser();

                var list = user.HistoricalTradeStock.Where(x => x.Stock.StockSymbol == stockSymbol).OrderBy(x => x.TradeTime).ToList();
                marketFlexGrid.ItemsSource = list;
            }
          
        }
    }
}
