using DevExpress.XtraEditors;
using LolDataRequestLib;
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.Frame
{
    public partial class RunePageFrame : UserControl
    {
        MainForm mainForm;
        DataTable dt = new DataTable();
        public RunePageFrame(MainForm _mainForm)
        {
            InitializeComponent();

            mainForm = _mainForm;
        }

        public void AllClear()
        {
            void removeImage(Control c)
            {
                foreach (Control v in c.Controls)
                {
                    if (v.GetType().ToString().Contains("PictureEdit"))
                    {
                        ((PictureEdit)v).Image = mainForm.imageReSize(40, 40, "");
                    }
                }
            }
            removeImage(groupC1);
            removeImage(gc8);
            
            
        }

        public void btnRefresh_Click(object sender, EventArgs e)
        {
            dt = mainForm.getRunData();

            mainForm.resultFrame.cmbPOG.Properties.Items.Clear();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //챔피언
                string champName = dt.Rows[i][2].ToString();

                if (i < 5) ((PictureEdit)groupC1.Controls["BP" + (i + 1)]).Image = mainForm.imageReSize(40, 40, getChampsPath(champName));
                else ((PictureEdit)groupC1.Controls["RP" + (i - 4)]).Image = mainForm.imageReSize(40, 40, getChampsPath(champName));

                if (i < 5) ((PictureEdit)gc8.Controls["BCC" + (i + 1)]).Image = mainForm.imageReSize(70, 70, getChampsPath(champName));
                else ((PictureEdit)gc8_1.Controls["pictureEdit" + (i - 4)]).Image = mainForm.imageReSize(70, 70, getChampsPath(champName));



                //룬
                string mainRune = dt.Rows[i][4].ToString();
                if (i < 5) ((PictureEdit)groupC1.Controls["pb" + (i + 1)]).Image = mainForm.imageReSize(40, 40, getRunePath(mainRune));
                else ((PictureEdit)groupC1.Controls["pr" + (i - 4)]).Image = mainForm.imageReSize(40, 40, getRunePath(mainRune));

                string subRune = dt.Rows[i][6].ToString();
                if (i < 5) ((PictureEdit)groupC1.Controls["pb" + (i + 1) + "_1"]).Image = mainForm.imageReSize(40, 40, getRunePath(subRune));
                else ((PictureEdit)groupC1.Controls["pr" + (i - 4) + "_1"]).Image = mainForm.imageReSize(40, 40, getRunePath(subRune));

                for (int j = 0; j < 6; j++)
                {
                    string rune = dt.Rows[i][j + 7].ToString();
                    ((PictureEdit)groupC1.Controls["s" + i + "p" + (j + 1)]).Image = mainForm.imageReSize(40, 40, getRunePath(rune));
                }
            }
        }

        private string getChampsPath(string fileName) => RES_FOLDER_PATH + @"Champs(140x140)\" + fileName + "_140140.png";
        private string getRunePath(string fileName)
        {

            return RES_FOLDER_PATH + @"Runes(140x140)\" + fileName + ".png";
        }

        private string RES_FOLDER_PATH = Environment.CurrentDirectory + @"\Resource\";


        private void btnRune_Click(object sender, EventArgs e)
        {
            if (!mainForm.TM.isConnected())
            {
                MessageBox.Show("Tornado와 송출 연결이 되지 않았습니다.");
                return;
            }

            if (mainForm.MainLayerButton((SimpleButton)sender)) mainForm.TM.DisplayRunes(dt);
        }

        private void kistone_Click(object sender, EventArgs e)
        {
            if (!mainForm.TM.isConnected())
            {
                MessageBox.Show("Tornado와 송출 연결이 되지 않았습니다.");
                return;
            }

            if (mainForm.MainLayerButton((SimpleButton)sender)) mainForm.TM.DisplayKeyStone(dt, mainForm.DC.BlueLiner, mainForm.DC.RedLiner);
        }

        private void ChampGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRefresh_Click(null, null);
        }

        private void WinRate_Click(object sender, EventArgs e)
        {
            if (mainForm.MainLayerButton((SimpleButton)sender))
            {
                string[] winRate = new string[] { tb1_1.Text, tb2_1.Text, tb3_1.Text, tb4_1.Text, tb5_1.Text };

                 mainForm.TM.DisplayChampWinRate(mainForm.DC.BlueLiner,winRate, true);
              
            }
        }

        private void WinRate2_Click(object sender, EventArgs e)
        {
            if (mainForm.MainLayerButton((SimpleButton)sender))
            {
                string[] winRate = new string[] { tb1_2.Text, tb2_2.Text, tb3_2.Text, tb4_2.Text, tb5_2.Text };

                mainForm.TM.DisplayChampWinRate(mainForm.DC.RedLiner, winRate, false);
            }
        }
    }
}
