using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GFATask
{
    public partial class ucAdminNewsFeeds : UserControl
    {
        public ucAdminNewsFeeds()
        {
            InitializeComponent();

            this.SetDefaultDesign();
            dgvNewsFeeds.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            List<ReadableNewsFeed> converted_newsfeedlist = Config.NewsFeeds.Select(w => new ReadableNewsFeed(w) ).ToList();
            DataTable dt = new DataTable();
            dt.Columns.AddRange(converted_newsfeedlist[0].GetType().GetProperties().Select(prop => new DataColumn(prop.Name, prop.PropertyType)).ToArray());
            converted_newsfeedlist.ForEach(w => dt.Rows.Add(w.Name, w.Aktiv, w.Symbol, w.Icon, w.Starttag, w.Startmonat, w.Endtag, w.Endmonat, w.Titel));
            dgvNewsFeeds.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            dgvNewsFeeds.DataSource = dt;
            dgvNewsFeeds.CellEndEdit += DgvWorldDays_CellEndEdit;
            dgvNewsFeeds.CellBeginEdit += DgvWorldDays_CellBeginEdit;
        }

        private void DgvWorldDays_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            //if(dgvNewsFeeds.Columns["Symbol"].Index == e.ColumnIndex || dgvNewsFeeds.Columns["Icon"].Index == e.ColumnIndex)
            //{
            //    var ofd = new OpenFileDialog() { Multiselect = false };
            //    if(ofd.ShowDialog() == DialogResult.OK)
            //    {
            //        dgvNewsFeeds.CurrentCell.Value = ofd.SafeFileName;
            //        dgvNewsFeeds.Rows[dgvNewsFeeds.CurrentRow.Index].Cells["Icon"].Value = new Bitmap(Image.FromStream(File.OpenRead(ofd.FileName)), new Size(16,16));
            //    }
            //}
        }
        

        private void DgvWorldDays_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dgvNewsFeeds.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange);
            dgvNewsFeeds.BindingContext[dgvNewsFeeds.DataSource].EndCurrentEdit();
        }

        private void abtnAnwenden_Click(object sender, EventArgs e)
        {
            List<Models.NewsFeed> return_newsfeedlist = ((DataTable)dgvNewsFeeds.DataSource).Rows.Cast<DataRow>().Select(w => new Models.NewsFeed() {
                Name = w[0].ToString(), 
                Enable = (bool)w[1],
                Icon = w[2] == null ? "" : w[2].ToString(),
                Begin = new Models.NewsFeed.Date() { Day = (int)w[4], Month = (int)w[5]}, 
                End = new Models.NewsFeed.Date() { Day = (int)w[6], Month = (int)w[7]},
                Title = w[8].ToString()
            }).ToList();
            Config.NewsFeeds = return_newsfeedlist;
            JSON.Save(JSONConfig.NewsFeeds);

            Forms.Main.UseNewsFeeds();
        }

        private void abtnVerwerfen_Click(object sender, EventArgs e)
        {

        }

        private void dgvWorldDays_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            
        }

        private void dgvNewsFeeds_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvNewsFeeds.Columns["Symbol"].Index == e.ColumnIndex || dgvNewsFeeds.Columns["Icon"].Index == e.ColumnIndex)
            {
                var ofd = new OpenFileDialog() { Multiselect = false };
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if(ofd.FileName == "[Kein Icon]")
                    {

                        return;
                    }

                    Creator.StreamCopy(ofd.FileName, Paths.NewsFeeds.Icons.Directory + ofd.SafeFileName, (fs) =>
                    {
                        dgvNewsFeeds[dgvNewsFeeds.Columns["Symbol"].Index, e.RowIndex].Value = ofd.SafeFileName == "[Kein Icon]" ? null : Path.GetFileName(fs.Name);
                        dgvNewsFeeds.Rows[dgvNewsFeeds.CurrentRow.Index].Cells["Icon"].Value = ofd.SafeFileName == "[Kein Icon]" ? null : new Bitmap(Image.FromStream(fs), new Size(16, 16));
                    });
                }
            }
        }
    }
}
