using MuEditor.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MuEditor
{
    public class PentagramPanel : ucBase
    {
        public const int EquipNum = 1;

        private int index = -1;

        private int lastedIndex = -2;

        private bool isShowTip;

        private Rectangle[] positions = new Rectangle[1]
        {
        new Rectangle(6, 6, 46, 46)
        };

        private DrawingUnit[] units = new DrawingUnit[1];

        private DrawingUnit curUnit = new DrawingUnit();

        private DrawingUnit selectedUnit;

        private int prof = -1;

        private EquipEditor editor;

        private IContainer components;

        private ContextMenuStrip popMenu;

        private ToolStripMenuItem 删除ToolStripMenuItem;

        private ToolStripMenuItem 属性ToolStripMenuItem;

        private ToolStripMenuItem 关闭ToolStripMenuItem;

        private ToolStripMenuItem 复制CToolStripMenuItem;

        private ContextMenuStrip muCopy;

        private ToolStripMenuItem 粘贴VToolStripMenuItem;

        private ToolTip ttInfo;

        private ToolStripMenuItem 复制全部AToolStripMenuItem;

        private ToolStripMenuItem 清空CToolStripMenuItem;

        private ToolStripMenuItem toolStripMenuItem2;

        private ToolStripMenuItem toolStripMenuItem1;

        public EquipEditor Editor
        {
            set
            {
                editor = value;
            }
        }

        public int Prof
        {
            get
            {
                return prof;
            }
            set
            {
                prof = value;
            }
        }

        public PentagramPanel()
        {
            InitializeComponent();
        }

        public PentagramPanel(EquipEditor editor)
        {
            InitializeComponent();
            this.editor = editor;
        }

        private bool addEditItem(int index)
        {
            if (curUnit != null && curUnit.Item != null && curUnit.Item.Img != null)
            {
                curUnit.X = index;
                curUnit.Y = 0;
                editor.updateData(curUnit.Item);
                if (canPutItem(curUnit) && putItem(curUnit, true, false))
                {
                    return true;
                }
                Utils.WarningMessage("Item " + curUnit.Item.Name + " can not be placed in that position!");
                return false;
            }
            return false;
        }

        public bool canPutItem(DrawingUnit unit)
        {
            EquipItem item = unit.Item;
            int x = unit.X;
            int y = unit.Y;
            switch (x)
            {
            }
            goto IL_0029;
        IL_001d:
            if (units[x] == null)
            {
                goto IL_0029;
            }
            goto IL_0027;
        IL_0027:
            return false;
        IL_0029:
            if ((ushort)(EquipEditor.xItem[item.Type * 512 + item.Code].ItemSlot - 236) == index)
            {
                return true;
            }
            return false;
        }

        public void clearData()
        {
            selectedUnit = null;
            for (int i = 0; i < 1; i++)
            {
                units[i] = null;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void drawUnit(Graphics g, DrawingUnit unit)
        {
            int x = unit.X;
            int y = unit.Y;
            EquipItem item = unit.Item;
            int num = Math.Max(positions[x].Width - item.Width * 26 >> 1, 0);
            int num2 = Math.Max(positions[x].Height - item.Height * 26 >> 1, 0);
            int width = Math.Min(item.Width * 26, positions[x].Width);
            int height = Math.Min(item.Height * 26, positions[x].Height);
            g.DrawImage(item.Img, positions[x].X + num, positions[x].Y + num2, width, height);
            if (item.Level > 0)
            {
                g.DrawString($"+{item.Level}", new Font("Arial", 8f), Brushes.White, (float)(positions[x].X + num), (float)(positions[x].Y + num2));
            }
            else if (item.IsNoDurability && item.Durability > 1)
            {
                g.DrawString(Convert.ToString(item.Durability), new Font("Arial", 6f), Brushes.White, (float)(positions[x].X + num), (float)(positions[x].Y + num2));
            }
            if (item.SN < 0)
            {
                g.DrawRectangle(Pens.White, positions[x].X + num, positions[x].Y + num2, width, height);
                return;
            }
            if (item.XQ1 > 0 && item.XQ1 < EquipEditor.xSocket.Length && item.XQ2 > 0 && item.XQ2 < EquipEditor.xSocket.Length && item.XQ3 > 0 && item.XQ3 < EquipEditor.xSocket.Length && item.XQ4 > 0 && item.XQ4 < EquipEditor.xSocket.Length && item.XQ5 > 0 && item.XQ5 < EquipEditor.xSocket.Length && item.YG > 0 && item.YG < Utils.YGS.Length - 1 && (item.ZY1 || item.ZY2 || item.ZY3 || item.ZY4 || item.ZY5 || item.ZY6))
            {
                g.DrawRectangle(Pens.Fuchsia, positions[x].X + num, positions[x].Y + num2, width, height);
                return;
            }
            if (item.XQ1 > 0 && item.XQ1 < EquipEditor.xSocket.Length)
            {
                goto IL_037c;
            }
            if (item.XQ2 > 0 && item.XQ2 < EquipEditor.xSocket.Length)
            {
                goto IL_037c;
            }
            if (item.XQ3 > 0 && item.XQ3 < EquipEditor.xSocket.Length)
            {
                goto IL_037c;
            }
            if (item.XQ4 > 0 && item.XQ4 < EquipEditor.xSocket.Length)
            {
                goto IL_037c;
            }
            if (item.XQ5 > 0 && item.XQ5 < EquipEditor.xSocket.Length)
            {
                goto IL_037c;
            }
            if (item.YG > 0 && item.YG < Utils.YGS.Length - 1)
            {
                goto IL_037c;
            }
            goto IL_03e2;
        IL_0594:
            g.DrawRectangle(Pens.Turquoise, positions[x].X + num, positions[x].Y + num2, width, height);
            return;
        IL_037c:
            if (!item.ZY1 && !item.ZY2 && !item.ZY3 && !item.ZY4 && !item.ZY5 && !item.ZY6)
            {
                goto IL_03e2;
            }
            g.DrawRectangle(Pens.Violet, positions[x].X + num, positions[x].Y + num2, width, height);
            return;
        IL_03e2:
            if (item.XQ1 > 0 && item.XQ1 < EquipEditor.xSocket.Length)
            {
                goto IL_0474;
            }
            if (item.XQ2 > 0 && item.XQ2 < EquipEditor.xSocket.Length)
            {
                goto IL_0474;
            }
            if (item.XQ3 > 0 && item.XQ3 < EquipEditor.xSocket.Length)
            {
                goto IL_0474;
            }
            if (item.XQ4 > 0 && item.XQ4 < EquipEditor.xSocket.Length)
            {
                goto IL_0474;
            }
            if (item.XQ5 > 0 && item.XQ5 < EquipEditor.xSocket.Length)
            {
                goto IL_0474;
            }
            if (item.YG > 0 && item.YG < Utils.YGS.Length - 1)
            {
                goto IL_0474;
            }
            if (item.PlusLevel > 0 && item.SetVal > 0 && item.ZY1 && item.ZY2 && item.ZY3 && item.ZY4 && item.ZY5 && item.ZY6)
            {
                g.DrawRectangle(Pens.Aqua, positions[x].X + num, positions[x].Y + num2, width, height);
            }
            else
            {
                if (item.SetVal > 0 && item.ZY1 && item.ZY2 && item.ZY3 && item.ZY4 && item.ZY5 && item.ZY6)
                {
                    goto IL_0594;
                }
                if (item.PlusLevel > 0 && item.ZY1 && item.ZY2 && item.ZY3 && item.ZY4 && item.ZY5 && item.ZY6)
                {
                    goto IL_0594;
                }
                if (item.ZY1 && item.ZY2 && item.ZY3 && item.ZY4 && item.ZY5 && item.ZY6)
                {
                    g.DrawRectangle(Pens.LawnGreen, positions[x].X + num, positions[x].Y + num2, width, height);
                }
                else if (item.PlusLevel > 0 && item.SetVal > 0 && (item.ZY1 || item.ZY2 || item.ZY3 || item.ZY4 || item.ZY5 || item.ZY6))
                {
                    g.DrawRectangle(Pens.Gold, positions[x].X + num, positions[x].Y + num2, width, height);
                }
                else
                {
                    if (item.PlusLevel > 0 && (item.ZY1 || item.ZY2 || item.ZY3 || item.ZY4 || item.ZY5 || item.ZY6))
                    {
                        goto IL_071a;
                    }
                    if (item.SetVal > 0 && (item.ZY1 || item.ZY2 || item.ZY3 || item.ZY4 || item.ZY5 || item.ZY6))
                    {
                        goto IL_071a;
                    }
                    if (item.ZY1 || item.ZY2 || item.ZY3 || item.ZY4 || item.ZY5 || item.ZY6)
                    {
                        g.DrawRectangle(Pens.YellowGreen, positions[x].X + num, positions[x].Y + num2, width, height);
                    }
                    else if (item.PlusLevel > 0)
                    {
                        g.DrawRectangle(Pens.SkyBlue, positions[x].X + num, positions[x].Y + num2, width, height);
                    }
                    else if (item.SetVal > 0)
                    {
                        g.DrawRectangle(Pens.Khaki, positions[x].X + num, positions[x].Y + num2, width, height);
                    }
                }
            }
            return;
        IL_071a:
            g.DrawRectangle(Pens.Yellow, positions[x].X + num, positions[x].Y + num2, width, height);
            return;
        IL_0474:
            g.DrawRectangle(Pens.Pink, positions[x].X + num, positions[x].Y + num2, width, height);
        }

        public int findRectangle(int x, int y)
        {
            for (int i = 0; i < 1; i++)
            {
                if (positions[i].Contains(x, y))
                {
                    return i;
                }
            }
            return -1;
        }

        public string getEquipCodes()
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 1; i++)
            {
                if (units[i] == null || units[i].Item == null)
                {
                    stringBuilder.Append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
                }
                else
                {
                    stringBuilder.Append(units[i].Item.ToString());
                }
            }
            return stringBuilder.ToString();
        }

        private bool hasDataInClipboard()
        {
            IDataObject dataObject = Clipboard.GetDataObject();
            if (dataObject.GetDataPresent(DataFormats.Text))
            {
                string text = ((string)dataObject.GetData(DataFormats.Text)).Replace("&", "").Replace("0x", "");
                if (text.Length == 32 && Validator.HexString(text) && !text.StartsWith("FF"))
                {
                    return true;
                }
            }
            return false;
        }

        private void InitializeComponent()
        {
            components = new Container();
            popMenu = new ContextMenuStrip(components);
            关闭ToolStripMenuItem = new ToolStripMenuItem();
            复制CToolStripMenuItem = new ToolStripMenuItem();
            复制全部AToolStripMenuItem = new ToolStripMenuItem();
            属性ToolStripMenuItem = new ToolStripMenuItem();
            删除ToolStripMenuItem = new ToolStripMenuItem();
            toolStripMenuItem1 = new ToolStripMenuItem();
            muCopy = new ContextMenuStrip(components);
            清空CToolStripMenuItem = new ToolStripMenuItem();
            toolStripMenuItem2 = new ToolStripMenuItem();
            粘贴VToolStripMenuItem = new ToolStripMenuItem();
            ttInfo = new ToolTip(components);
            popMenu.SuspendLayout();
            muCopy.SuspendLayout();
            base.SuspendLayout();
            ToolStripItemCollection items = popMenu.Items;
            ToolStripItem[] toolStripItems = new ToolStripItem[1]
            {
            删除ToolStripMenuItem
            };
            items.AddRange(toolStripItems);
            popMenu.Name = "popMenu";
            popMenu.Size = new Size(141, 136);
            关闭ToolStripMenuItem.Name = "关闭ToolStripMenuItem";
            关闭ToolStripMenuItem.Size = new Size(140, 22);
            关闭ToolStripMenuItem.Click += 关闭ToolStripMenuItem_Click;
            复制CToolStripMenuItem.Name = "复制CToolStripMenuItem";
            复制CToolStripMenuItem.Size = new Size(140, 22);
            复制CToolStripMenuItem.Text = "Copy";
            复制CToolStripMenuItem.Click += 复制CToolStripMenuItem_Click;
            复制全部AToolStripMenuItem.Name = "复制全部AToolStripMenuItem";
            复制全部AToolStripMenuItem.Size = new Size(140, 22);
            复制全部AToolStripMenuItem.Text = "Copy All";
            复制全部AToolStripMenuItem.Click += 复制全部AToolStripMenuItem_Click;
            属性ToolStripMenuItem.Name = "属性ToolStripMenuItem";
            属性ToolStripMenuItem.Size = new Size(140, 22);
            属性ToolStripMenuItem.Text = "Modify";
            属性ToolStripMenuItem.Click += 属性ToolStripMenuItem_Click;
            删除ToolStripMenuItem.Name = "删除ToolStripMenuItem";
            删除ToolStripMenuItem.Size = new Size(140, 22);
            删除ToolStripMenuItem.Text = "Delete Item";
            删除ToolStripMenuItem.Click += 删除ToolStripMenuItem_Click;
            toolStripMenuItem1.Name = "toolStripMenuItem1";
            toolStripMenuItem1.Size = new Size(140, 22);
            toolStripMenuItem1.Text = "Clear";
            toolStripMenuItem1.Click += 清空CToolStripMenuItem_Click;
            muCopy.Name = "muCopy";
            muCopy.Size = new Size(141, 70);
            muCopy.Opening += muCopy_Opening;
            清空CToolStripMenuItem.Name = "清空CToolStripMenuItem";
            清空CToolStripMenuItem.Size = new Size(140, 22);
            清空CToolStripMenuItem.Text = "Clear";
            清空CToolStripMenuItem.Click += 清空CToolStripMenuItem_Click;
            toolStripMenuItem2.Name = "toolStripMenuItem2";
            toolStripMenuItem2.Size = new Size(140, 22);
            toolStripMenuItem2.Text = "Copy All";
            toolStripMenuItem2.Click += 复制全部AToolStripMenuItem_Click;
            粘贴VToolStripMenuItem.Name = "粘贴VToolStripMenuItem";
            粘贴VToolStripMenuItem.Size = new Size(140, 22);
            粘贴VToolStripMenuItem.Text = "Stick";
            粘贴VToolStripMenuItem.Click += 粘贴VToolStripMenuItem_Click;
            ttInfo.ToolTipIcon = ToolTipIcon.Info;
            ttInfo.ToolTipTitle = "Item Information";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            BackgroundImage = Resources.skillbook;
            BackgroundImageLayout = ImageLayout.None;
            Cursor = Cursors.Default;
            DoubleBuffered = true;
            base.Margin = new Padding(0);
            base.Name = "SkillBookPanel";
            base.Size = new Size(58, 58);
            base.Paint += SkillPanel_Paint;
            base.MouseClick += SkillPanel_MouseClick;
            base.MouseDoubleClick += SkillPanel_MouseDoubleClick;
            base.MouseEnter += SkillPanel_MouseEnter;
            base.MouseLeave += SkillPanel_MouseLeave;
            base.MouseMove += SkillPanel_MouseMove;
            popMenu.ResumeLayout(false);
            muCopy.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        public bool loadItemsByCodes(byte[] codes)
        {
            return loadItemsByCodes(codes, 0, codes.Length);
        }

        public bool loadItemsByCodes(byte[] codes, int offset, int len)
        {
            if (offset < 0 || len < EquipItem.ITEM_SIZE * EquipNum || codes == null)
            {
                this.Invalidate();
                return false;
            }

            EquipItem equipItem = null;
            for (int i = 0; i < EquipNum; i++)
            {
                EquipItem equipItem2 = EquipItem.createItem(codes, offset + i * EquipItem.ITEM_SIZE, EquipItem.ITEM_SIZE);
                equipItem = equipItem2;
                if (equipItem2 != null)
                {
                    units[i] = new DrawingUnit(equipItem, i, 0);
                }
            }
            base.Invalidate();
            return true;
        }

        private void muCopy_Opening(object sender, CancelEventArgs e)
        {
            粘贴VToolStripMenuItem.Enabled = hasDataInClipboard();
        }

        public bool putItem(DrawingUnit unit, bool add, bool isCopy)
        {
            int x = unit.X;
            int y = unit.Y;
            if (!isCopy)
            {
                if (!add)
                {
                    Utils.SN--;
                    unit.Item.SN = Utils.SN;
                    units[x] = unit;
                }
                else
                {
                    units[x] = new DrawingUnit(new EquipItem(), x, y);
                    Utils.SN--;
                    unit.Item.SN = Utils.SN;
                    units[x].Item.assign(unit.Item);
                }
                if (!Utils.ListPackage.Contains(unit.Item.ToString()))
                {
                    Utils.ListPackage.Add(unit.Item.ToString());
                }
            }
            else
            {
                units[index] = new DrawingUnit(new EquipItem(), index, y);
                units[index].Item.assign(unit.Item);
            }
            return true;
        }

        public bool removeItem(DrawingUnit unit)
        {
            int x = unit.X;
            units[x] = null;
            return true;
        }

        private void setEquipProperty(DrawingUnit unit)
        {
            if (unit != null)
            {
                Cursor = Cursors.WaitCursor;
                EquipProperty equipProperty = new EquipProperty(unit.Item);
                equipProperty.ShowDialog();
                equipProperty.Dispose();
                Cursor = Cursors.Default;
            }
        }

        private void SkillPanel_MouseClick(object sender, MouseEventArgs e)
        {
            index = findRectangle(e.X, e.Y);
            if (e.Button == MouseButtons.Right)
            {
                if (index < 0)
                {
                    selectedUnit = null;
                }
                else if (units[index] == null)
                {
                    muCopy.Show(this, e.X, e.Y);
                    selectedUnit = null;
                }
                else
                {
                    selectedUnit = units[index];
                    popMenu.Show(this, e.X, e.Y);
                }
            }
            else if (index < 0)
            {
                selectedUnit = null;
            }
            else if (units[index] == null)
            {
                selectedUnit = null;
            }
            else
            {
                if (base.Parent.Parent.Controls.Find("tabcInfo", false).Length > 0)
                {
                    ((TabControl)base.Parent.Parent.Controls.Find("tabcInfo", false)[0]).SelectedIndex = 2;
                }
                selectedUnit = units[index];
                editor.updateUI(selectedUnit.Item);
            }
        }

        private void SkillPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            index = findRectangle(e.X, e.Y);
            if (index < 0)
            {
                selectedUnit = null;
            }
            else if (units[index] == null)
            {
                addEditItem(index);
            }
        }

        private void SkillPanel_MouseEnter(object sender, EventArgs e)
        {
            curUnit.Item = editor.EditItem;
        }

        private void SkillPanel_MouseLeave(object sender, EventArgs e)
        {
            curUnit.Item = null;
            isShowTip = false;
            ttInfo.Hide(this);
            base.Invalidate();
        }

        private void SkillPanel_MouseMove(object sender, MouseEventArgs e)
        {
            index = findRectangle(e.X, e.Y);
            if (lastedIndex != index)
            {
                isShowTip = false;
                ttInfo.Hide(this);
            }
            if (index < 0)
            {
                isShowTip = false;
                ttInfo.Hide(this);
            }
            else if (units[index] != null && !isShowTip)
            {
                isShowTip = true;
                lastedIndex = index;
                ttInfo.Show(Utils.GetEquipInfo(units[index].Item), this, e.X + 20, e.Y + 20);
            }
            base.Invalidate();
        }

        private void SkillPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            for (int i = 0; i < 1; i++)
            {
                if (units[i] != null)
                {
                    drawUnit(graphics, units[i]);
                }
            }
            if (selectedUnit != null && selectedUnit.Item != null)
            {
                Brush brush = new SolidBrush(Color.FromArgb(60, 128, 128, 128));
                graphics.FillRectangle(brush, positions[selectedUnit.X]);
            }
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(selectedUnit.Item.ToString(), true);
            removeItem(selectedUnit);
            selectedUnit = null;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            removeItem(selectedUnit);
            Utils.ListPackage.Remove(selectedUnit.Item.ToString());
            selectedUnit = null;
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject("&" + selectedUnit.Item.ToString(), true);
        }

        private void 复制全部AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject("&" + getEquipCodes(), true);
        }

        private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setEquipProperty(selectedUnit);
        }

        private void 清空CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            clearData();
            base.Invalidate();
        }

        private void 粘贴VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IDataObject dataObject = Clipboard.GetDataObject();
            if (dataObject.GetDataPresent(DataFormats.Text))
            {
                string text = ((string)dataObject.GetData(DataFormats.Text)).Replace("&", "").Replace("0x", "");
                if (text.Length == 32 && Validator.HexString(text) && !text.StartsWith("FF"))
                {
                    EquipItem equipItem = EquipItem.createItem(text);
                    DrawingUnit unit = new DrawingUnit(equipItem);
                    if (equipItem != null && canPutItem(unit))
                    {
                        putItem(unit, false, true);
                    }
                    else
                    {
                        Utils.WarningMessage("Item" + equipItem.Name + "can not be placed in that position!");
                    }
                }
            }
        }
    }
}