using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using DataModels.Enums;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace WinFormsApp
{
    public partial class FormStatistics : Form
    {
        ISupplyLogic _supplyLogic;
        public FormStatistics(ISupplyLogic supplyLogic)
        {
            InitializeComponent();
            _supplyLogic = supplyLogic;
        }

        private void FormStatistics_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            chart.Series.Clear();
            var list = _supplyLogic.ReadList(new SupplySearchModel()
            {
                DateStart = dateTimePickerTo.Value,
                DateEnd = dateTimePickerFrom.Value,
            });
            if (checkBoxPending.Checked)
            {
                var date = dateTimePickerTo.Value;
                chart.Series.Add("Pending");
                for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++)
                {
                    var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Pending).ToList().Count;
                    DataPoint dataPoint = new DataPoint();
                    dataPoint.XValue = i;
                    dataPoint.IsVisibleInLegend = false;
                    dataPoint.Label = $"{date.Year}.{date.Month}";
                    dataPoint.SetValueY(count);
                    chart.Series["Pending"].Points.Add(dataPoint);
                    date = date.AddMonths(1);
                }
            }
            if (checkBoxArriving.Checked) 
            {
                var date = dateTimePickerTo.Value;
                chart.Series.Add("Arriving");
                for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++)
                {
                    var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Arriving).ToList().Count;
                    DataPoint dataPoint = new DataPoint();
                    dataPoint.XValue = i;
                    dataPoint.IsVisibleInLegend = false;
                    dataPoint.Label = $"{date.Year}.{date.Month}";
                    dataPoint.SetValueY(count);
                    chart.Series["Arriving"].Points.Add(dataPoint);
                    date = date.AddMonths(1);
                }
            }
            if (checkBoxCompleted.Checked) 
            {
                var date = dateTimePickerTo.Value;
                chart.Series.Add("Completed");
                for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++)
                {
                    var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Completed).ToList().Count;
                    DataPoint dataPoint = new DataPoint();
                    dataPoint.XValue = i;
                    dataPoint.IsVisibleInLegend = false;
                    dataPoint.Label = $"{date.Year}.{date.Month}";
                    dataPoint.SetValueY(count);
                    chart.Series["Completed"].Points.Add(dataPoint);
                    date = date.AddMonths(1);
                }
            }
        }

        private void chart_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePickerFrom_ValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void dateTimePickerTo_ValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private int diff_month(DateTime d1, DateTime d2)
        {
            return (d1.Year - d2.Year) * 12 + d1.Month - d2.Month;
        }

        private void checkBoxPending_CheckedChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void checkBoxArriving_CheckedChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void checkBoxCompleted_CheckedChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
