using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wispro.sp.share
{
    public class ChartData
    {
        public string type { get; set; }

        public string CustomerType { get; set; }

        public string year { get; set; }

        public string CustomerYear { get; set; }

        public double value { get; set; }
    }

    public class ChartDatas
    {
        public List<ChartData> Datas { get; set; }

        public string Title { get; set; }

        public string ATitle { get; set; }

        public string BTitle { get; set; }

        public List<string> GetYear()
        {
            List<string> retColumns = new List<string>();

            foreach (var cData in Datas)
            {
                if (!retColumns.Contains(cData.year))
                {
                    retColumns.Add(cData.year);
                }
            }

            retColumns.Sort();

            return retColumns;
        }

        public List<string> GetSeries()
        {
            List<string> retColumns = new List<string>();
            foreach (var cData in Datas)
            {
                if (!retColumns.Contains(cData.type))
                {
                    retColumns.Add(cData.type);
                }
            }

            return retColumns;
        }

        public DataTable GetTableData()
        {
            DataTable retTable = new DataTable();
            List<string> Columns = GetYear();
            retTable.Columns.Add(ATitle);

            foreach (string col in Columns)
            {
                retTable.Columns.Add(col);
            }

            foreach (var cData in Datas)
            {
                int iIndex = Columns.IndexOf(cData.year);

                DataRow row = null;
                foreach (DataRow temRow in retTable.Rows)
                {
                    if (temRow[ATitle].ToString() == cData.type)
                    {
                        row = temRow;
                        break;
                    }
                }

                if (row != null)
                {
                    row[cData.year] = cData.value;
                }
                else
                {
                    row = retTable.NewRow();
                    row[ATitle] = cData.type;
                    row[cData.year] = cData.value;
                    retTable.Rows.Add(row);

                }
            }

            return retTable;
        }


    }
}
