using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
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;

namespace lol_coder.Forms
{
    public partial class PlayerForm : XtraForm
    {
        MainForm mainForm;
        RepositoryItemComboBox positionComboBox;
        RepositoryItemToggleSwitch edit;

        public PlayerForm(MainForm _mainForm)
        {
            InitializeComponent();

            mainForm = _mainForm;

            mainForm.DC.LoadTeams();

            foreach(var t in mainForm.DC.Teams)
            {
                cmbTeam.Items.Add(t.TeamName);
            }
          
        }

        private void PlayerForm_Shown(object sender, EventArgs e)
        {
            InitGridControl();

            DataBind();

            positionComboBox = new RepositoryItemComboBox();
           
            gridView1.Columns[1].ColumnEdit = positionComboBox;
            
            InitCustomEdit();

            cmbTeam.SelectedIndex = 0;
        }

        private void InitGridControl()
        {
            //gridControl1.ShowGroupPanel = false;
            gridView1.OptionsBehavior.Editable = true; 
        }

        private void DataBind()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Position");
            dt.Columns.Add("IsPlaying",typeof(Boolean));

            dt.Rows.Add(new object[] { "도란", "탑", false });

            gridControl1.DataSource = dt;
        }

        private void InitCustomEdit()
        {
            DevExpressHelper.ClearComboBoxEditData(positionComboBox);
            DevExpressHelper.SetComboBoxEditData(positionComboBox, "탑", "정글", "미드", "원딜", "서폿"); 

            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, "Position", positionComboBox.Items[0]);
            
            positionComboBox.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;


            edit = new RepositoryItemToggleSwitch();
            gridControl1.RepositoryItems.Add(edit);            
            gridView1.Columns[2].ColumnEdit = edit;
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, "isPlaying", false);

            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);
                }
            }

            btnRemove_Click(btnRemove, new EventArgs());
        }

        #region Events

        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)
        {
            try
            {

                string team = cmbTeam.Text;

                int indexT = mainForm.DC.Teams.FindIndex(x => x.TeamName.Equals(team));

                if (mainForm.DC.Teams[indexT].Players != null)
                {
                    mainForm.DC.Teams[indexT].Players.Clear();
                }


                foreach (DataRowView v in (DataView)gridView1.DataSource)
                {
                    string name = (string)v[0];
                    string position = (string)v[1];
                    bool isPlay = false;

                    if (v[2].ToString() != "") isPlay = (bool)v[2];

                    mainForm.DC.Teams[indexT].Players.Add(new Tuple<string, string, bool>(name, position, isPlay));
                }

                mainForm.DC.SavePlayer(team);
            } catch(Exception ex)
            {
                MessageBox.Show("값을 입력하지 않은 선수가 있습니다..");
            }


            

        }

        private void cmbTeam_SelectedIndexChanged(object sender, EventArgs e)
        {            
            string path = Environment.CurrentDirectory + @"\Data\Team\" + cmbTeam.Text + ".json";

            for (int i = 0; i< gridView1.RowCount; i++)
            {
                gridView1.DeleteRow(0);
            }
            
            mainForm.DC.LoadPlayer(cmbTeam.Text);
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Position");
            dt.Columns.Add("IsPlaying", typeof(Boolean));

            for (int i = 0; i < mainForm.DC.Teams.Find(x => x.TeamName.Equals(cmbTeam.Text)).Players.Count; i++)
            {
                var players = mainForm.DC.Teams.Find(x => x.TeamName.Equals(cmbTeam.Text)).Players;  
                
                dt.Rows.Add(new object[] { players[i].Item1, players[i].Item2, players[i].Item3 }); 
            }
            gridControl1.DataSource = dt;
        }

        #endregion


    }

    public class DevExpressHelper
    {
        public static void ClearComboBoxEditData(RepositoryItemComboBox sourceControl) => sourceControl.Items.Clear();

        public static void SetComboBoxEditData(RepositoryItemComboBox sourceControl, params string[] itemValueArry)
        {
            foreach (string itemValue in itemValueArry)
            {
                sourceControl.Items.Add(itemValue);
            }
        }         
    }
}
