using DevExpress.XtraBars.ToolbarForm;
using DevExpress.XtraTreeList.Nodes;
using DeviceRepair.Models;
using DeviceRepairAndOptimization.Biz;
using DeviceRepairAndOptimization.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages.DriveMaintenance
{
    public partial class pageRouteAssign : ToolbarForm
    {
        public DeviceRouteInfo CurrentSelectModel = null;
        List<DeviceRouteInfo> routes = null;
        int CurrentRoute = 0;

        public pageRouteAssign(int Route = 0)
        {
            InitializeComponent();
            CurrentRoute = Route;
            this.Load += PageRouteAssign_Load;
        }

        private void PageRouteAssign_Load(object sender, EventArgs e)
        {
            InitializeTreeDatas();

            if (CurrentRoute != 0)
            {
                //treeList1.SetNodeCheckState(treeList1.FindNodeByKeyID(CurrentRoute), CheckState.Checked);
                foreach (TreeListNode node in treeList1.Nodes)
                {
                    SelectNodeByID(node, CurrentRoute);
                }
            }
        }

        private void SelectNodeByID(TreeListNode node, int targetID)
        {
            // 假设ID存储在节点的第一个列（索引0）
            if (node.GetValue("AutoID")?.ToString() == targetID.ToString())
            {
                treeList1.SetNodeCheckState(node, CheckState.Checked);
                treeList1.SetFocusedNode(node);
                //node.Checked = true;
                return;
            }

            // 递归处理子节点
            foreach (TreeListNode childNode in node.Nodes)
            {
                SelectNodeByID(childNode, targetID);
            }
        }

        void InitializeTreeDatas()
        {
            try
            {
                APIResponseData apiResponseData = DeviceManager.Instance.GetDeviceRoute();
                if (apiResponseData.Code != 1)
                {
                    splashScreenManager1.CloseWaitForm();
                    XtraMessageBoxHelper.Error(apiResponseData.Message);
                    return;
                }
                routes = apiResponseData.ToDeserializeObject<List<DeviceRouteInfo>>();

                treeList1.DataSource = routes;
                treeList1.KeyFieldName = "AutoID";
                treeList1.ParentFieldName = "ParentID";
                treeList1.ExpandAll();
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void treeList1_CustomDrawNodeCheckBox(object sender, DevExpress.XtraTreeList.CustomDrawNodeCheckBoxEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                e.Handled = true;
                return;
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<TreeListNode> treeListNodes = treeList1.GetAllCheckedNodes();
            if ((treeListNodes?.Count ?? 0) > 0)
            {
                TreeListNode node = treeListNodes.FirstOrDefault(x => x.HasChildren == false);
                if (node == null)
                {
                    XtraMessageBoxHelper.Warn("选中数据不能为空！");
                    return;
                }

                CurrentSelectModel = routes.FirstOrDefault(x => x.AutoID.ToString() == node.GetValue("AutoID").ToString());
                if (CurrentSelectModel == null)
                {
                    XtraMessageBoxHelper.Warn("选中数据不能为空！");
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                XtraMessageBoxHelper.Warn("选中数据不能为空！");
            }
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            treeList1.UncheckAll();
            e.Node.Checked = true;
        }
    }
}
