using System;
using System.IO;
using System.Windows;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using System.Reflection;
using System.Threading.Tasks;
using Windows.Storage;
using C1.Xaml;

namespace Fortune1000
{
    /// <summary>
    /// Source: http://www.forbes.com/lists/2009/18/global-09_The-Global-2000_Rank.html
    /// </summary>
    public class Model : DependencyObject, INotifyPropertyChanged
    {
        // ** fields
        C1CollectionView _view;

        // ** ctor
        public Model()
        {
            Load();
        }

        private async Task Load()
        {
            var cvs = new C1CollectionView();
            cvs.SourceCollection = await LoadCompanies();
            _view = cvs;
            RaisePropertyChanged("Companies");
        }

        // ** object model
        public C1CollectionView Companies
        {
            get { return _view; }
        }
        /// <summary>
        /// Gets or sets whether the data should be grouped by country and industry.
        /// </summary>
        public bool GroupData
        {
            get { return (bool)GetValue(GroupDataProperty); }
            set { SetValue(GroupDataProperty, value); }
        }
        /// <summary>
        /// Identifies the <see cref="GroupData"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty GroupDataProperty =
            DependencyProperty.Register(
                "GroupData", typeof(bool), typeof(Model),
                new PropertyMetadata(false, OnGroupDataPropertyChanged));
        static void OnGroupDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var model = (Model)d;
            var view = model.Companies;
            //view.GroupDescriptions.Clear();
            //if (model.GroupData)
            //{
            //    view.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
            //    view.GroupDescriptions.Add(new PropertyGroupDescription("Industry"));
            //}
        }


        // ** implementation
        private async Task<List<Company>> LoadCompanies()
        {
            var list = new List<Company>();
            var uri = new Uri("ms-appx:///Model/Companies.txt");
            var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
            using (var sr = new StreamReader(await file.OpenStreamForReadAsync()))
            {
                // skip titles
                var line = sr.ReadLine();

                // read data
                for (line = sr.ReadLine(); line != null; line = sr.ReadLine())
                {
                    if (line.Trim() == string.Empty) continue;
                    var parts = line.Split('\t');

                    var c = new Company();
                    //c.Rank = list.Count + 1;
                    c.Name = parts[0];
                    c.Country = parts[1];
                    c.Industry = parts[2];
                    c.Sales = double.Parse(parts[3], NumberStyles.Any);
                    c.Profits = double.Parse(parts[4], NumberStyles.Any);
                    c.Assets = double.Parse(parts[5], NumberStyles.Any);
                    c.MarketValue = double.Parse(parts[6], NumberStyles.Any);

                    list.Add(c);
                }
            }
            return list;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    /// <summary>
    /// All amounts in US$ billions.
    /// </summary>
    public class Company
    {
        //public int Rank { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public string Industry { get; set; }
        public double Sales { get; set; }
        public double Profits { get; set; }
        public double Assets { get; set; }
        public double MarketValue { get; set; }
    }
}
