using DevExpress.XtraEditors;
using lol_coder.Log;
using LolDataRequestLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace lol_coder.Forms.Frame
{
    public partial class LiveCoderFrame : DevExpress.XtraEditors.XtraUserControl
    {
        Timer timerManual;
        MainForm mainForm;
        public LiveCoderFrame(MainForm _mainForm)
        {
            InitializeComponent();

            mainForm = _mainForm;

            Dragon_Load(new DataTable());

            timerManual = new Timer();
            timerManual.Interval = 1000;
            timerManual.Tick += new EventHandler(timerManual_Tick);

            R_EpicMonster.Tag = RES_FOLDER_PATH + @"\무력행사\EpicMonster_0.png";
            B_EpicMonster.Tag = RES_FOLDER_PATH + @"\무력행사\EpicMonster_0.png";
            R_FirstBlood.Tag = RES_FOLDER_PATH + @"\무력행사\FirstBlood_0.png";
            B_FirstBlood.Tag = RES_FOLDER_PATH + @"\무력행사\FirstBlood_0.png";
            R_Turret.Tag = RES_FOLDER_PATH + @"\무력행사\Turret_0.png";
            B_Turret.Tag = RES_FOLDER_PATH + @"\무력행사\Turret_0.png";

            set무력행사Image();

            radioGroup2.SelectedIndex = 1;
        }



        
         
        private void pic무력행사_Click(object sender, EventArgs e)
        {
            PictureEdit target = (PictureEdit)sender;

            if (((MouseEventArgs)e).Button == MouseButtons.Right)
            {
                if (target.Tag.ToString().Contains("_1"))
                {
                    target.Tag = target.Tag.ToString().Replace("_1", "_0");
                }
                else if (target.Name.Contains("Epic") || target.Name.Contains("FirstBlood"))
                {
                    if (target.Tag.ToString().Contains("_3")) target.Tag = target.Tag.ToString().Replace("_3", "_2");
                    else if (target.Tag.ToString().Contains("_2")) target.Tag = target.Tag.ToString().Replace("_2", "_1");
                }
            }
            else if (((MouseEventArgs)e).Button == MouseButtons.Left)
            {
                if (target.Tag.ToString().Contains("_0"))
                {
                    target.Tag = target.Tag.ToString().Replace("_0", "_1");
                }
                else if (target.Name.Contains("Epic") || target.Name.Contains("FirstBlood"))
                {
                    if (target.Tag.ToString().Contains("_1")) target.Tag = target.Tag.ToString().Replace("_1", "_2");
                    else if (target.Tag.ToString().Contains("_2")) target.Tag = target.Tag.ToString().Replace("_2", "_3");
                }
            } 

            set무력행사Image();
        }

        public void set무력행사Image()
        {
            try
            {
                R_EpicMonster.Image = new Bitmap(R_EpicMonster.Tag.ToString());
                B_EpicMonster.Image = new Bitmap(B_EpicMonster.Tag.ToString());

                R_FirstBlood.Image = new Bitmap(R_FirstBlood.Tag.ToString());
                B_FirstBlood.Image = new Bitmap(B_FirstBlood.Tag.ToString());

                R_Turret.Image = new Bitmap(R_Turret.Tag.ToString());
                B_Turret.Image = new Bitmap(B_Turret.Tag.ToString());

                if (mainForm.TM.isScoreDisplaying)
                {
                    string[] values = new string[] { mainForm.bscore.Text, txtBTeam.Text, mainForm.rscore.Text, txtRTeam.Text, mainForm.bscoreAll.Text, mainForm.rscoreAll.Text };
                    string[] values2 = new string[] { R_EpicMonster.Tag.ToString(), R_Turret.Tag.ToString(), R_FirstBlood.Tag.ToString(), B_EpicMonster.Tag.ToString(), B_Turret.Tag.ToString(), B_FirstBlood.Tag.ToString() };
                    mainForm.TM.DisplayScore(values, values2);
                }
            }
            catch(Exception ex)
            {
                mainForm.Log(ex.Message, LogWriter.LogType.Error);
            }
        }

        public void AllClear()
        {
            try
            {
                isAtakanTimerManual = false;
                isDragonTimerManual = false;
                isHeraldTimerManual = false;
                isHordeTimerManual = false;
                isBaronTimerManual = false;
                isElderBuffTimerManual = false;
                isBaronBuffTimerManual = false;
                isInhibitorBlueManual = new bool[] { false, false, false, false, false };
                isInhibitorRedManual = new bool[] { false, false, false, false, false };

                AllReset();
                AllOut();
                timerManual.Stop();
                Dragon_Load(new DataTable());

                BHorde.Text = "0";
                RHorde.Text = "0";
                BT.Text = "0";
                RT.Text = "0";

                textEdit1.Text = "";
                textEdit2.Text = "";
                textEdit3.Text = "";

                R_EpicMonster.Tag = RES_FOLDER_PATH + @"\무력행사\EpicMonster_0.png";
                B_EpicMonster.Tag = RES_FOLDER_PATH + @"\무력행사\EpicMonster_0.png";
                R_FirstBlood.Tag = RES_FOLDER_PATH + @"\무력행사\FirstBlood_0.png";
                B_FirstBlood.Tag = RES_FOLDER_PATH + @"\무력행사\FirstBlood_0.png";
                R_Turret.Tag = RES_FOLDER_PATH + @"\무력행사\Turret_0.png";
                B_Turret.Tag = RES_FOLDER_PATH + @"\무력행사\Turret_0.png";

                radioGroup3.SelectedIndex = 1;

                set무력행사Image();

                checkEdit1.Checked = false;
                checkEdit2.Checked = false;
                checkEdit3.Checked = false;
                checkEdit4.Checked = false;
                checkEdit5.Checked = false;
                checkEdit6.Checked = false;
                checkEdit7.Checked = false;
                checkEdit8.Checked = false;
                checkEdit9.Checked = false;
                checkEdit10.Checked = false;

                for (int i = 0; i < 5; i++)
                {
                    mainForm.TM.BQuest[i] = false;
                    mainForm.TM.RQuest[i] = false;
                }

                foreach(Control c in groupControl11.Controls)
                {
                    if (c is TextEdit)
                    {
                        c.Text = "0";
                    }
                }

                beforeGoldTower = -1;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }

        }

        public void AllReset()
        {
            TimerReset_Click(DTR, new EventArgs());
            TimerReset_Click(HTR, new EventArgs());
            TimerReset_Click(BIT1, new EventArgs());
            TimerReset_Click(BIT2, new EventArgs());
            TimerReset_Click(BIT3, new EventArgs());
            TimerReset_Click(BIT4, new EventArgs());
            TimerReset_Click(BIT5, new EventArgs());
            TimerReset_Click(BR, new EventArgs());
            TimerReset_Click(ER, new EventArgs());
            TimerReset_Click(BTR, new EventArgs());
            TimerReset_Click(RIT1, new EventArgs());
            TimerReset_Click(RIT2, new EventArgs());
            TimerReset_Click(RIT3, new EventArgs());
            TimerReset_Click(RIT4, new EventArgs());
            TimerReset_Click(RIT5, new EventArgs());
        }

        public void AllOut()
        {
            ScoreOUT_Click(null, null);
            DTOUT_Click(null, null);
            AtakanOUT_Click(null, null);
            HeraldOUT_Click(null, null);
            HordeOut_Click(null, null);
            ElderOUT_Click(null, null);
            Dragon_Out_Click(null, null);
            btnoutBtor_Click(null, null);
            RInOut_Click(null, null);
            BraonOUT_Click(null, null);
            BFOUT_Click(null, null); 
        }


        #region 상단스코어판

        private void ScoreIN_Click(object sender, EventArgs e)
        {
            try
            {
                string[] values = new string[] { mainForm.bscore.Text, txtBTeam.Text, mainForm.rscore.Text, txtRTeam.Text, mainForm.bscoreAll.Text, mainForm.rscoreAll.Text };
                string[] values2 = new string[] { R_EpicMonster.Tag.ToString(), R_Turret.Tag.ToString(), R_FirstBlood.Tag.ToString(), B_EpicMonster.Tag.ToString(), B_Turret.Tag.ToString(), B_FirstBlood.Tag.ToString() };
                mainForm.TM.DisplayScore(values, values2);
                ScoreIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }

        }

        private void ScoreOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutScore();
                ScoreIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }

        }

        #endregion


        #region 타워 골드 관련

        int beforeGoldTower = -1;

        private enum Role { Top, Jgl, Mid, Adc, Spt }

        public void 골드획득정보디버깅(DataTable dt)
        {
            //dataGridView1.DataSource = dt;

            if (dt.Rows.Count == beforeGoldTower) return;

            beforeGoldTower = dt.Rows.Count;
            

            // 1) 초기화(기존 값 남는 것 방지)
            ResetTGTextBoxes();

            const int PLATES_PER_TURRET = 5;

            // plateCount: "gainTeamID|tier|lane" -> 0..5
            var plateCount = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

            // turretDestroyed: "gainTeamID|tier|lane" -> true (타워가 파괴되면 plate=5 확정)
            var turretDestroyed = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            // 중복 제거(plate / turret 모두 recipient 분배로 여러 줄 들어올 수 있음)
            // dedupKey는 "towerOwnerTeam(가능하면 rawTeamId)|tier|lane|gameTime|x|z|source"
            var seenEvents = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            // role gold: "teamID|role" -> sumGold
            var goldByTeamRole = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);

            // team sum
            long blueSum = 0;
            long redSum = 0;

            foreach (DataRow dr in dt.Rows)
            {
                // 컬럼명 우선, 없으면 fallback index 사용
                // 기본 가정(필요 시 fallback 인덱스 조정):
                // 0: source, 1: teamID, 2: recipientId, 3: amount, 4: lane, 5: turretTier, 6: gameTime, 7:x, 8:z
                string source = GetStr(dr, "source", 0);
                if (string.IsNullOrWhiteSpace(source)) continue;

                int rawTeamId = GetInt(dr, "teamID", 1);      // 타워 소유팀(피해팀)일 가능성이 큼(그래서 집계에는 안 씀)
                int recipientId = GetInt(dr, "recipientId", 2);
                int amount = GetInt(dr, "amount", 3);

                string lane = NormalizeLane(GetStr(dr, "lane", 4));
                string tier = NormalizeTier(GetStr(dr, "turretTier", 5));

                int gameTime = GetInt(dr, "gameTime", 6);
                int x = GetInt(dr, "x", 7);
                int z = GetInt(dr, "z", 8);

                // 2) 획득팀/포지션은 recipientId로 확정
                int gainTeamId = TeamFromRecipientId(recipientId);      // 100(블루) / 200(레드) / 0(모름)
                if (gainTeamId == 0)
                {
                    // recipientId가 없으면(0/누락) 판단 불가
                    // 이 케이스는 데이터 구조가 다르거나 예외 이벤트일 수 있음 -> 안전하게 스킵
                    continue;
                }

                // 넥서스 타워는 plate 없음(요구사항)
                bool isNexusTier = tier.Equals("nexus", StringComparison.OrdinalIgnoreCase);

                // 3) 이벤트 중복 제거 키(plate/turret는 분배 때문에 같은 이벤트가 여러 줄로 들어올 수 있음)
                // rawTeamId가 타워 소유팀(피해팀)일 가능성이 높아 dedup 안정화에 도움
                int ownerKeyTeam = (rawTeamId == 100 || rawTeamId == 200) ? rawTeamId : gainTeamId;

                string dedupKey = $"{ownerKeyTeam}|{tier}|{lane}|{gameTime}|{x}|{z}|{source}";
                bool isFirstSeen = seenEvents.Add(dedupKey);

                // 4) 골드 집계(플레이트 + 타워 파괴)
                if (source.Equals("turretPlate", StringComparison.OrdinalIgnoreCase) &&                    amount > 0)
                {
                    // 팀 합
                    if (gainTeamId == 100) blueSum += amount;
                    else if (gainTeamId == 200) redSum += amount;

                    // 역할(포지션) 합
                    Role role = RoleFromRecipientId(recipientId);
                    Add(goldByTeamRole, $"{gainTeamId}|{role}", amount);
                }

                // 5) 플레이트 카운트
                // - turretPlate: 플레이트 1장씩 누적(중복 제거된 경우만)
                // - turret: 타워 파괴 발생 시, 해당 타워(넥서스 제외)는 plate=5 확정(중복 제거된 경우만)
                if (!isNexusTier && !string.IsNullOrEmpty(lane))
                {
                    if (source.Equals("turretPlate", StringComparison.OrdinalIgnoreCase))
                    {
                        if (isFirstSeen)
                        {
                            string plateKey = $"{gainTeamId}|{tier}|{lane}";
                            int cur = GetInt(plateCount, plateKey);
                            cur = Math.Min(PLATES_PER_TURRET, cur + 1);
                            plateCount[plateKey] = cur;
                        }
                    }
                    else if (source.Equals("turret", StringComparison.OrdinalIgnoreCase))
                    {
                        if (isFirstSeen)
                        {
                            string turretKey = $"{gainTeamId}|{tier}|{lane}";
                            turretDestroyed.Add(turretKey);
                        }
                    }
                }
            }

            // 6) 타워 파괴된 타워는 plate=5로 확정(넥서스 제외는 이미 위에서 걸렀음)
            foreach (var key in turretDestroyed)
            {
                plateCount[key] = PLATES_PER_TURRET;
            }

            // 7) UI 반영(plate)
            ApplyPlateTextBoxes(plateCount);

            // 8) UI 반영(role gold)
            ApplyRoleGoldTextBoxes(goldByTeamRole);

            // 9) UI 반영(team sum)
            txtTG_B_total_gold.Text = blueSum.ToString();
            txtTG_R_total_gold.Text = redSum.ToString();


            if (mainForm.TM.isTowerGoldDisplaying)
            {
                List<string[]> strs = new List<string[]>();
                foreach (Control c in groupControl11.Controls)
                {
                    if (c is TextEdit) strs.Add(new string[] { c.Name.Replace("txtTG_", ""), c.Text });
                }

                mainForm.TM.DisplayGoldTower(strs);
            }            
        }

        // -----------------------------
        // UI Helpers
        // -----------------------------
        private void ResetTGTextBoxes()
        {
            // plate boxes (Blue)
            SetText(txtTG_B_outer_top, "0");
            SetText(txtTG_B_outer_mid, "0");
            SetText(txtTG_B_outer_bot, "0");
            SetText(txtTG_B_inner_top, "0");
            SetText(txtTG_B_inner_mid, "0");
            SetText(txtTG_B_inner_bot, "0");
            SetText(txtTG_B_base_top, "0");
            SetText(txtTG_B_base_mid, "0");
            SetText(txtTG_B_base_bot, "0");

            // plate boxes (Red)
            SetText(txtTG_R_outer_top, "0");
            SetText(txtTG_R_outer_mid, "0");
            SetText(txtTG_R_outer_bot, "0");
            SetText(txtTG_R_inner_top, "0");
            SetText(txtTG_R_inner_mid, "0");
            SetText(txtTG_R_inner_bot, "0");
            SetText(txtTG_R_base_top, "0");
            SetText(txtTG_R_base_mid, "0");
            SetText(txtTG_R_base_bot, "0");

            // role gold boxes (Blue)
            SetText(txtTG_B_top, "0");
            SetText(txtTG_B_jgl, "0");
            SetText(txtTG_B_mid, "0");
            SetText(txtTG_B_adc, "0");
            SetText(txtTG_B_spt, "0");

            // role gold boxes (Red)
            SetText(txtTG_R_top, "0");
            SetText(txtTG_R_jgl, "0");
            SetText(txtTG_R_mid, "0");
            SetText(txtTG_R_adc, "0");
            SetText(txtTG_R_spt, "0");

            // team sum
            SetText(txtTG_B_total_gold, "0");
            SetText(txtTG_R_total_gold, "0");
        }

        private void ApplyPlateTextBoxes(Dictionary<string, int> plateCount)
        {
            // key 형식: "teamID|tier|lane"
            // tier: outer/inner/base, lane: top/mid/bot
            SetText(txtTG_B_outer_top, GetPlate(plateCount, 100, "outer", "top"));
            SetText(txtTG_B_outer_mid, GetPlate(plateCount, 100, "outer", "mid"));
            SetText(txtTG_B_outer_bot, GetPlate(plateCount, 100, "outer", "bot"));

            SetText(txtTG_B_inner_top, GetPlate(plateCount, 100, "inner", "top"));
            SetText(txtTG_B_inner_mid, GetPlate(plateCount, 100, "inner", "mid"));
            SetText(txtTG_B_inner_bot, GetPlate(plateCount, 100, "inner", "bot"));

            SetText(txtTG_B_base_top, GetPlate(plateCount, 100, "base", "top"));
            SetText(txtTG_B_base_mid, GetPlate(plateCount, 100, "base", "mid"));
            SetText(txtTG_B_base_bot, GetPlate(plateCount, 100, "base", "bot"));

            SetText(txtTG_R_outer_top, GetPlate(plateCount, 200, "outer", "top"));
            SetText(txtTG_R_outer_mid, GetPlate(plateCount, 200, "outer", "mid"));
            SetText(txtTG_R_outer_bot, GetPlate(plateCount, 200, "outer", "bot"));

            SetText(txtTG_R_inner_top, GetPlate(plateCount, 200, "inner", "top"));
            SetText(txtTG_R_inner_mid, GetPlate(plateCount, 200, "inner", "mid"));
            SetText(txtTG_R_inner_bot, GetPlate(plateCount, 200, "inner", "bot"));

            SetText(txtTG_R_base_top, GetPlate(plateCount, 200, "base", "top"));
            SetText(txtTG_R_base_mid, GetPlate(plateCount, 200, "base", "mid"));
            SetText(txtTG_R_base_bot, GetPlate(plateCount, 200, "base", "bot"));
        }

        private void ApplyRoleGoldTextBoxes(Dictionary<string, long> goldByTeamRole)
        {
            SetText(txtTG_B_top, GetRoleGold(goldByTeamRole, 100, Role.Top));
            SetText(txtTG_B_jgl, GetRoleGold(goldByTeamRole, 100, Role.Jgl));
            SetText(txtTG_B_mid, GetRoleGold(goldByTeamRole, 100, Role.Mid));
            SetText(txtTG_B_adc, GetRoleGold(goldByTeamRole, 100, Role.Adc));
            SetText(txtTG_B_spt, GetRoleGold(goldByTeamRole, 100, Role.Spt));

            SetText(txtTG_R_top, GetRoleGold(goldByTeamRole, 200, Role.Top));
            SetText(txtTG_R_jgl, GetRoleGold(goldByTeamRole, 200, Role.Jgl));
            SetText(txtTG_R_mid, GetRoleGold(goldByTeamRole, 200, Role.Mid));
            SetText(txtTG_R_adc, GetRoleGold(goldByTeamRole, 200, Role.Adc));
            SetText(txtTG_R_spt, GetRoleGold(goldByTeamRole, 200, Role.Spt));
        }

        private static string GetPlate(Dictionary<string, int> dict, int teamId, string tier, string lane)
        {
            string key = $"{teamId}|{tier}|{lane}";
            return dict.TryGetValue(key, out int v) ? v.ToString() : "0";
        }

        private static string GetRoleGold(Dictionary<string, long> dict, int teamId, Role role)
        {
            string key = $"{teamId}|{role}";
            return dict.TryGetValue(key, out long v) ? v.ToString() : "0";
        }

        private static void SetText(Control c, string text)
        {
            if (c != null) c.Text = text;
        }

        // -----------------------------
        // Mapping (recipientId -> team/role)
        // -----------------------------
        private static int TeamFromRecipientId(int recipientId)
        {
            if (recipientId >= 1 && recipientId <= 5) return 100;   // Blue
            if (recipientId >= 6 && recipientId <= 10) return 200;  // Red
            return 0;
        }

        private static Role RoleFromRecipientId(int recipientId)
        {
            // 1~5: Blue, 6~10: Red
            int idx = recipientId;
            if (idx >= 6 && idx <= 10) idx -= 5;

            switch (idx)
            {
                case 1: return Role.Top;
                case 2: return Role.Jgl;
                case 3: return Role.Mid;
                case 4: return Role.Adc;
                case 5: return Role.Spt;
                default: return Role.Top;
            }
        }

        // -----------------------------
        // Normalization (tier/lane)
        // -----------------------------
        private static string NormalizeLane(string lane)
        {
            if (string.IsNullOrWhiteSpace(lane)) return "";

            lane = lane.Trim().ToLowerInvariant();

            // 데이터가 "top", "mid", "bot"이 아닐 수도 있어서 보정
            if (lane.Contains("top")) return "top";
            if (lane.Contains("mid")) return "mid";
            if (lane.Contains("bot") || lane.Contains("bottom")) return "bot";

            return lane;
        }

        private static string NormalizeTier(string tier)
        {
            if (string.IsNullOrWhiteSpace(tier)) return "";

            tier = tier.Trim().ToLowerInvariant();

            if (tier.Contains("outer")) return "outer";
            if (tier.Contains("inner")) return "inner";
            if (tier.Contains("base")) return "base";
            if (tier.Contains("nexus")) return "nexus";

            return tier;
        }

        // -----------------------------
        // Data Helpers
        // -----------------------------
        private static string GetStr(DataRow dr, string colName, int fallbackIndex)
        {
            if (dr.Table.Columns.Contains(colName))
                return dr[colName]?.ToString() ?? "";

            if (fallbackIndex >= 0 && fallbackIndex < dr.ItemArray.Length)
                return dr[fallbackIndex]?.ToString() ?? "";

            return "";
        }

        private static int GetInt(DataRow dr, string colName, int fallbackIndex)
        {
            var s = GetStr(dr, colName, fallbackIndex);
            return int.TryParse(s, out int v) ? v : 0;
        }

        private static int GetInt(Dictionary<string, int> dict, string key)
        {
            return dict.TryGetValue(key, out int v) ? v : 0;
        }

        private static void Add(Dictionary<string, long> dict, string key, long add)
        {
            dict.TryGetValue(key, out long cur);
            dict[key] = cur + add;
        }

        #endregion



        #region 드래곤


        static string RES_FOLDER_PATH = Environment.CurrentDirectory + @"\Res\";

        public void Dragon_Load(DataTable dt)
        {
            try
            {
                int blueIndex = 0;
                int redIndex = 0;

                mainForm.TM.bbufD[0] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.bbufD[1] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.bbufD[2] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.bbufD[3] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.bbufD[4] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.bbufD[5] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.rbufD[0] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.rbufD[1] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.rbufD[2] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.rbufD[3] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.rbufD[4] = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.rbufD[5] = RES_FOLDER_PATH + "CLEAR.png";

                mainForm.TM.bAtakhan = RES_FOLDER_PATH + "CLEAR.png";
                mainForm.TM.rAtakhan = RES_FOLDER_PATH + "CLEAR.png";
                string setObjectImg(string fileName) => Environment.CurrentDirectory + @"\Resource\MonsterRename\" + fileName + ".png";


                foreach (DataRow dr in dt.Rows)
                {
                    if (dr[0].ToString().Equals("dragon"))
                    {
                        if (dr[2].ToString().Equals("블루"))
                        {
                            mainForm.TM.bbufD[blueIndex] = Dragon_Icon(dr[1].ToString());
                            blueIndex++;
                        }
                        else
                        {
                            mainForm.TM.rbufD[redIndex] = Dragon_Icon(dr[1].ToString());
                            redIndex++;
                        }

                        if (chkDragonBugFix.Checked)
                        {
                            if (blueIndex >3 || redIndex > 3)
                            {
                                dt1.Text = "06";
                                dt2.Text = "00";
                            }
                            else
                            {
                                dt1.Text = "05";
                                dt2.Text = "00";
                            } 

                        }
                    }
                    else if (dr[0].ToString().Contains("Atakhan"))
                    {
                        if (Convert.ToInt32(mainForm.NowGameTime(true)) > 10)
                        {
                            if (dr[2].ToString().Equals("블루")) mainForm.TM.bAtakhan = setObjectImg(dr[0].ToString());
                            else mainForm.TM.rAtakhan = setObjectImg(dr[0].ToString());
                        }                        
                    }
                }

                BDragon1.Image = mainForm.imageReSize(30, 30, mainForm.TM.bbufD[0]);
                BDragon2.Image = mainForm.imageReSize(30, 30, mainForm.TM.bbufD[1]);
                BDragon3.Image = mainForm.imageReSize(30, 30, mainForm.TM.bbufD[2]);
                BDragon4.Image = mainForm.imageReSize(30, 30, mainForm.TM.bbufD[3]);
                BDragon5.Image = mainForm.imageReSize(30, 30, mainForm.TM.bbufD[4]);
                BDragon6.Image = mainForm.imageReSize(30, 30, mainForm.TM.bbufD[5]);
                RDragon1.Image = mainForm.imageReSize(30, 30, mainForm.TM.rbufD[0]);
                RDragon2.Image = mainForm.imageReSize(30, 30, mainForm.TM.rbufD[1]);
                RDragon3.Image = mainForm.imageReSize(30, 30, mainForm.TM.rbufD[2]);
                RDragon4.Image = mainForm.imageReSize(30, 30, mainForm.TM.rbufD[3]);
                RDragon5.Image = mainForm.imageReSize(30, 30, mainForm.TM.rbufD[4]);
                RDragon6.Image = mainForm.imageReSize(30, 30, mainForm.TM.rbufD[5]);

                BAtakhan.Image = mainForm.imageReSize(30, 30, mainForm.TM.bAtakhan);
                RAtakhan.Image = mainForm.imageReSize(30, 30, mainForm.TM.rAtakhan);


            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }

        }

        public string Dragon_Icon(string str)
        {
            string value = string.Empty;
            if (str.ToLower() == "infernal")
            {
                value = RES_FOLDER_PATH + "Infernal.png";
            }
            else if (str.ToLower() == "earth")
            {
                value = RES_FOLDER_PATH + "Earth.png";
            }
            else if (str.ToLower() == "ocean")
            {
                value = RES_FOLDER_PATH + "Ocean.png";
            }
            else if (str.ToLower() == "wind")
            {
                value = RES_FOLDER_PATH + "Wind.png";
            }
            else if (str.ToLower() == "elder")
            {
                value = RES_FOLDER_PATH + "Elder.png";
            }
            else if (str.ToLower() == "fire")
            {
                value = RES_FOLDER_PATH + "fire.png";
            }
            else if (str.ToLower() == "water")
            {
                value = RES_FOLDER_PATH + "Ocean.png";
            }
            else if (str.ToLower() == "air")
            {
                value = RES_FOLDER_PATH + "Wind.png";
            }
            else if (str.ToLower() == "hextech")
            {
                value = RES_FOLDER_PATH + "Hextech.png";
            }
            else if (str.ToLower() == "chemtech")
            {
                value = RES_FOLDER_PATH + "Chemtech.png";
            }
            else
            {
                value = RES_FOLDER_PATH + "CLEAR.png";
            }
            return value;
        }


        private void Dragon_IN_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.DisplayDragon();
                Dragon_IN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void Dragon_Out_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutDragon();
                Dragon_IN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        string targetManualDragon = "";
        private void picDragon_Click(object sender, EventArgs e)
        {
            if (!ck2.Checked) return;

            ContextMenu custommenu = new ContextMenu();

            targetManualDragon= ((PictureEdit)sender).Name;


            custommenu.MenuItems.Add("Infernal", new EventHandler(ContextMenu_Dragon_Click));
            custommenu.MenuItems.Add("Earth", new EventHandler(ContextMenu_Dragon_Click));
            custommenu.MenuItems.Add("Ocean", new EventHandler(ContextMenu_Dragon_Click));
            custommenu.MenuItems.Add("Wind", new EventHandler(ContextMenu_Dragon_Click));
            custommenu.MenuItems.Add("Hextech", new EventHandler(ContextMenu_Dragon_Click));
            custommenu.MenuItems.Add("Chemtech", new EventHandler(ContextMenu_Dragon_Click));
            custommenu.MenuItems.Add("Elder", new EventHandler(ContextMenu_Dragon_Click));
            custommenu.MenuItems.Add("CLEAR", new EventHandler(ContextMenu_Dragon_Click));

            ((PictureEdit)sender).ContextMenu = custommenu;
        }

        private void ContextMenu_Dragon_Click(object sender, System.EventArgs e)
        {
            try
            {
                //이전에 선택한 이미지
                PictureEdit target = (PictureEdit)gc10.Controls[targetManualDragon];
                string str = ((System.Windows.Forms.MenuItem)sender).Text;

                if (str == "CLEAR")
                    target.Image = Image.FromFile(RES_FOLDER_PATH + "CLEAR.png");
                else
                    target.Image = mainForm.imageReSize(30, 30, RES_FOLDER_PATH + str + ".png");

                int index = Convert.ToInt32(target.Name.Substring(7));

                if (target.Name.Contains("BDragon"))
                    mainForm.TM.bbufD[index] = Dragon_Icon(str);
                else
                    mainForm.TM.rbufD[index] = Dragon_Icon(str);

                if (mainForm.TM.isDisplayDragon) mainForm.TM.DisplayDragon();
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion


        #region 수동 타이머관련

        bool isAtakanTimerManual = false;
        bool isDragonTimerManual = false;
        bool isHeraldTimerManual = false;
        bool isHordeTimerManual = false;
        bool isBaronTimerManual = false;
        bool isElderBuffTimerManual = false;
        bool isBaronBuffTimerManual = false;
        bool[] isInhibitorBlueManual = new bool[] { false, false, false, false, false };
        bool[] isInhibitorRedManual = new bool[] { false, false, false, false, false };


        public void timerManual_Tick(object sender, EventArgs e)
        {
            bool RemainTimeMethod(TextEdit txtMIN, TextEdit txtSEC, bool isLiveShow)
            {
                try
                {
                    int sec = Convert.ToInt32(txtMIN.Text) * 60 + Convert.ToInt32(txtSEC.Text);
                    if (sec == 0) return false;
                    sec--;

                    txtMIN.Text = mainForm.GetTime((sec / 60).ToString());
                    txtSEC.Text = mainForm.GetTime((sec % 60).ToString());

                    if (isLiveShow)
                    {
                        if (txtMIN.Text == "00" && txtSEC.Text == "00")
                        {
                            txtSEC.Text = "LIVE";
                            return false;
                        }
                    }

                   

                    return true;
                }
                catch(Exception ex)
                {
                    return false;
                }
            }

            try
            {
                if (isAtakanTimerManual)
                {
                    if (!RemainTimeMethod(at1, at2, true)) isAtakanTimerManual = false;
                }
                if (isHeraldTimerManual)
                {
                    if (!RemainTimeMethod(ht1, ht2, true)) isHeraldTimerManual = false;
                }
                if (isHordeTimerManual)
                {
                    if (!RemainTimeMethod(hordet1, hordet2, true)) isHordeTimerManual = false;
                }
                if (isDragonTimerManual)
                {
                    if (!RemainTimeMethod(dt1, dt2, true)) isDragonTimerManual = false;
                }
                if (isBaronTimerManual)
                {
                    if (!RemainTimeMethod(bt1, bt2, true)) isBaronTimerManual = false;
                }
                if (isElderBuffTimerManual)
                {
                    if (!RemainTimeMethod(ed1, ed2, false)) isElderBuffTimerManual = false;
                }
                if (isBaronBuffTimerManual)
                {
                    if (!RemainTimeMethod(br1, br2, false)) isBaronBuffTimerManual = false;
                }
                if (isInhibitorBlueManual[0])
                {
                    if (!RemainTimeMethod(bbt1, bbt2, false)) isInhibitorBlueManual[0] = false;
                }

                if (isInhibitorBlueManual[1])
                {
                    if (!RemainTimeMethod(bbt3, bbt4, false)) isInhibitorBlueManual[1] = false;
                }
                if (isInhibitorBlueManual[2])
                {
                    if (!RemainTimeMethod(bbt5, bbt6, false)) isInhibitorBlueManual[2] = false;
                }
                if (isInhibitorBlueManual[3])
                {
                    if (!RemainTimeMethod(bbt7, bbt8, false)) isInhibitorBlueManual[3] = false;
                }
                if (isInhibitorBlueManual[4])
                {
                    if (!RemainTimeMethod(bbt9, bbt10, false)) isInhibitorBlueManual[4] = false;
                }


                if (isInhibitorRedManual[0])
                {
                    if (!RemainTimeMethod(rbt1, rbt2, false)) isInhibitorRedManual[0] = false;
                }
                if (isInhibitorRedManual[1])
                {
                    if (!RemainTimeMethod(rbt3, rbt4, false)) isInhibitorRedManual[1] = false;
                }
                if (isInhibitorRedManual[2])
                {
                    if (!RemainTimeMethod(rbt5, rbt6, false)) isInhibitorRedManual[2] = false;
                }
                if (isInhibitorRedManual[3])
                {
                    if (!RemainTimeMethod(rbt7, rbt8, false)) isInhibitorRedManual[3] = false;
                }
                if (isInhibitorRedManual[4])
                {
                    if (!RemainTimeMethod(rbt9, rbt10, false)) isInhibitorRedManual[4] = false;
                }


                if (mainForm.TM.isDisplayInhibitorBlue)
                {
                    mainForm.TM.DisplayInhibitor(bbt1.Text, bbt2.Text, bbt3.Text, bbt4.Text, bbt5.Text, bbt6.Text,bbt7.Text, bbt8.Text, bbt9.Text, bbt10.Text, true); 
                } 
                if (mainForm.TM.isDisplayInhibitorRed)
                {
                    mainForm.TM.DisplayInhibitor(rbt1.Text, rbt2.Text, rbt3.Text, rbt4.Text, rbt5.Text, rbt6.Text, rbt7.Text, rbt8.Text, rbt9.Text, rbt10.Text, false);
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void checkBoxManual_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == ck10)
            {
                if (!ck10.Checked) isAtakanTimerManual = false;
            }
            if (sender == ck1)
            {
                if (!ck1.Checked) isHeraldTimerManual = false;
                //HTS.Visible = ck1.Checked;                HTR.Visible = ck1.Checked;
            }
            if (sender == ck9)
            {
                if (!ck9.Checked) isHordeTimerManual = false;                 
            }
            if (sender == ck3)
            {
                if (!ck3.Checked) isDragonTimerManual = false;
                //DTS.Visible = ck3.Checked;                DTR.Visible = ck3.Checked;
            }
            if (sender == ck4)
            {
                if (!ck4.Checked) isBaronTimerManual = false;
                //BTS.Visible = ck4.Checked;                BTR.Visible = ck4.Checked;                BTR2.Visible = ck4.Checked;
            }
            if (sender == ck7)
            {
                if (!ck7.Checked) isElderBuffTimerManual = false;
                //ES.Visible = ck7.Checked;                ER.Visible = ck7.Checked; 
            }
            if (sender == ck8)
            {
                if (!ck8.Checked) isBaronBuffTimerManual = false;
                //BS.Visible = ck8.Checked;                BR.Visible = ck8.Checked;
            }
            if (sender == ck5)
            {
                if (!ck5.Checked) isInhibitorBlueManual = new bool[] { false, false, false, false, false };
                //BIS1.Visible = ck5.Checked;                BIT1.Visible = ck5.Checked;
                //BIS2.Visible = ck5.Checked;                BIT2.Visible = ck5.Checked;
                //BIS3.Visible = ck5.Checked;                BIT3.Visible = ck5.Checked;
            }
            if (sender == ck6)
            {
                if (!ck6.Checked) isInhibitorRedManual = new bool[] { false, false, false, false, false };
                //RIS1.Visible = ck6.Checked;               RIT1.Visible = ck6.Checked;
                //RIS2.Visible = ck6.Checked;                RIT2.Visible = ck6.Checked;
                //RIS3.Visible = ck6.Checked;                RIT3.Visible = ck6.Checked;
            }
        }

        private void TimerStart_Click(object sender, EventArgs e)
        {
            if (sender == ATS && ck10.Checked) isAtakanTimerManual = true;
            if (sender == HTS && ck1.Checked) isHeraldTimerManual = true;
            if (sender == HordeTS && ck9.Checked) isHordeTimerManual = true;
            if (sender == DTS && ck3.Checked) isDragonTimerManual = true;
            if (sender == BTS && ck4.Checked ) isBaronTimerManual = true;
            if (sender == ES && ck7.Checked) isElderBuffTimerManual = true;
            if (sender == BS && ck8.Checked) isBaronBuffTimerManual = true;

            if (ck5.Checked)
            {
                if (sender == BIS1) isInhibitorBlueManual[0] = true;
                if (sender == BIS2) isInhibitorBlueManual[1] = true;
                if (sender == BIS3) isInhibitorBlueManual[2] = true;
                if (sender == BIS4) isInhibitorBlueManual[3] = true;
                if (sender == BIS5) isInhibitorBlueManual[4] = true;
            }

            if (ck6.Checked)
            {
                if (sender == RIS1) isInhibitorRedManual[0] = true;
                if (sender == RIS2) isInhibitorRedManual[1] = true;
                if (sender == RIS3) isInhibitorRedManual[2] = true;
                if (sender == RIS4) isInhibitorRedManual[3] = true;
                if (sender == RIS5) isInhibitorRedManual[4] = true;
            }

            if (!timerManual.Enabled) timerManual.Start();
        }

        private void TimerReset_Click(object sender, EventArgs e)
        {
            if (sender == ATR) { at1.Text = "20"; at2.Text = "00"; }
            if (sender == HTR) { ht1.Text = "14"; ht2.Text = "00"; }
            if (sender == HordeTR) { hordet1.Text = "05"; hordet2.Text = "00"; }
            if (sender == DTR) { dt1.Text = "05"; dt2.Text = "00"; }
            if (sender == BTR) { bt1.Text = "25"; bt2.Text = "00"; }
            if (sender == BTR2) { bt1.Text = "06"; bt2.Text = "00"; }
            if (sender == ER) { ed1.Text = "02"; ed2.Text = "30"; }
            if (sender == BR) { br1.Text = "03"; br2.Text = "30"; }
            if (sender == BIT1) { bbt1.Text = "05"; bbt2.Text = "00"; }
            if (sender == BIT2) { bbt3.Text = "05"; bbt4.Text = "00"; }
            if (sender == BIT3) { bbt5.Text = "05"; bbt6.Text = "00"; }
            if (sender == BIT4) { bbt7.Text = "03"; bbt8.Text = "00"; }
            if (sender == BIT5) { bbt9.Text = "03"; bbt10.Text = "00"; }

            if (sender == RIT1) { rbt1.Text = "05"; rbt2.Text = "00"; }
            if (sender == RIT2) { rbt3.Text = "05"; rbt4.Text = "00"; }
            if (sender == RIT3) { rbt5.Text = "05"; rbt6.Text = "00"; }
            if (sender == RIT4) { rbt7.Text = "03"; rbt8.Text = "00"; }
            if (sender == RIT5) { rbt9.Text = "03"; rbt10.Text = "00"; }

        }

        #endregion



        #region 용 타이머

        private void DTIN_Click(object sender, EventArgs e)
        {
            try
            {
                if (ComboRow.SelectedIndex < 0)
                {
                    MessageBox.Show("용이 선택되지 않았습니다.");
                    return;
                }

                if (mainForm.TM.isDisplayElderTimer)
                {
                    MessageBox.Show("용과 장로 버프는 한번에 표출할 수 없습니다.");
                    return;
                }

                string dragon = (string)ComboRow.SelectedItem;
                string dragonTime;

                if (dt1.Text == "00" && dt2.Text == "LIVE")
                    dragonTime = "LIVE";
                else
                    dragonTime = dt1.Text + ":" + dt2.Text;

                mainForm.TM.DisplayDragonTime(true, dragonTime, dragon);

                DTIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void dt2_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayDragonTimer)
                {
                    string dragon = (string)ComboRow.SelectedItem;
                    string dragonTime = "";

                    if (dt1.Text == "00" && dt2.Text == "LIVE")
                        dragonTime = "LIVE";
                    else
                        dragonTime = dt1.Text + ":" + dt2.Text;

                    mainForm.TM.DisplayDragonTime(false, dragonTime, dragon);
                }
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }


        private void DTOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutDragonTime();
                DTIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }



        #endregion


        #region 장로 타이머

        private void ElderIN_Click(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayDragonTimer)
                {
                    MessageBox.Show("용과 장로 버프는 한번에 표출할 수 없습니다.");
                    return;
                }

                bool isBlue = ElderGroup.SelectedIndex == 0 ? true : false;
                mainForm.TM.DisplayElderTime(true, ed1.Text + ":" + ed2.Text, isBlue);

                ElderIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void ElderOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutElderTime();
                ElderIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion


        #region 전령 타이머

        private void HeraldIN_Click(object sender, EventArgs e)
        {
            try
            {
                string heraldTime = "";

                if (ht1.Text == "00" && ht2.Text == "LIVE")
                    heraldTime = "LIVE";
                else
                    heraldTime = ht1.Text + ":" + ht2.Text;

                mainForm.TM.DisplayHeraldTime(true, heraldTime);
                HeraldIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void ht2_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayHeraldTimer)
                {
                    string heraldTime = "";

                    if (ht1.Text == "00" && ht2.Text == "LIVE")
                        heraldTime = "LIVE";
                    else
                        heraldTime = ht1.Text + ":" + ht2.Text;

                    mainForm.TM.DisplayHeraldTime(false, heraldTime);
                }
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void HeraldOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutHeraldTime();
                HeraldIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }


        #endregion

        #region 공허유충 타이머

  

        private void HordeIN_Click(object sender, EventArgs e)
        {
            try
            {
                string hordeTime = "";

                if (hordet1.Text == "00" && hordet2.Text == "LIVE")
                    hordeTime = "LIVE";
                else
                    hordeTime = hordet1.Text + ":" + hordet2.Text;

                mainForm.TM.DisplayHordeTime(true, hordeTime);
                HordeIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void hordet2_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayHordeTimer)
                {
                    string hordeTime = "";

                    if (hordet1.Text == "00" && hordet2.Text == "LIVE")
                        hordeTime = "LIVE";
                    else
                        hordeTime = hordet1.Text + ":" + hordet2.Text;

                    mainForm.TM.DisplayHordeTime(false, hordeTime);
                }
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void HordeOut_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutHordeTime();
                HordeIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion

        #region 바론 타이머

        private void BraonIN_Click(object sender, EventArgs e)
        {
            try
            {
                string baronTime = "";

                if (bt1.Text == "00" && bt2.Text == "LIVE")
                    baronTime = "LIVE";
                else
                    baronTime = bt1.Text + ":" + bt2.Text;

                mainForm.TM.DisplayBaronTime(true, baronTime);
                BraonIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void bt2_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayBaronTimer)
                {
                    string baronTime = "";

                    if (bt1.Text == "00" && bt2.Text == "LIVE")
                        baronTime = "LIVE";
                    else
                        baronTime = bt1.Text + ":" + bt2.Text;

                    mainForm.TM.DisplayBaronTime(false, baronTime);
                }
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void BraonOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutBaronTime();
                BraonIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion

      


        #region 바론 버프

        private void BFIN_Click(object sender, EventArgs e)
        {
            try
            {
                string baronTime = br1.Text + ":" + br2.Text;
                bool isBlue = BaronGroup.SelectedIndex == 0 ? true : false;
                string goldGap = txtgold.Text;
                mainForm.TM.DisplayBaronBuff(true, baronTime, isBlue, goldGap);
                BFIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void br2_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayBraonBuff)
                {
                    string baronTime = br1.Text + ":" + br2.Text;
                    bool isBlue = BaronGroup.SelectedIndex == 0 ? true : false;
                    string goldGap = txtgold.Text;
                    mainForm.TM.DisplayBaronBuff(false, baronTime, isBlue, goldGap);

                    if (baronTime.Equals("00:00"))
                    {
                        BFOUT_Click(null, null);
                        //임시 - 파워플레이 아웃
                    }
                }
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void BFOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutBaronBuff();
                BFIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion


     

        #region 타워 정보

        private void TIN_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.DisplayTower(BT.Text, RT.Text);
                TIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void TOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutTower();
                TIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion


        #region 억제기 타이머

        private void btnInBtor_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.DisplayInhibitor(bbt1.Text, bbt2.Text, bbt3.Text, bbt4.Text, bbt5.Text, bbt6.Text,bbt7.Text, bbt8.Text, bbt9.Text, bbt10.Text, true);
                btnInBtor.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void btnoutBtor_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutInhibitor(true);
                btnInBtor.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void btnInRtor_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.DisplayInhibitor(rbt1.Text, rbt2.Text, rbt3.Text, rbt4.Text, rbt5.Text, rbt6.Text, rbt7.Text, rbt8.Text, rbt9.Text, rbt10.Text, false);
                btnInRtor.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void RInOut_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutInhibitor(false);
                btnInRtor.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion


        #region 골드 / 데미지



        //누적골드
        private void btnGold_Click(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.MainLayerButton((SimpleButton)sender))
                {
                    mainForm.AccumulatedGold();
                }

            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        //골드그래프
        private void btnGoldGraph_Click(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.MainLayerButton((SimpleButton)sender)) mainForm.GoldGraph(true);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        //누적 데미지
        private void btnDeal_Click(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.MainLayerButton((SimpleButton)sender)) mainForm.Deal();
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        #endregion


        #region 한타딜량

        private void Fstart_Click(object sender, EventArgs e)
        {
            try
            {
                if (Fstart.Text == "START")
                {
                    DataManager.getInstance().초당한타딜량리턴시작();
                    Fstart.Text = "STOP";
                    Fstart.Appearance.BackColor = Color.FromArgb(255, 255, 0);
                }

                else
                {
                    DataManager.getInstance().초당한타딜량리턴종료();
                    Fstart.Text = "START";
                    Fstart.Appearance.BackColor = Color.Transparent;
                }
            }
            catch(Exception ex) { }
            
        }

        private void btnF_Click(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.MainLayerButton((SimpleButton)sender))
                {
                    if (radioGroup2.SelectedIndex == 1) // 실시간
                    {
                        if (Fstart.Text == "START")
                        {
                            MessageBox.Show("START 버튼을 눌러주세요");
                            mainForm.MainLayerButton(null);
                            return;
                        }
                        else
                        {
                            mainForm.TM.isDisplayFightRealTime = true;
                        }
                    }
                    else //요청후 한번에 처리
                    {
                        try
                        {
                            int stime = (Convert.ToInt32(t1.Text) * 60) + Convert.ToInt32(t2.Text);
                            int etime = (Convert.ToInt32(t3.Text) * 60) + Convert.ToInt32(t4.Text);
                            DataTable dt = DataManager.getInstance().한타딜량요청(stime, etime).Tables[0];

                            string timeInfo = t1.Text + ":" + t2.Text + " ~ " + t3.Text + ":" + t4.Text;
                            mainForm.TM.DisplayFight(false, timeInfo, dt, mainForm.DC); ;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("데이터를 수신할 수 없습니다..");
                            mainForm.MainLayerButton((SimpleButton)sender);
                            return;
                        }



                    }
                }
            }
            catch(Exception ex)
            {

            } 
        }



        #endregion

        private void btnFightTimeSet1_Click(object sender, EventArgs e)
        {
            t1.Text = mainForm.NowGameTime(true);
            t2.Text = mainForm.NowGameTime(false);
        }

        private void btnFightTimeSet2_Click(object sender, EventArgs e)
        {
            t3.Text = mainForm.NowGameTime(true);
            t4.Text = mainForm.NowGameTime(false);
        }

        private void ed2_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayElderTimer)
                {
                    bool isBlue = ElderGroup.SelectedIndex == 0 ? true : false;
                    mainForm.TM.DisplayElderTime(false, ed1.Text + ":" + ed2.Text, isBlue);
                }
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void TowerGoldIN_Click(object sender, EventArgs e)
        {
            try
            {
                List<string[]> strs = new List<string[]>();
                foreach (Control c in groupControl11.Controls)
                {
                    if (c is TextEdit) strs.Add(new string[] { c.Name.Replace("txtTG_", ""), c.Text });
                }

                if (mainForm.MainLayerButton((SimpleButton)sender)) mainForm.TM.DisplayGoldTower(strs); 
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void AtakanIN_Click(object sender, EventArgs e)
        {
            try
            {
                string atakanTime = "";

                if (at1.Text == "00" && at2.Text == "LIVE")
                    atakanTime = "LIVE";
                else
                    atakanTime = at1.Text + ":" + at2.Text;

                mainForm.TM.DisplayAtakanTime(true, atakanTime, radioGroup3.SelectedIndex);
                AtakanIN.Appearance.BackColor = Color.FromArgb(255, 0, 0);
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void at2_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.TM.isDisplayAtakanTimer)
                {
                    string atakanTime = "";

                    if (at1.Text == "00" && at2.Text == "LIVE")
                        atakanTime = "LIVE";
                    else
                        atakanTime = at1.Text + ":" + at2.Text;

                    mainForm.TM.DisplayAtakanTime(false, atakanTime,radioGroup3.SelectedIndex);
                }
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void AtakanOUT_Click(object sender, EventArgs e)
        {
            try
            {
                mainForm.TM.OutAtakanTime();
                AtakanIN.Appearance.BackColor = Color.Transparent;
            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }

        private void radioGroup3_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
             *    string newPath = Environment.CurrentDirectory + @"\Resource\MonsterRename\RuinousAtakhan.png";
            if (!File.Exists(newPath)) Log("아타칸 이미지 " + newPath + " 가 존재하지 않습니다.", LogType.Error);
            newPath = Environment.CurrentDirectory + @"\Resource\MonsterRename\VoraciousAtakhan.png";
             */
            //pictureEdit3.Image = mainForm.imageReSize(30, 30, Environment.CurrentDirectory + @"\Resource\MonsterRename\ThornboundAtakhan.png");
            /*
            if (radioGroup3.SelectedIndex == 0)
            {
                
            }
            else
            {
                pictureEdit3.Image = mainForm.imageReSize(30, 30, Environment.CurrentDirectory + @"\Resource\MonsterRename\VoraciousAtakhan.png"); 
            }
            */
        }

        

        public void setCheckBox()
        {
            try
            {
                DataTable dataTable = DataManager.getInstance().라이브데이터요청(DBDefine.요청데이터분류.퀘스트완료여부).Tables[DBDefine.요청데이터분류.퀘스트완료여부.GetStringValue()];
                var dc = mainForm.DC;
                foreach (DataRow dr in dataTable.Rows)
                {
                    if (dc.BlueLiner.Top.champ.Equals(dr[2].ToString())) { dc.BlueLiner.Top.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.BlueLiner.Jungle.champ.Equals(dr[2].ToString())) { dc.BlueLiner.Jungle.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.BlueLiner.Mid.champ.Equals(dr[2].ToString())) { dc.BlueLiner.Mid.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.BlueLiner.ADCarry.champ.Equals(dr[2].ToString())) { dc.BlueLiner.ADCarry.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.BlueLiner.Supporter.champ.Equals(dr[2].ToString())) { dc.BlueLiner.Supporter.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.RedLiner.Top.champ.Equals(dr[2].ToString())) { dc.RedLiner.Top.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.RedLiner.Jungle.champ.Equals(dr[2].ToString())) { dc.RedLiner.Jungle.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.RedLiner.Mid.champ.Equals(dr[2].ToString())) { dc.RedLiner.Mid.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.RedLiner.ADCarry.champ.Equals(dr[2].ToString())) { dc.RedLiner.ADCarry.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                    else if (dc.RedLiner.Supporter.champ.Equals(dr[2].ToString())) { dc.RedLiner.Supporter.isQuest = Convert.ToBoolean(dr[3].ToString()); }
                }

                checkEdit1.Checked = dc.BlueLiner.Top.isQuest;
                checkEdit2.Checked = dc.BlueLiner.Jungle.isQuest;
                checkEdit3.Checked = dc.BlueLiner.Mid.isQuest;
                checkEdit4.Checked = dc.BlueLiner.ADCarry.isQuest;
                checkEdit5.Checked = dc.BlueLiner.Supporter.isQuest;

                checkEdit6.Checked = dc.RedLiner.Top.isQuest;
                checkEdit7.Checked = dc.RedLiner.Jungle.isQuest;
                checkEdit8.Checked = dc.RedLiner.Mid.isQuest;
                checkEdit9.Checked = dc.RedLiner.ADCarry.isQuest;
                checkEdit10.Checked = dc.RedLiner.Supporter.isQuest;

                mainForm.TM.BQuest[0] = checkEdit1.Checked;
                mainForm.TM.BQuest[1] = checkEdit2.Checked;
                mainForm.TM.BQuest[2] = checkEdit3.Checked;
                mainForm.TM.BQuest[3] = checkEdit4.Checked;
                mainForm.TM.BQuest[4] = checkEdit5.Checked;

                mainForm.TM.RQuest[0] = checkEdit6.Checked;
                mainForm.TM.RQuest[1] = checkEdit7.Checked;
                mainForm.TM.RQuest[2] = checkEdit8.Checked;
                mainForm.TM.RQuest[3] = checkEdit9.Checked;
                mainForm.TM.RQuest[4] = checkEdit10.Checked;

                if (mainForm.TM.isDisplayDragon) mainForm.TM.DisplayDragon();
            }
            catch(Exception ex)
            {

            }
        }

        private void btnQuest_Click(object sender, EventArgs e)
        {
            try
            {
                if (mainForm.MainLayerButton((SimpleButton)sender))
                {
                    DataTable dataTable = DataManager.getInstance().라이브데이터요청(DBDefine.요청데이터분류.퀘스트완료여부).Tables[DBDefine.요청데이터분류.퀘스트완료여부.GetStringValue()];
                    mainForm.TM.DisplayQuest(dataTable, mainForm.DC);
                }

            }
            catch (Exception ex)
            {
                mainForm.Log(MethodBase.GetCurrentMethod().Name + " : " + ex.Message, LogWriter.LogType.Error);
            }
        }
    }
}
