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

    public class EquipPanel : ucBase
    {
        private DrawingUnit activeUnit;
        private IContainer components;
        private DrawingUnit curUnit;
        private DrawingUnit deleteUnit;
        private EquipEditor editor;
        private bool[,] flags;
        private bool isShowTip;
        private int lastedTipX;
        private int lastedTipY;
        private int lastedX;
        private int lastedY;
        private ContextMenuStrip muCopy;
        public const int pixels = 0x1a;
        private ContextMenuStrip popMenu;
        private DrawingUnit selectedUnit;
        private ToolStripMenuItem toolStripMenuItem1;
        private ToolStripMenuItem toolStripMenuItem2;
        private ToolStripMenuItem toolStripMenuItem3;
        private ToolStripMenuItem toolStripMenuItem4;
        public ToolTip ttInfo;
        private DrawingUnit[,] units;
        private int xnum;
        private int ynum;
        private ToolStripMenuItem 关闭ToolStripMenuItem;
        private ToolStripMenuItem 删除ToolStripMenuItem;
        private ToolStripMenuItem 复制CToolStripMenuItem;
        private ToolStripMenuItem 复制全部AToolStripMenuItem;
        private ToolStripMenuItem 整理ZToolStripMenuItem;
        private ToolStripMenuItem 清空CToolStripMenuItem;
        private ToolStripMenuItem 粘贴VToolStripMenuItem;

        public EquipPanel()
        {
            this.curUnit = new DrawingUnit();
            this.lastedTipX = -1;
            this.lastedTipY = -1;
            this.InitializeComponent();
        }

        public EquipPanel(int xnum, int ynum, EquipEditor editor)
        {
            this.curUnit = new DrawingUnit();
            this.lastedTipX = -1;
            this.lastedTipY = -1;
            this.InitializeComponent();
            this.setSize(xnum, ynum);
            this.editor = editor;
            this.curUnit.Item = editor.EditItem;
        }

        private bool addEditItem(int x, int y)
        {
            if (((this.curUnit != null) && (this.curUnit.Item != null)) && (this.curUnit.Item.Img != null))
            {
                this.curUnit.X = x;
                this.curUnit.Y = y;
                this.editor.updateData(this.curUnit.Item);
                if (this.canPutItem(this.curUnit) && this.putItem(this.curUnit, true))
                {
                    return true;
                }
                Utils.WarningMessage("Error: Not enough space to add [" + this.curUnit.Item.Name + "]");
            }
            return false;
        }

        public bool canPutItem(DrawingUnit unit)
        {
            EquipItem item = unit.Item;
            int x = unit.X;
            int y = unit.Y;
            if ((((item.Width + x) > this.xnum) || ((item.Height + y) > this.ynum)) || ((x < 0) || (y < 0)))
            {
                return false;
            }
            if (this.flags[x, y])
            {
                return false;
            }
            for (int i = 0; i < item.Width; i++)
            {
                for (int j = 0; j < item.Height; j++)
                {
                    if (this.flags[i + x, j + y])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public bool canPutItem(EquipItem item, int x, int y)
        {
            if ((((item.Width + x) > this.xnum) || ((item.Height + y) > this.ynum)) || ((x < 0) || (y < 0)))
            {
                return false;
            }
            if (this.flags[x, y])
            {
                return false;
            }
            for (int i = 0; i < item.Width; i++)
            {
                for (int j = 0; j < item.Height; j++)
                {
                    if (this.flags[i + x, j + y])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public bool canPutItem(EquipItem item, int x, int y, bool[,] flags)
        {
            if ((((item.Width + x) > this.xnum) || ((item.Height + y) > this.ynum)) || ((x < 0) || (y < 0)))
            {
                return false;
            }
            if (flags[x, y])
            {
                return false;
            }
            for (int i = 0; i < item.Width; i++)
            {
                for (int j = 0; j < item.Height; j++)
                {
                    if (flags[i + x, j + y])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public bool canPutItems(EquipItem[] items)
        {
            bool[,] flags = (bool[,])this.flags.Clone();
            foreach (EquipItem item in items)
            {
                if (item != null)
                {
                    for (int i = 0; i < this.ynum; i++)
                    {
                        for (int j = 0; j < this.xnum; j++)
                        {
                            if ((!flags[j, i] && this.canPutItem(item, j, i, flags)) && this.tryPutItem(item, j, i, flags))
                            {
                                continue;
                            }
                        }
                    }
                    return false;
                }
            }
            return true;
        }

        public void clearData()
        {
            this.activeUnit = null;
            this.selectedUnit = null;
            this.lastedX = 0;
            this.lastedY = 0;
            for (int i = 0; i < this.xnum; i++)
            {
                for (int j = 0; j < this.ynum; j++)
                {
                    this.units[i, j] = null;
                    this.flags[i, j] = false;
                }
            }
        }

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

        public void DownItem()
        {
            if (this.activeUnit != null)
            {
                this.activeUnit.X = this.lastedX;
                this.activeUnit.Y = this.lastedY;
                if (this.canPutItem(this.activeUnit) && this.putItem(this.activeUnit, false))
                {
                    this.selectedUnit = null;
                    this.activeUnit = null;
                }
            }
        }

        private void drawUnit(Graphics g, DrawingUnit unit)
        {
            int x = unit.X;
            int y = unit.Y;
            EquipItem item = unit.Item;
            g.DrawImage(item.Img, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            if (item.Level > 0)
            {
                g.DrawString(string.Format("+{0}", item.Level), new Font("Arial", 8f), Brushes.White, (float)(x * 0x1a), (float)(y * 0x1a));
            }
            else if (item.IsNoDurability && (item.Durability > 1))
            {
                g.DrawString(Convert.ToString(item.Durability), new Font("Arial", 6f), Brushes.White, (float)(x * 0x1a), (float)(y * 0x1a));
            }
            if (item.SN < 0)
            {
                g.DrawRectangle(Pens.White, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else 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, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else 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.Violet, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else 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)))))
            {
                g.DrawRectangle(Pens.Pink, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if ((((item.PlusLevel > 0) && (item.SetVal > 0)) && (item.ZY1 && item.ZY2)) && ((item.ZY3 && item.ZY4) && (item.ZY5 && item.ZY6)))
            {
                g.DrawRectangle(Pens.Aqua, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if (((((item.SetVal > 0) && item.ZY1) && (item.ZY2 && item.ZY3)) && ((item.ZY4 && item.ZY5) && item.ZY6)) || ((((item.PlusLevel > 0) && item.ZY1) && (item.ZY2 && item.ZY3)) && ((item.ZY4 && item.ZY5) && item.ZY6)))
            {
                g.DrawRectangle(Pens.Turquoise, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if (((item.ZY1 && item.ZY2) && (item.ZY3 && item.ZY4)) && (item.ZY5 && item.ZY6))
            {
                g.DrawRectangle(Pens.LawnGreen, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if (((item.PlusLevel > 0) && (item.SetVal > 0)) && (((item.ZY1 || item.ZY2) || (item.ZY3 || item.ZY4)) || (item.ZY5 || item.ZY6)))
            {
                g.DrawRectangle(Pens.Gold, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if (((item.PlusLevel > 0) && (((item.ZY1 || item.ZY2) || (item.ZY3 || item.ZY4)) || (item.ZY5 || item.ZY6))) || ((item.SetVal > 0) && (((item.ZY1 || item.ZY2) || (item.ZY3 || item.ZY4)) || (item.ZY5 || item.ZY6))))
            {
                g.DrawRectangle(Pens.Yellow, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if (((item.ZY1 || item.ZY2) || (item.ZY3 || item.ZY4)) || (item.ZY5 || item.ZY6))
            {
                g.DrawRectangle(Pens.YellowGreen, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if (item.PlusLevel > 0)
            {
                g.DrawRectangle(Pens.SkyBlue, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
            else if (item.SetVal > 0)
            {
                g.DrawRectangle(Pens.Khaki, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
            }
        }

        private void EquipPanel_MouseClick(object sender, MouseEventArgs e)
        {
            DrawingUnit unit = null;
            int x = e.X / 0x1a;
            int y = e.Y / 0x1a;
            if (e.Button == MouseButtons.Right)
            {
                if (this.activeUnit != null)
                {
                    this.activeUnit.X = this.lastedX;
                    this.activeUnit.Y = this.lastedY;
                    if (this.canPutItem(this.activeUnit) && this.putItem(this.activeUnit, false))
                    {
                        this.selectedUnit = null;
                        this.activeUnit = null;
                        unit = this.findUnit(x, y);
                        if (unit != null)
                        {
                            this.deleteUnit = unit;
                            this.popMenu.Show(this, e.X, e.Y);
                        }
                        else
                        {
                            this.muCopy.Show(this, e.X, e.Y);
                        }
                    }
                }
                else
                {
                    unit = this.findUnit(x, y);
                    if (unit != null)
                    {
                        this.deleteUnit = unit;
                        this.popMenu.Show(this, e.X, e.Y);
                    }
                    else
                    {
                        this.muCopy.Show(this, e.X, e.Y);
                    }
                }
            }
            else if (this.activeUnit == null)
            {
                unit = this.findUnit(x, y);
                this.activeUnit = unit;
                if (this.activeUnit != null)
                {
                    this.removeItem(this.activeUnit);
                }
                if (!this.flags[x, y] && (this.selectedUnit != null))
                {
                    this.removeItem(this.selectedUnit);
                    this.activeUnit = this.selectedUnit;
                    this.lastedX = this.selectedUnit.X;
                    this.lastedY = this.selectedUnit.Y;
                    this.selectedUnit = null;
                }
                if (unit != null)
                {
                    if (base.Parent.Parent.Controls.Find("tabcInfo", false).Length > 0)
                    {
                        ((TabControl)base.Parent.Parent.Controls.Find("tabcInfo", false)[0]).SelectedIndex = 2;
                    }
                    this.selectedUnit = unit;
                    this.lastedX = this.selectedUnit.X;
                    this.lastedY = this.selectedUnit.Y;
                    this.editor.updateUI(unit.Item);
                }
            }
            else
            {
                this.activeUnit.X = x;
                this.activeUnit.Y = y;
                if (this.canPutItem(this.activeUnit) && this.putItem(this.activeUnit, false))
                {
                    this.selectedUnit = null;
                    this.activeUnit = null;
                }
                else
                {
                    this.activeUnit.X = this.lastedX;
                    this.activeUnit.Y = this.lastedY;
                    if (this.canPutItem(this.activeUnit) && this.putItem(this.activeUnit, false))
                    {
                        this.selectedUnit = null;
                        this.activeUnit = null;
                    }
                    this.EquipPanel_MouseClick(sender, e);
                }
            }
            base.Invalidate();
        }

        private void EquipPanel_MouseDBClick(object sender, MouseEventArgs e)
        {
            int num = e.X / 0x1a;
            int num2 = e.Y / 0x1a;
            if (!this.flags[num, num2] && (this.selectedUnit == null))
            {
                if (this.editor.AllPart)
                {
                    this.putItems();
                    this.editor.AllPart = false;
                }
                else
                {
                    this.putItems(this.editor.AddNum);
                }
            }
            base.Invalidate();
        }

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

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

        private void EquipPanel_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                int x = e.X / 0x1a;
                int y = e.Y / 0x1a;
                if ((this.lastedTipX != x) || (this.lastedTipY != y))
                {
                    this.isShowTip = false;
                    this.ttInfo.Hide(this);
                }
                if (this.flags[x, y])
                {
                    this.Cursor = Cursors.Default;
                    if ((this.findUnit(x, y) != null) && !this.isShowTip)
                    {
                        this.isShowTip = true;
                        this.lastedTipX = x;
                        this.lastedTipY = y;
                        this.ttInfo.Show(Utils.GetEquipInfo(this.findUnit(x, y).Item), this, (int)(e.X + 20), e.Y + 20);
                    }
                }
                else
                {
                    if (this.activeUnit != null)
                    {
                        this.Cursor = Cursors.Hand;
                    }
                    else
                    {
                        this.Cursor = Cursors.Default;
                    }
                    this.isShowTip = false;
                    this.ttInfo.Hide(this);
                }
            }
            catch
            {
            }
            base.Invalidate();
        }

        private void EquipPanel_Paint(object sender, PaintEventArgs e)
        {
            if (this.units != null)
            {
                Graphics g = e.Graphics;
                DrawingUnit unit = null;
                EquipItem item = null;
                int x = 0;
                int y = 0;
                for (int i = 0; i < this.xnum; i++)
                {
                    for (int j = 0; j < this.ynum; j++)
                    {
                        if (((unit = this.units[i, j]) != null) && (unit.Item != null))
                        {
                            this.drawUnit(g, this.units[i, j]);
                        }
                    }
                }
                if ((this.activeUnit != null) && (this.activeUnit.Item != null))
                {
                    item = this.activeUnit.Item;
                    x = this.activeUnit.X;
                    y = this.activeUnit.Y;
                    g.DrawImage(item.Img, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
                    Brush brush = new SolidBrush(Color.FromArgb(60, 0x80, 0x80, 0x80));
                    g.FillRectangle(brush, (int)(this.activeUnit.X * 0x1a), (int)(this.activeUnit.Y * 0x1a), (int)(this.activeUnit.Item.Width * 0x1a), (int)(this.activeUnit.Item.Height * 0x1a));
                    g.DrawRectangle(Pens.Blue, (int)(x * 0x1a), (int)(y * 0x1a), (int)(item.Width * 0x1a), (int)(item.Height * 0x1a));
                }
            }
        }

        protected DrawingUnit findUnit(int x, int y)
        {
            DrawingUnit unit = null;
            if (this.flags[x, y])
            {
                for (int i = 0; i < this.xnum; i++)
                {
                    for (int j = 0; j < this.ynum; j++)
                    {
                        if (((unit = this.units[i, j]) != null) && unit.contains(x, y))
                        {
                            return unit;
                        }
                    }
                }
            }
            return null;
        }

        public string getEquipCodes()
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < this.ynum; i++)
            {
                for (int j = 0; j < this.xnum; j++)
                {
                    if ((this.units[j, i] == null) || (this.units[j, i].Item == null))
                    {
                        builder.Append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
                    }
                    else
                    {
                        builder.Append(this.units[j, i].Item.ToString());
                    }
                }
            }
            return builder.ToString();
        }

        public EquipItem[] getEquipItems()
        {
            IList list = this.getItems();
            EquipItem[] itemArray = new EquipItem[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                itemArray[i] = (EquipItem)list[i];
            }
            return itemArray;
        }

        public IList getItems()
        {
            IList list = new ArrayList();
            for (int i = 0; i < this.ynum; i++)
            {
                for (int j = 0; j < this.xnum; j++)
                {
                    if ((this.units[j, i] != null) && (this.units[j, i].Item != null))
                    {
                        list.Add(this.units[j, i].Item);
                    }
                }
            }
            return list;
        }

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

        private void InitializeComponent()
        {
            this.components = new Container();
            this.popMenu = new ContextMenuStrip(this.components);
            this.关闭ToolStripMenuItem = new ToolStripMenuItem();
            this.复制CToolStripMenuItem = new ToolStripMenuItem();
            this.复制全部AToolStripMenuItem = new ToolStripMenuItem();
            this.toolStripMenuItem4 = new ToolStripMenuItem();
            this.toolStripMenuItem1 = new ToolStripMenuItem();
            this.删除ToolStripMenuItem = new ToolStripMenuItem();
            this.toolStripMenuItem3 = new ToolStripMenuItem();
            this.muCopy = new ContextMenuStrip(this.components);
            this.清空CToolStripMenuItem = new ToolStripMenuItem();
            this.整理ZToolStripMenuItem = new ToolStripMenuItem();
            this.toolStripMenuItem2 = new ToolStripMenuItem();
            this.粘贴VToolStripMenuItem = new ToolStripMenuItem();
            this.ttInfo = new ToolTip(this.components);
            this.popMenu.SuspendLayout();
            this.muCopy.SuspendLayout();
            base.SuspendLayout();
            this.popMenu.Items.AddRange(new ToolStripItem[] { this.关闭ToolStripMenuItem, this.复制CToolStripMenuItem, this.复制全部AToolStripMenuItem, this.toolStripMenuItem4, this.toolStripMenuItem1, this.删除ToolStripMenuItem, this.toolStripMenuItem3 });
            this.popMenu.Name = "popMenu";
            this.popMenu.Size = new Size(0x89, 0x9e);
            this.关闭ToolStripMenuItem.Name = "关闭ToolStripMenuItem";
            this.关闭ToolStripMenuItem.Size = new Size(0x88, 0x16);
            this.关闭ToolStripMenuItem.Text = "Cut(&T)";
            this.关闭ToolStripMenuItem.Click += new EventHandler(this.关闭ToolStripMenuItem_Click);
            this.复制CToolStripMenuItem.Name = "复制CToolStripMenuItem";
            this.复制CToolStripMenuItem.Size = new Size(0x88, 0x16);
            this.复制CToolStripMenuItem.Text = "Copy(&C)";
            this.复制CToolStripMenuItem.Click += new EventHandler(this.复制CToolStripMenuItem_Click);
            this.复制全部AToolStripMenuItem.Name = "复制全部AToolStripMenuItem";
            this.复制全部AToolStripMenuItem.Size = new Size(0x88, 0x16);
            this.复制全部AToolStripMenuItem.Text = "Copy All(&A)";
            this.复制全部AToolStripMenuItem.Click += new EventHandler(this.复制全部AToolStripMenuItem_Click);
            this.toolStripMenuItem4.Name = "toolStripMenuItem4";
            this.toolStripMenuItem4.Size = new Size(0x88, 0x16);
            this.toolStripMenuItem4.Text = "整理(&L)";
            this.toolStripMenuItem4.Click += new EventHandler(this.整理ZToolStripMenuItem_Click);
            this.toolStripMenuItem4.Visible = false;
            this.toolStripMenuItem1.Name = "toolStripMenuItem1";
            this.toolStripMenuItem1.Size = new Size(0x88, 0x16);
            this.toolStripMenuItem1.Text = "Edit(&E)";
            this.toolStripMenuItem1.Click += new EventHandler(this.toolStripMenuItem1_Click);
            this.删除ToolStripMenuItem.Name = "删除ToolStripMenuItem";
            this.删除ToolStripMenuItem.Size = new Size(0x88, 0x16);
            this.删除ToolStripMenuItem.Text = "Delete(&D)";
            this.删除ToolStripMenuItem.Click += new EventHandler(this.删除ToolStripMenuItem_Click);
            this.toolStripMenuItem3.Name = "toolStripMenuItem3";
            this.toolStripMenuItem3.Size = new Size(0x88, 0x16);
            this.toolStripMenuItem3.Text = "Clear(&M)";
            this.toolStripMenuItem3.Click += new EventHandler(this.清空CToolStripMenuItem_Click);
            this.muCopy.Items.AddRange(new ToolStripItem[] { this.清空CToolStripMenuItem, this.整理ZToolStripMenuItem, this.toolStripMenuItem2, this.粘贴VToolStripMenuItem });
            this.muCopy.Name = "muCopy";
            this.muCopy.Size = new Size(0x99, 0x72);
            this.muCopy.Opening += new CancelEventHandler(this.muCopy_Opening);
            this.清空CToolStripMenuItem.Name = "清空CToolStripMenuItem";
            this.清空CToolStripMenuItem.Size = new Size(0x98, 0x16);
            this.清空CToolStripMenuItem.Text = "Clear(&C)";
            this.清空CToolStripMenuItem.Click += new EventHandler(this.清空CToolStripMenuItem_Click);
            this.整理ZToolStripMenuItem.Name = "整理ZToolStripMenuItem";
            this.整理ZToolStripMenuItem.Size = new Size(0x98, 0x16);
            this.整理ZToolStripMenuItem.Text = "整理(&L)";
            this.整理ZToolStripMenuItem.Visible = false;
            this.整理ZToolStripMenuItem.Click += new EventHandler(this.整理ZToolStripMenuItem_Click);
            this.toolStripMenuItem2.Name = "toolStripMenuItem2";
            this.toolStripMenuItem2.Size = new Size(0x98, 0x16);
            this.toolStripMenuItem2.Text = "Copy All(&A)";
            this.toolStripMenuItem2.Click += new EventHandler(this.复制全部AToolStripMenuItem_Click);
            this.粘贴VToolStripMenuItem.Name = "粘贴VToolStripMenuItem";
            this.粘贴VToolStripMenuItem.Size = new Size(0x98, 0x16);
            this.粘贴VToolStripMenuItem.Text = "Paste(&V)";
            this.粘贴VToolStripMenuItem.Click += new EventHandler(this.粘贴VToolStripMenuItem_Click);
            this.ttInfo.ToolTipIcon = ToolTipIcon.Info;
            this.ttInfo.ToolTipTitle = "Item information";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            //base.AutoScaleMode = AutoScaleMode.Font;
            this.BackgroundImage = Resources.warehouse_2;
            this.BackgroundImageLayout = ImageLayout.None;
            this.Cursor = Cursors.Default;
            this.DoubleBuffered = true;
            base.Margin = new Padding(0);
            base.Name = "EquipPanel";
            base.Size = new Size(0xd1, 0xd1);
            base.Paint += new PaintEventHandler(this.EquipPanel_Paint);
            base.MouseClick += new MouseEventHandler(this.EquipPanel_MouseClick);
            base.MouseDoubleClick += new MouseEventHandler(this.EquipPanel_MouseDBClick);
            base.MouseEnter += new EventHandler(this.EquipPanel_MouseEnter);
            base.MouseLeave += new EventHandler(this.EquipPanel_MouseLeave);
            base.MouseMove += new MouseEventHandler(this.EquipPanel_MouseMove);
            this.popMenu.ResumeLayout(false);
            this.muCopy.ResumeLayout(false);
            base.ResumeLayout(false);
        }

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

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

            EquipItem equipItem = null;
            for (int i = 0; i < ynum; i++)
            {
                for (int j = 0; j < xnum; j++)
                {
                    EquipItem equipItem2 = EquipItem.createItem(codes, offset + (i * xnum + j) * EquipItem.ITEM_SIZE, EquipItem.ITEM_SIZE);
                    equipItem = equipItem2;
                    if (equipItem2 != null)
                    {
                        DrawingUnit[,] array = units;
                        int num = j;
                        int num2 = i;
                        DrawingUnit drawingUnit = new DrawingUnit(equipItem, j, i);
                        array[num, num2] = drawingUnit;
                        putItem(units[j, i], false);
                    }
                }
            }
            base.Invalidate();
            return true;
        }

        public bool moveItem(DrawingUnit unit, int x, int y)
        {
            if ((unit != null) && (unit.Item != null))
            {
                int num = unit.X;
                int num2 = unit.Y;
                this.removeItem(unit);
                unit.X = x;
                unit.Y = y;
                if (this.canPutItem(unit) && this.putItem(unit, false))
                {
                    return true;
                }
                unit.X = num;
                unit.Y = num2;
                this.putItem(unit, false);
            }
            return false;
        }

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

        public bool putItem(DrawingUnit unit, bool add)
        {
            int x = unit.X;
            int y = unit.Y;
            if (add)
            {
                this.units[x, y] = new DrawingUnit(new EquipItem(), x, y);
                this.units[x, y].Item.assign(unit.Item);
                Utils.SN--;
                this.units[x, y].Item.SN = Utils.SN;
            }
            else
            {
                this.units[x, y] = unit;
            }
            for (int i = 0; i < unit.Item.Width; i++)
            {
                for (int j = 0; j < unit.Item.Height; j++)
                {
                    this.flags[i + x, j + y] = true;
                }
            }
            return true;
        }

        public bool putItem(EquipItem item, bool add, bool isCopy)
        {
            DrawingUnit unit = null;
            if (isCopy)
            {
                unit = new DrawingUnit(new EquipItem(), 0, 0);
                unit.Item.assign(item);
            }
            else if (add)
            {
                unit = new DrawingUnit(new EquipItem(), 0, 0);
                unit.Item.assign(item);
                Utils.SN--;
                unit.Item.SN = Utils.SN;
                if (!Utils.ListWareHouse.Contains(unit.Item.ToString()))
                {
                    Utils.ListWareHouse.Add(unit.Item.ToString());
                }
            }
            else
            {
                unit = new DrawingUnit(item, 0, 0);
            }
            for (int i = 0; i < this.ynum; i++)
            {
                for (int j = 0; j < this.xnum; j++)
                {
                    if (!this.flags[j, i])
                    {
                        unit.X = j;
                        unit.Y = i;
                        if (this.canPutItem(unit))
                        {
                            return this.putItem(unit, false);
                        }
                    }
                }
            }
            return false;
        }

        public bool putItems()
        {
            bool flag = true;
            this.editor.updateData(this.curUnit.Item);
            EquipItem item = new EquipItem();
            string str = "7,8,9,10,11";
            string str2 = "0";
            switch (this.curUnit.Item.Code)
            {
                case 0x17:
                case 0x20:
                case 15:
                case 20:
                case 0x2f:
                case 0x30:
                case 0x25:
                case 0x47:
                    str2 = "7";
                    break;

                case 0x3b:
                case 60:
                case 0x3d:
                case 0x48:
                    str2 = "10";
                    break;
            }
            foreach (string str3 in str.Split(new char[] { ',' }))
            {
                if (!(str3 == str2) && (EquipEditor.xItem[(Convert.ToByte(str3) * 0x200) + this.curUnit.Item.Code] != null))
                {
                    item.assign(EquipItem.createItem(this.curUnit.Item.Code, Convert.ToByte(str3)));
                    item.Code = this.curUnit.Item.Code;
                    item.Type = Convert.ToByte(str3);
                    this.editor.updateData(item);
                    flag = flag && this.putItem(item, true, false);
                }
            }
            return flag;
        }

        public bool putItems(int nums)
        {
            bool flag = true;
            this.editor.updateData(this.curUnit.Item);
            for (int i = 0; i < nums; i++)
            {
                flag = flag && this.putItem(this.curUnit.Item, true, false);
            }
            return flag;
        }

        public bool putItems(EquipItem[] items, bool isCopy)
        {
            bool flag = true;
            if (!this.canPutItems(items))
            {
                return false;
            }
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] != null)
                {
                    flag = flag && this.putItem(items[i], true, isCopy);
                }
            }
            return flag;
        }

        public bool removeItem(DrawingUnit unit)
        {
            if (unit != null)
            {
                int x = unit.X;
                int y = unit.Y;
                this.units[x, y] = null;
                for (int i = 0; i < unit.Item.Width; i++)
                {
                    for (int j = 0; j < unit.Item.Height; j++)
                    {
                        this.flags[i + x, j + y] = false;
                    }
                }
            }
            return true;
        }

        protected void resizePanel()
        {
            base.Width = this.xnum * 0x1a;
            base.Height = this.ynum * 0x1a;
        }

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

        public void setSize(int xnum, int ynum)
        {
            this.xnum = xnum;
            this.ynum = ynum;
            this.resizePanel();
            this.units = new DrawingUnit[xnum, ynum];
            this.flags = new bool[xnum, ynum];
            for (int i = 0; i < xnum; i++)
            {
                for (int j = 0; j < ynum; j++)
                {
                    this.units[i, j] = null;
                    this.flags[i, j] = false;
                }
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.setEquipProperty(this.deleteUnit);
            this.deleteUnit = null;
        }

        public bool tryPutItem(EquipItem item, int x, int y, bool[,] flags)
        {
            if ((((item.Width + x) > this.xnum) || ((item.Height + y) > this.ynum)) || ((x < 0) || (y < 0)))
            {
                return false;
            }
            if (flags[x, y])
            {
                return false;
            }
            for (int i = 0; i < item.Width; i++)
            {
                for (int j = 0; j < item.Height; j++)
                {
                    if (!flags[i + x, j + y])
                    {
                        flags[i + x, j + y] = true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }

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

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

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

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

        private void 整理ZToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.DownItem();
            EquipItem[] items = this.getEquipItems();
            this.clearData();
            this.putItems(items, true);
            base.Invalidate();
        }

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

        private void 粘贴VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IDataObject dataObject = Clipboard.GetDataObject();
            if (dataObject.GetDataPresent(DataFormats.Text))
            {
                string str = ((string)dataObject.GetData(DataFormats.Text)).Replace("&", "").Replace("0x", "");
                if (((str.Length == 0x20) && Validator.HexString(str)) && !str.StartsWith("FF"))
                {
                    EquipItem item = EquipItem.createItem(str);
                    if ((item == null) || !this.putItem(item, false, true))
                    {
                    }
                }
                else if (((str.Length % 0x20) == 0) && Validator.HexString(str))
                {
                    EquipItem[] items = new EquipItem[str.Length / 0x20];
                    for (int i = 0; i < (str.Length / 0x20); i++)
                    {
                        string scodes = str.Substring(i * 0x20, 0x20);
                        if (!scodes.StartsWith("FF"))
                        {
                            items[i] = EquipItem.createItem(scodes);
                        }
                    }
                    if (!this.putItems(items, true))
                    {
                        Utils.WarningMessage("Error: Not enough space for more items.");
                    }
                }
            }
        }

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

        public IList Items
        {
            get
            {
                return this.getItems();
            }
        }

        public int XNum
        {
            get
            {
                return this.xnum;
            }
        }

        public int YNum
        {
            get
            {
                return this.ynum;
            }
        }
    }
}

