using DevExpress.XtraGrid.Views.Tile;
using System;
using System.Data;
using System.Threading.Tasks;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Tile.ViewInfo;
using DevExpress.XtraEditors;

namespace DeviceRepairAndOptimization.Pages
{
    public partial class page_DriveItems : FormBase
    {
        public page_DriveItems()
        {
            InitializeComponent();
        }

        TileView MainView
        {
            get { return tileView; }
        }

        DataSet Datas;

        /// <summary>
        /// 窗口加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void page_DriveType_Load(object sender, EventArgs e)
        {
            Task.Run(() => { Invoke(new Action(() => { InitializeGridData(); })); });

            gridControl.ToolTipController = toolTipController1;
        }

        /// <summary>
        /// 数据模拟
        /// </summary>
        void InitializeGridData()
        {
            #region 造数据

            Datas = new DataSet();
            DataTable table = new DataTable();
            table.Columns.Add("ViewColumn1");
            table.Columns.Add("ViewColumn2");

            string[] t = { "抛光机", "方柱立式钻床", "台式钻床", "研磨机", "普铣", "普车", "压力机", "干燥箱" };

            foreach (string item in t)
            {
                DataRow row = table.NewRow();
                row["ViewColumn1"] = item;
                row["ViewColumn2"] = $"这里是{item}类型的说明。";
                table.Rows.Add(row);
            }

            Datas.Tables.Add(table);

            table = new DataTable();
            table.Columns.Add("DriveType");
            table.Columns.Add("DriveNumber");
            table.Columns.Add("DriveName");
            table.Columns.Add("DriveLocation");
            table.Columns.Add("NextDate");
            table.Columns.Add("BindVersion");

            for (int i = 0; i < 500; i++)
            {
                DataRow row = table.NewRow();
                row["DriveType"] = t[i % t.Length];
                row["DriveNumber"] = (i + 1) + "";
                row["DriveName"] = t[i % t.Length] + "设备" + (i + 1);
                row["DriveLocation"] = "安装地点" + (i + 1);
                row["NextDate"] = System.DateTime.Now;
                row["BindVersion"] = "v1.0." + i;
                table.Rows.Add(row);
            }

            Datas.Tables.Add(table);

            #endregion

            Datas.Relations.Add("Lk", Datas.Tables[0].Columns["ViewColumn1"], Datas.Tables[1].Columns["DriveType"]);

            gridControl.DataSource = Datas.Tables[0];
        }

        /// <summary>
        /// 双击 Item 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tileView_ItemDoubleClick(object sender, TileViewItemClickEventArgs e)
        {
            string dType = tileView.GetRowCellValue(e.Item.RowHandle, "ViewColumn1") + "";
            DataRowView dataRowView = (tileView.GetRow(e.Item.RowHandle) as DataRowView);
            if (dataRowView != null)
            {
                DataRow[] rows = dataRowView.Row.GetChildRows(Datas.Relations[0]);
                gridControl.MainView = tileView1;
                lb_Caption.Text = dType;
                btn_Return.Visible = true;
                gridControl.DataSource = rows.Cast<DataRow>().CopyToDataTable();
            }
        }

        /// <summary>
        /// 退回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Back_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl.MainView = MainView;
            lb_Caption.Text = MainView.ViewCaption;
            btn_Return.Visible = false;
            gridControl.DataSource = Datas.Tables[0];
        }


        private void tileView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(Control.MousePosition);
            }
        }

        private void btn_Return_Click(object sender, EventArgs e)
        {
            gridControl.MainView = MainView;
            lb_Caption.Text = MainView.ViewCaption;
            btn_Return.Visible = false;
            gridControl.DataSource = Datas.Tables[0];
        }

        /// <summary>
        /// 根据条件动态转换背景色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tileView1_ItemCustomize(object sender, TileViewItemCustomizeEventArgs e)
        {
            //TileView tv = (sender as TileView);
            //DataRow item = tv.GetDataRow(e.RowHandle);

            int a = Guid.NewGuid().ToByteArray()[0];
            switch (a % 4)
            {
                case 1:
                    e.Item.AppearanceItem.Normal.BackColor = System.Drawing.Color.FromArgb(223, 240, 216);
                    break;
                case 2:
                    e.Item.AppearanceItem.Normal.BackColor = System.Drawing.Color.FromArgb(252, 248, 227);
                    break;
                case 3:
                    e.Item.AppearanceItem.Normal.BackColor = System.Drawing.Color.FromArgb(242, 222, 222);
                    break;
                default:
                    break;
            }
        }

        private void tileView1_ContextButtonClick(object sender, ContextItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "contextButton1":
                    XtraMessageBox.Show("设备保养");
                    break;
                case "contextButton2":
                    EquipmentMaintenance((sender as TileView), ((TileViewItem)e.DataItem).RowHandle);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 设备维护
        /// </summary>
        /// <param name="tv"></param>
        /// <param name="RowHandle"></param>
        void EquipmentMaintenance(TileView tv, int RowHandle)
        {
            //DataRow item = tv.GetDataRow(RowHandle);
            //using (DialogWindow dw = new DialogWindow(item["DriveName"] + "", typeof(page_DriveMaintenance), item))
            //{
            //    dw.ShowDialog();
            //}
        }

        private void tileView1_ContextButtonCustomize(object sender, TileViewContextButtonCustomizeEventArgs e)
        {
            //TileView tv = (sender as TileView);
            //DataRow item = tv.GetDataRow(e.RowHandle);
            //using (DialogWindow dw = new DialogWindow(item["DriveName"] + ""))
            //{
            //    dw.ShowDialog();
            //}
        }

        TileViewItem hoveredItem;

        private void tileView1_MouseMove(object sender, MouseEventArgs e)
        {
            TileViewHitInfo hitInfo = (sender as TileView).CalcHitInfo(e.Location);
            if (hitInfo.HitTest != DevExpress.XtraEditors.TileControlHitTest.Item)
                return;
            if (hitInfo.Item != hoveredItem)
            {
                hoveredItem = hitInfo.Item;
                (sender as TileView).LayoutChanged();
                //DataRow item = ((DataRowView)tileView1.GetRow(hoveredItem.RowHandle)).Row;
            }
        }

        private void toolTipController1_GetActiveObjectInfo(object sender,
            ToolTipControllerGetActiveObjectInfoEventArgs e)
        {
            if (gridControl.MainView != MainView && hoveredItem != null)
            {
                ToolTipControlInfo info = new ToolTipControlInfo(hoveredItem.ItemInfo, "text");
                info.ToolTipType = ToolTipType.SuperTip;
                info.SuperTip = new SuperToolTip();
                info.SuperTip.Items.AddTitle("台式钻床");
                info.SuperTip.Items.Add($"型号规格:Z4116B.");
                info.SuperTip.Items.Add($"制造厂家:常州台钻厂.");
                info.SuperTip.Items.Add($"出厂编号:35759.");
                info.SuperTip.Items.Add($"投产年月:1998.");
                info.SuperTip.Items.Add($"设备总容量(KW):0.55.");
                info.SuperTip.Items.Add($"设备重量(吨):0.2.");
                info.SuperTip.Items.Add($"设备类别:B类.");
                info.SuperTip.Items.Add($"设备原值(万元):0.25.");
                info.SuperTip.Items.Add($"设备状态:运行中.");
                info.SuperTip.Items.Add($"安装地点:KH一楼北.");
                info.SuperTip.Items.Add($"运行参数:∮16mm.");
                e.Info = info;
            }
        }
    }
}