using DevExpress.Utils;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static lol_coder.Data.DataControl;

namespace lol_coder.Forms
{
    public partial class MatchForm : XtraForm
    {
        MainForm mainForm;

        public MatchForm(MainForm _mainForm)
        {
            InitializeComponent();

            mainForm = _mainForm; 
        }

        private void MatchForm_Shown(object sender, EventArgs e)
        {
            gridView1.OptionsBehavior.Editable = true;

            DataBind(true);
            gridView1.BestFitColumns();
            btnRemove_Click(btnRemove, new EventArgs());

            Font font = gridControl1.Font;
            foreach (AppearanceObject ap in gridView1.Appearance)
            {
                ap.Font = font;
            }

            if (gridView1.FormatConditions.Count > 0)
            {
                for (int i = 0; i < gridView1.FormatConditions.Count; i++)
                {
                    gridView1.FormatConditions[i].Appearance.Font = new Font(font.FontFamily, font.Size, gridView1.FormatConditions[i].Appearance.Font.Style);
                }
            }

            LoadMatch(); 
        } 

        private void DataBind(bool isFirst)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Match");
            dt.Columns.Add("Blue");
            dt.Columns.Add("RED");
            dt.Columns.Add("방제");

            if (isFirst)
            {
                dt.Rows.Add(new object[] { "123", "DRX", "DRX", "108211175222716701|game1" });
            }
            else
            {
                foreach(var m in mainForm.DC.Matchs)
                {
                    dt.Rows.Add(new object[] { m.MatchNumber, m.BlueTeam, m.RedTeam, m.RoomName });
                } 
            }
            

            gridControl1.DataSource = dt;
        }

        private void LoadMatch()
        {
            mainForm.DC.LoadMatchs();
            DataBind(false);
        }

        

        private void btnAdd_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow();            
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            gridView1.DeleteRow(gridView1.FocusedRowHandle);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            mainForm.DC.Matchs.Clear();

            foreach (DataRowView v in (DataView)gridView1.DataSource)
            {
                Match match = new Match();
                match.MatchNumber = (string)v[0];
                match.BlueTeam = (string)v[1];
                match.RedTeam = (string)v[2];
                match.RoomName = (string)v[3];

                mainForm.DC.Matchs.Add(match);
            }

            mainForm.DC.SaveMatchs();
        }

        
    }
}
