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.Graphics.Display;
using Windows.UI.Core;
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;
using C1.Xaml.FlexGrid;


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

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

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }
        AppBarButton btnFind = new AppBarButton { Icon = new SymbolIcon(Symbol.Find), Label = "New Trade" };
        public MyPortfolioMainPage()
        {
            this.InitializeComponent();
            

            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
           
            btnFind.Click += btnFind_Click;
        }

        void btnFind_Click(object sender, RoutedEventArgs e)
        {
            if (!Frame.Navigate(typeof(SearchStockPage)))
            {
                throw new Exception("NavigationFailedExceptionMessage");
            }
        }

       

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            progressBar.Visibility = Visibility.Visible;
            var stockData = await SampleDataSource.GetUserStocksAsync();       
            progressBar.Visibility = Visibility.Collapsed;
          
            this.marketFlexGrid.ItemsSource = stockData;
           
          
        }
       
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);
            bottomAppBar.PrimaryCommands.Add(btnFind);
          
        }

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

        private async void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            //StockDataItem item = (sender as Button).DataContext as StockDataItem;
            //if (item != null)
            //{

            //    bool? result = true;

            //    List<IUICommand> commands = new List<IUICommand>();
            //    commands.Add(new UICommand("Yes", new UICommandInvokedHandler((cmd) => result = true)));
            //    commands.Add(new UICommand("Cancel", new UICommandInvokedHandler((cmd) => result = false)));
            //    await Util.ShowDialog("Are you sure you want to delete this symbol?", "Delete Confirmation", commands);
            //    if (result == true)
            //    {
            //        await SampleDataSource.DeleteItemAsync(item);
            //    }

            //}
        }
      
        private void marketFlexGridDoubleClick(object sender, RoutedEventArgs e)
        {
            var flexGrid = sender as C1FlexGrid;
            if (flexGrid != null && flexGrid.SelectedItem != null)
            {

                if (!Frame.Navigate(typeof(StockDetailPage), flexGrid.SelectedItem))
                {
                    throw new Exception("NavigationFailedExceptionMessage");
                }

            }
        }

        private void Buy_Click(object sender, RoutedEventArgs e)
        {

            StockDataItem item = (sender as Button).DataContext as StockDataItem;
            if (item != null)
            {
                List<object> parameter = new List<object>();
                parameter.Add("Buy");
                parameter.Add(item);

                if (!Frame.Navigate(typeof(SearchStockPage), parameter))
                {
                    throw new Exception("NavigationFailedExceptionMessage");
                }
            }
           
        }

        private void Sell_Click(object sender, RoutedEventArgs e)
        {
            StockDataItem item = (sender as Button).DataContext as StockDataItem;
            if (item!=null)
            {
                List<object> parameter = new List<object>();
                parameter.Add("Sell");
                parameter.Add(item);
                if (!Frame.Navigate(typeof(SearchStockPage),parameter))
                {
                    throw new Exception("NavigationFailedExceptionMessage");
                }
            }
           

        }

        private void Chart_Click(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;
            if (bt != null)
            {

                if (!Frame.Navigate(typeof(StockDetailPage), bt.DataContext))
                {
                    throw new Exception("NavigationFailedExceptionMessage");
                }

            }
        }
     

     
    }
}
