using ESRI.ArcGIS.Controls;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.ExtensionMethod;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.Views;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Kingo.Plugin.MapView.Views
{
    /// 
    /// BrowserLocal.xaml 的交互逻辑
    /// 
    public partial class BrowserLocal : UserControl
    {
        private double dGridWidthNow;
        private bool IsFirstOpen = false;
        private double dGridWidthInitial = 0;
        private double dReduceScale = 0;
        private string sParentCatalogPath = "";
        private string sCurrentCatalogPath = "";
        private string sRootCatalogDescription = "此电脑";
        private string sCurrentShowCatalogPath = "";
        private bool IsRootCatalog;//当前是否是根目录展示信息(true是 false不是)
        public DataTable dtShow = new DataTable("浏览本地");
        public event EventHandler Close;
        private IViewManager m_ViewManager;
        public List ObjectInfoList { get; set; }
        public class ObjectInfo
        {
            /// 
            /// 显示的名称
            /// 
            public string Name { get; set; }
            /// 
            /// 文件所在地址
            /// 
            public string Path { get; set; }
            /// 
            /// 最近修改时间
            /// 
            public string RecentOpenDate { get; set; }
            /// 
            /// 文件对应显示的图片
            /// 
            public BitmapImage FileImage { get; set; }
            /// 
            /// 实际选中文件的名称
            /// 
            public string RealSelectObjectName { get; set; }
            /// 
            /// 后缀名
            /// 
            public string Extension { get; set; }
        }
        public BrowserLocal()
        {
            IsFirstOpen = true;
            dReduceScale = 1;
            InitializeComponent();
            InitData();
            btnBack.IsEnabled = false;
            btnBack.ToolTip = "无上一级目录";
            IsRootCatalog = true;
            lbInfoTemp.Content = sRootCatalogDescription;
            lbInfoTemp.ToolTip = lbInfoTemp.Content;
        }
        private void RefreshGridData()
        {
            try
            {
                ObjectInfoList = new List();
                ObjectInfo OI = new ObjectInfo();
                if (dtShow != null && dtShow.Rows.Count > 0)
                {
                    for (int i = 0; i < dtShow.Rows.Count; i++)
                    {
                        OI = new ObjectInfo();
                        //OI.Name = GetFileShowMsg(dtShow.Rows[i]["objname"].ToString(), dtShow.Rows[i]["objpath"].ToString());
                        OI.Name = GetFileShowMsg(dtShow.Rows[i]["objname"].ToString(), "");
                        OI.Path = dtShow.Rows[i]["objpath"].ToString();
                        OI.RecentOpenDate = dtShow.Rows[i]["objopendate"].ToString();
                        OI.FileImage = GetFileBitmapImage(1);
                        OI.RealSelectObjectName = dtShow.Rows[i]["objname"].ToString();
                        OI.Extension = dtShow.Rows[i]["objextension"].ToString();
                        ObjectInfoList.Add(OI);
                    }
                }
                dgBrowserLocal.ItemsSource = ObjectInfoList;
                btnBack.IsEnabled = false;
                btnBack.ToolTip = "无上一级目录";
                IsRootCatalog = true;
            }
            catch (Exception ex)
            {
                LogAPI.Debug("在 获取展示用Grid数据时失败,异常原因: " + ex + " ; ");
                return;
            }
        }
        /// 
        /// 获取显示的图片类型(0未知 1文件夹 2文件)
        /// 
        /// 获取显示的图片类型(0未知 1文件夹 2文件)
        /// 
        private BitmapImage GetFileBitmapImage(int iImgType)
        {
            try
            {
                string sAppPathNowTemp = SysAppPath.GetCurrentAppPath() + "Plugins\\KGIS.Plugin.MapView\\Resources\\Tool";
                if (string.IsNullOrWhiteSpace(sAppPathNowTemp) == true)
                {
                    return null;
                }
                else
                {
                    string sFolderImagePath = sAppPathNowTemp + "\\folderimg.png";
                    string sFileImagePath = sAppPathNowTemp + "\\fileimg.png";
                    if (File.Exists(sFolderImagePath) == false || File.Exists(sFileImagePath) == false)
                    {
                        return null;
                    }
                    else
                    {
                        if (iImgType == 1)
                        {
                            BitmapImage bitmap = new BitmapImage(new Uri(sFolderImagePath));
                            return bitmap;
                        }
                        else// if (iImgType == 2)
                        {
                            BitmapImage bitmap = new BitmapImage(new Uri(sFileImagePath));
                            return bitmap;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取显示的图片类型(iImgType= " + iImgType + ")时失败,异常原因: " + ex + " ; ");
                return null;
            }
        }
        private string GetFileShowMsg(string sFileName, string sPath)
        {
            string sMark = "";
            string _Path = "";
            try
            {
                if (string.IsNullOrWhiteSpace(sPath) == true)
                {
                    if (string.IsNullOrWhiteSpace(sFileName) == true)
                    {
                        return "";
                    }
                    else
                    {
                        sMark = GetRealShowMessageByReduceScale(sFileName);
                        return sMark;
                    }
                }
                else
                {
                    _Path = GetFileDirectoryName(sPath);
                    if (string.IsNullOrWhiteSpace(sFileName) == true)
                    {
                        sMark = GetRealShowMessageByReduceScale(_Path);
                        return sMark;
                    }
                    else
                    {
                        sMark = GetRealShowMessageByReduceScale(sFileName);
                        sMark += "\r";
                        sMark += GetRealShowMessageByReduceScale(_Path);
                        return sMark;
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取文件展示用内容时失败,异常原因: " + ex + " ; ");
                return "";
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(sMark) == false)
                {
                    sMark = "";
                }
                if (string.IsNullOrWhiteSpace(_Path) == false)
                {
                    _Path = "";
                }
            }
        }
        private string GetRealShowMessageByReduceScale(string sOriginalContent)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sOriginalContent) == true)
                {
                    return "";
                }
                if (dReduceScale <= 0)
                {
                    return "";
                }
                if (dReduceScale > 1)
                {
                    dReduceScale = 1;
                }
                int iLength = sOriginalContent.Length;
                double dTemp = iLength * dReduceScale;
                int iNeedLength = GetNeedLength(dTemp * 1.5);
                if (iNeedLength <= 0)
                {
                    iNeedLength = iLength;
                }
                if (iNeedLength >= iLength)
                {
                    return sOriginalContent;
                }
                else
                {
                    string sMark = sOriginalContent.Substring(0, iNeedLength) + "...";
                    return sMark;
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("按照比例获取显示内容时失败,异常原因: " + ex + " ; ");
                return "";
            }
        }
        private int GetNeedLength(double dTemp)
        {
            try
            {
                return (int)dTemp;//取整数部分
            }
            catch (Exception ex)
            {
                LogAPI.Debug("转化Double数(" + dTemp + ") 时失败,异常原因:" + ex + " ; ");
                return 0;
            }
        }
        private string GetFileDirectoryName(string sPath)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sPath) == true)
                {
                    return "";
                }
                if (File.Exists(sPath) == false)
                {
                    return "";
                }
                string sThePath = System.IO.Path.GetDirectoryName(sPath);
                return sThePath;
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取文件夹路径时失败,异常原因: " + ex + " ; ");
                return "";
            }
        }
        private void InitData()
        {
            dtShow.Columns.Add("objname", typeof(string));
            dtShow.Columns.Add("objpath", typeof(string));
            dtShow.Columns.Add("objopendate", typeof(string));
            dtShow.Columns.Add("objextension", typeof(string));
            ToRefreshGridData();
        }
        private void ToRefreshGridData()
        {
            DriveInfo[] diInfoList = null;
            try
            {
                tbCatalog.Text = sRootCatalogDescription;
                if (string.IsNullOrWhiteSpace(tbCatalog.Text) == false)
                {
                    tbCatalog.SelectionStart = tbCatalog.Text.Length;
                }
                diInfoList = DriveInfo.GetDrives();//获取电脑磁盘符号
                if (diInfoList == null || diInfoList.Count() <= 0)
                {
                    return;
                }
                if (dtShow != null && dtShow.Rows.Count > 0)
                {
                    dtShow.Rows.Clear();
                }
                #region 获取 本地电脑 信息
                for (int y = 0; y < diInfoList.Count(); y++)
                {
                    string name = GetLocalDeviceInfoByType_Drive(diInfoList[y], 1);
                    if (string.IsNullOrWhiteSpace(diInfoList[y].Name.Trim()) || string.IsNullOrWhiteSpace(name.Trim()))
                    {
                        continue;
                    }
                    dtShow.Rows.Add(
                        new object[] {
                        GetLocalDeviceInfoByType_Drive(diInfoList[y],1),//1获取此System.IO.DirectoryInfo实例的名称
                        GetLocalDeviceInfoByType_Drive(diInfoList[y],2),//2获取目录或文件的完整目录
                        GetLocalDeviceInfoByType_Drive(diInfoList[y],3),//3获取或设置上次访问当前文件或目录的时间
                        GetLocalDeviceInfoByType_Drive(diInfoList[y],4)//4获取表示文件扩展名部分的字符串
                        });
                }
                #endregion
                RefreshGridData();
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取展示用 Grid 数据期间失败,异常原因: " + ex + " ; ");
                return;
            }
            finally
            {
                if (diInfoList != null)
                {
                    diInfoList = null;
                }
            }
        }
        /// 
        /// 依据类型获取想要的信息(1获取此System.IO.DirectoryInfo实例的名称 2获取目录或文件的完整目录 3获取或设置上次访问当前文件或目录的时间 4获取表示文件扩展名部分的字符串)
        /// 
        /// 
        /// 想要的信息类型(1获取此System.IO.DirectoryInfo实例的名称 2获取目录或文件的完整目录 3获取或设置上次访问当前文件或目录的时间 4获取表示文件扩展名部分的字符串)
        /// 
        private string GetLocalDeviceInfoByType_Drive(object obj, int iNeedInfoType)
        {
            string sInfo = "";
            try
            {
                if (obj == null)
                {
                    return "";
                }
                if ((obj is DriveInfo) == false)//DirectoryInfo
                {
                    return "";
                }
                DriveInfo dINow = null;
                dINow = obj as DriveInfo;
                if (JudgePathType(dINow.Name) == 0)
                {
                    return "";
                }
                DirectoryInfo di = new DirectoryInfo(dINow.Name);
                switch (iNeedInfoType)
                {
                    case 1:
                        string sTemp = string.IsNullOrWhiteSpace(dINow.VolumeLabel) == false ? dINow.VolumeLabel : "本地磁盘";
                        sInfo = sTemp + " (" + di.Name + ") ";
                        break;
                    case 2:
                        sInfo = di.FullName;//dINow.FullPath;受保护不可读取
                        break;
                    case 3:
                        if (ExtendMethd.JudgeIsCouldConversionDateTime(di.LastAccessTime) == false)
                        {
                            sInfo = "";
                        }
                        else
                        {
                            DateTime dtTemp1 = di.LastAccessTime;
                            sInfo = dtTemp1.Year + "-" + dtTemp1.Month + "-" + dtTemp1.Day + " " + dtTemp1.Hour + ":" + dtTemp1.Minute;
                        }
                        break;
                    case 4:
                        sInfo = di.Extension;
                        break;
                    default:
                        sInfo = "";
                        break;
                }
                return sInfo;
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取 盘符 信息时失败,异常原因: " + ex + " ; ");
                return "";
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(sInfo) == false)
                {
                    sInfo = "";
                }
            }
        }
        /// 
        /// 依据类型获取想要的信息(1获取此 System.IO.DirectoryInfo 实例的名称 2获取目录或文件的完整目录 3获取或设置上次访问当前文件或目录的时间 4获取表示文件扩展名部分的字符串)
        /// 
        /// 
        /// 想要的信息类型(1获取此 System.IO.DirectoryInfo 实例的名称 2获取目录或文件的完整目录 3获取或设置上次访问当前文件或目录的时间 4获取表示文件扩展名部分的字符串)
        /// 
        private string GetLocalDeviceInfoByType_Folder(object obj, int iNeedInfoType)
        {
            string sInfo = "";
            try
            {
                if (ExtendMethd.JudgeIsCouldConversionStringOrHasValue(obj) == false) return "";
                if (JudgePathType(obj.ToString()) != 1) return "";
                DirectoryInfo dir = new DirectoryInfo(obj.ToString());
                switch (iNeedInfoType)
                {
                    case 1:
                        sInfo = dir.Name;
                        break;
                    case 2:
                        sInfo = dir.FullName;
                        break;
                    case 3:
                        if (ExtendMethd.JudgeIsCouldConversionDateTime(dir.LastAccessTime) == false)
                        {
                            sInfo = "";
                        }
                        else
                        {
                            DateTime dtTemp1 = dir.LastAccessTime;
                            sInfo = dtTemp1.Year + "-" + dtTemp1.Month + "-" + dtTemp1.Day + " " + dtTemp1.Hour + ":" + dtTemp1.Minute;
                        }
                        break;
                    case 4:
                        sInfo = dir.Extension;
                        break;
                    default:
                        sInfo = "";
                        break;
                }
                return sInfo;
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取 文件夹 信息时失败,异常原因: " + ex + " ; ");
                return "";
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(sInfo) == false)
                {
                    sInfo = "";
                }
            }
        }
        /// 
        /// 依据类型获取想要的信息(1获取文件名 2获取表示目录的完整路径的字符串 3获取或设置上次访问当前文件或目录的时间 4获取表示文件扩展名部分的字符串)
        /// 
        /// 
        /// 想要的信息类型(1获取文件名 2获取表示目录的完整路径的字符串 3获取或设置上次访问当前文件或目录的时间 4获取表示文件扩展名部分的字符串)
        /// 
        private string GetLocalDeviceInfoByType_File(object obj, int iNeedInfoType)
        {
            string sInfo = "";
            try
            {
                if (ExtendMethd.JudgeIsCouldConversionStringOrHasValue(obj) == false) return "";
                if (JudgePathType(obj.ToString()) != 2) return "";
                FileInfo fINow = new FileInfo(obj.ToString());
                if (fINow == null)
                {
                    return "";
                }
                switch (iNeedInfoType)
                {
                    case 1:
                        sInfo = fINow.Name;
                        break;
                    case 2:
                        sInfo = fINow.DirectoryName;
                        break;
                    case 3:
                        if (ExtendMethd.JudgeIsCouldConversionDateTime(fINow.LastAccessTime) == false)
                        {
                            sInfo = "";
                        }
                        else
                        {
                            DateTime dtTemp1 = fINow.LastAccessTime;
                            sInfo = dtTemp1.Year + "-" + dtTemp1.Month + "-" + dtTemp1.Day + " " + dtTemp1.Hour + ":" + dtTemp1.Minute;
                        }
                        break;
                    case 4:
                        sInfo = fINow.Extension;
                        break;
                    default:
                        sInfo = "";
                        break;
                }
                return sInfo;
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取 文件 信息时失败,异常原因: " + ex + " ; ");
                return "";
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(sInfo) == false) sInfo = "";
            }
        }
        private void DgBrowserLocal_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseEnter += Row_MouseEnter;
        }
        void Row_MouseEnter(object sender, MouseEventArgs e)
        {
            //DataGridRow row = (DataGridRow)sender;
            //row.ToolTip = GetPathByIndex(row.GetIndex());配置较差电脑会导致黑块 暂时移除提示
        }        
        private void DgBrowserLocal_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                btnBack.IsEnabled = true;
                btnBack.ToolTip = "返回至上一级";
                IsRootCatalog = false;
                if (dgBrowserLocal.SelectedItem == null)
                {
                    return;
                }
                else
                {
                    ObjectInfo objInfo = dgBrowserLocal.SelectedItem as ObjectInfo;
                    if (objInfo == null)
                    {
                        return;
                    }
                    else
                    {
                        if (ExtendMethd.JudgeIsCouldConversionStringOrHasValue(objInfo.Path) == false)
                        {
                            LogAPI.Debug("浏览本地操作期间双击选中项后 Path 信息获取失败");
                            MessageHelper.ShowTips("信息获取失败!");
                            return;
                        }
                        if (ExtendMethd.JudgeIsCouldConversionStringOrHasValue(objInfo.RealSelectObjectName) == false)
                        {
                            LogAPI.Debug("浏览本地操作期间双击选中项后 RealSelectObjectName 信息获取失败,地址为:" + objInfo.Path + " ; ");
                            MessageHelper.ShowTips("信息获取失败!");
                            return;
                        }
                        string sCurrentPathTemp = "";
                        if (objInfo.Extension == null)
                        {
                            sCurrentPathTemp = objInfo.Path;
                            RefreshBrowserLocalDataGrid(objInfo.Path);
                        }
                        else
                        {
                            if (string.IsNullOrWhiteSpace(objInfo.Extension) == true)
                            {
                                sCurrentPathTemp = objInfo.Path;
                                RefreshBrowserLocalDataGrid(objInfo.Path);
                            }
                            else
                            {                                
                                //打开工程
                                string sThePathNow = objInfo.Path + "\\" + objInfo.RealSelectObjectName;
                                if (File.Exists(sThePathNow) == false)
                                {
                                    sCurrentPathTemp = "";
                                    LogAPI.Debug("未能找到路径为 " + sThePathNow + " 的文件或者文件夹命名不要包含‘.’");
                                    MessageHelper.ShowTips("未能找到路径为 " + sThePathNow + " 的文件或者文件夹命名不要包含‘.’");
                                    return;
                                }
                                else
                                {
                                    sCurrentPathTemp = sThePathNow;
                                    OpenProject(sThePathNow);
                                    KGIS.Framework.Maps.MapsManager.Instance.MapService.SetFolderPath(objInfo.Path);//获取工作目录路径
                                    Platform.Instance.ViewManager.SetFilePath(objInfo.Path);
                                }
                            }
                        }
                        SetParentCatalogPath(sCurrentPathTemp);
                        tbCatalog.Text = string.IsNullOrWhiteSpace(sCurrentPathTemp) == false ? sCurrentPathTemp.Replace("\\", " > ") : " ";
                        sCurrentShowCatalogPath = tbCatalog.Text;
                        lbInfoTemp.Content = tbCatalog.Text;
                        lbInfoTemp.ToolTip = lbInfoTemp.Content;
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("双击浏览文本地电脑文件地址时失败,异常原因:" + ex + " ; ");
                return;
            }
        }
        /// 
        /// 获取父级目录地址
        /// 
        /// 
        private void SetParentCatalogPath(string sCurrentPath)
        {
            DirectoryInfo lastLevel_DirectoryInfo = null;
            try
            {
                #region 获取上一级目录地址
                if (string.IsNullOrWhiteSpace(sCurrentPath) == true)
                {
                    LogAPI.Debug("获取父级目录地址失败,原因:当前选择的文件 当前所在目录 信息获取失败");
                    return;
                }
                lastLevel_DirectoryInfo = Directory.GetParent(sCurrentPath);
                if (lastLevel_DirectoryInfo == null)//第一级系统盘符的情况
                {
                    sParentCatalogPath = "";
                }
                else
                {
                    sParentCatalogPath = lastLevel_DirectoryInfo.FullName;
                }
                //if (string.IsNullOrWhiteSpace(sParentCatalogPath) == false)
                //{
                //    btnBack.IsEnabled = true;
                //}
                //else
                //{
                //    btnBack.IsEnabled = false;
                //}
                //LogAPI.Debug("上一级目录 地址 现为: " + sParentCatalogPath + " ; ");
                #endregion
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取父级目录地址失败,\r");
                LogAPI.Debug("当前所选文件所在地址: " + sCurrentPath + " \r");
                LogAPI.Debug("原因: " + ex + " ; ");
                sParentCatalogPath = "";
                return;
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(sCurrentPath) == false)
                {
                    sCurrentPath = "";
                }
                if (lastLevel_DirectoryInfo != null)
                {
                    lastLevel_DirectoryInfo = null;
                }
            }
        }
        /// 
        /// 打开工程操作
        /// 
        /// 
        private void OpenProject(string sPath)
        {
            string sProjectPath = "";
            try
            {
                EngineEditorClass engineEditorClass = new EngineEditorClass();
                if (engineEditorClass.EditState != esriEngineEditState.esriEngineStateNotEditing)
                {
                    MessageHelper.ShowTips("当前工程正处于编辑状态,打开工程之前请先结束编辑!");
                    this.Close?.Invoke(null, null);
                    return;
                }
                sProjectPath = sPath;
                if (string.IsNullOrWhiteSpace(sProjectPath) == true)
                {
                    return;
                }
                string prjType = ".KBG";
                switch (Platform.Instance.SystemType)
                {
                    case SystemTypeEnum.NDBGJK:
                        prjType = ".KBG";
                        break;
                    case SystemTypeEnum.WYZS:
                        prjType = ".KBG";
                        break;
                    case SystemTypeEnum.TBBG:
                        prjType = ".KAP";
                        break;
                    case SystemTypeEnum.DTBJK:
                        prjType = ".KBG";
                        break;
                    case SystemTypeEnum.YCLJK:
                        prjType = ".KBG";
                        break;
                    default:
                        break;
                }
                //打开文件是否与当前应用程序相符
                if (prjType.ToLower() != System.IO.Path.GetExtension(sProjectPath).ToLower())
                {
                    MessageHelper.ShowError("与当前工程文件不符,请选择正确文件!");
                    return;
                }
                if (File.Exists(sProjectPath) == false)
                {
                    LogAPI.Debug("未能找到 地址为 " + sProjectPath + " 的 KBG工程文件。");
                    return;
                }
                else
                {
                    //切换前关闭所有的未关闭的窗体
                    m_ViewManager = Platform.Instance.ViewManager;
                    m_ViewManager.CloseAllDocument();
                    m_ViewManager.CloseAllPanel();
                    KGIS.Framework.Maps.MapsManager.Instance.MapService.LoadProject(sProjectPath);//打开工程
                    this.Close?.Invoke(null, null);
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("在点击打开最近工程时失败,KBG工程文件地址: " + sProjectPath + ", 原因:" + ex + " ; ");
                MessageHelper.ShowError("工程打开 失败");
                return;
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(sProjectPath) == false)
                    sProjectPath = "";
            }
        }
        private void RefreshBrowserLocalDataGrid(string sPath)
        {
            try
            {
                dgBrowserLocal.ItemsSource = null;
                if (string.IsNullOrWhiteSpace(sPath) == true)
                {
                    return;
                }
                int iObjectType = JudgePathType(sPath);
                if (iObjectType == 0 || iObjectType == 2)
                {
                    return;
                }
                else if (iObjectType == 1)
                {
                    #region 点开了 文件夹
                    List AllNeedValidPathList = new List();
                    List ValidPathList = new List();
                    string[] ArrayDirectories = System.IO.Directory.GetDirectories(sPath);
                    if (ArrayDirectories != null && ArrayDirectories.Count() > 0)
                    {
                        for (int i = 0; i < ArrayDirectories.Count(); i++)
                        {
                            if (JudgeDirectoryIsNeedShow(ArrayDirectories[i]) == true)
                            {
                                ValidPathList.Add(ArrayDirectories[i]);
                            }
                        }
                    }
                    AllNeedValidPathList = ValidPathList;
                    string prjType = "*.KAP";
                    switch (Platform.Instance.SystemType)
                    {
                        case SystemTypeEnum.NDBGJK:
                            prjType = "*.KBG";
                            break;
                        case SystemTypeEnum.BGFWCG:
                            prjType = "*.prj";
                            break;
                        case SystemTypeEnum.TBBG:
                            prjType = "*.KAP";
                            break;
                        default:
                            break;
                    }
                    string[] ArrayKBGFiles = System.IO.Directory.GetFiles(sPath, prjType);
                    List listKBGFiles = new List(ArrayKBGFiles);
                    for (int j = 0; j < listKBGFiles.Count; j++)
                    {
                        AllNeedValidPathList.Add(listKBGFiles[j]);
                    }
                    ObjectInfoList = new List();
                    ObjectInfo OI = new ObjectInfo();
                    int iPathTypeNow = 0;
                    string sNameTemp = "";
                    string sDirectoryNameTemp = "";
                    string sLastAccessTimeTemp = "";
                    string sExtension = "";
                    for (int k = 0; k < AllNeedValidPathList.Count; k++)
                    {
                        iPathTypeNow = JudgePathType(AllNeedValidPathList[k]);
                        if (iPathTypeNow == 0)
                        {
                            continue;
                        }
                        else
                        {
                            sNameTemp = "";
                            sDirectoryNameTemp = "";
                            sLastAccessTimeTemp = "";
                            OI = new ObjectInfo();
                            if (iPathTypeNow == 1)//文件夹
                            {
                                OI.FileImage = GetFileBitmapImage(1);
                                sNameTemp = GetLocalDeviceInfoByType_Folder(AllNeedValidPathList[k], 1);
                                sDirectoryNameTemp = GetLocalDeviceInfoByType_Folder(AllNeedValidPathList[k], 2);
                                sLastAccessTimeTemp = GetLocalDeviceInfoByType_Folder(AllNeedValidPathList[k], 3);
                                sExtension = GetLocalDeviceInfoByType_Folder(AllNeedValidPathList[k], 4);
                            }
                            else if (iPathTypeNow == 2)//文件
                            {
                                OI.FileImage = GetFileBitmapImage(2);
                                sNameTemp = GetLocalDeviceInfoByType_File(AllNeedValidPathList[k], 1);
                                sDirectoryNameTemp = GetLocalDeviceInfoByType_File(AllNeedValidPathList[k], 2);
                                sLastAccessTimeTemp = GetLocalDeviceInfoByType_File(AllNeedValidPathList[k], 3);
                                sExtension = GetLocalDeviceInfoByType_File(AllNeedValidPathList[k], 4);
                            }
                            else
                            {
                                OI.FileImage = GetFileBitmapImage(0);
                            }
                            OI.Name = GetFileShowMsg(sNameTemp, "");
                            OI.Path = sDirectoryNameTemp;
                            OI.RecentOpenDate = sLastAccessTimeTemp;
                            OI.RealSelectObjectName = sNameTemp;
                            OI.Extension = sExtension;
                            ObjectInfoList.Add(OI);
                        }
                    }
                    dgBrowserLocal.ItemsSource = ObjectInfoList;
                    btnBack.IsEnabled = true;
                    btnBack.ToolTip = "返回至上一级";//无上一级目录
                    IsRootCatalog = false;
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("更新 浏览本地 文件资源时失败,异常原因:" + ex + " ; ");
                return;
            }
        }
        /// 
        /// 依据路径判断类型(0未知 1文件夹 2文件)
        /// 
        /// 
        /// 
        private int JudgePathType(string sPath)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sPath) == true)
                {
                    return 0;
                }
                string path = sPath;//@"D:\aaa";
                if (Directory.Exists(path))
                {
                    return 1;
                }
                else
                {
                    if (File.Exists(path))
                    {
                        return 2;
                    }
                    else
                    {
                        LogAPI.Debug("路径(" + sPath + ")判断时验证为无效。 ");
                        return 0;
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("依据路径(" + sPath + ")判断类型失败,异常原因: " + ex + " ; ");
                return 0;
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(sPath) == false) sPath = "";
            }
        }
        private bool JudgeDirectoryIsNeedShow(string sPath)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sPath) == true)
                {
                    return false;
                }
                DirectoryInfo di = new DirectoryInfo(sPath);
                if ((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                {//为隐藏的
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("判断目录地址 (" + sPath + ") 是否需要显示 失败,异常原因: " + ex + " ; ");
                return false;
            }
        }
        private void DgBrowserLocal_SizeChanged(object sender, SizeChangedEventArgs e)
        {
        }
        private void BtnBack_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sParentCatalogPath) == true)
                {
                    //MessageBox.Show("上一级地址信息获取失败。");
                    ToRefreshGridData();//将第一级盘符列表信息刷新显示
                }
                else
                {
                    tbCatalog.Text = string.IsNullOrWhiteSpace(sParentCatalogPath) == false ? sParentCatalogPath.Replace("\\", " > ") : " ";
                    sCurrentShowCatalogPath = tbCatalog.Text;
                    RefreshBrowserLocalDataGrid(sParentCatalogPath);
                    SetParentCatalogPath(sParentCatalogPath);
                }
                lbInfoTemp.Content = tbCatalog.Text;
                lbInfoTemp.ToolTip = lbInfoTemp.Content;
            }
            catch (Exception ex)
            {
                LogAPI.Debug("返回父级目录时失败,异常原因: " + ex + " ; ");
                return;
            }
        }
        private void TbCatalog_KeyDown(object sender, KeyEventArgs e)
        {
            string stbCatalogText = "";
            try
            {
                if (e.Key == Key.Enter)
                {
                    stbCatalogText = tbCatalog.Text;
                    if (string.IsNullOrWhiteSpace(stbCatalogText) == true)
                    {
                        MessageHelper.ShowTips("跳转的目录地址不能为空!");
                        if (IsRootCatalog == true)
                        {
                            btnBack.IsEnabled = false;
                            btnBack.ToolTip = "无上一级目录";//返回至上一级
                            tbCatalog.Text = sRootCatalogDescription;
                            if (string.IsNullOrWhiteSpace(tbCatalog.Text) == false)
                            {
                                tbCatalog.SelectionStart = tbCatalog.Text.Length;
                            }
                        }
                        else
                        {
                            btnBack.IsEnabled = true;
                            btnBack.ToolTip = "返回至上一级";//无上一级目录
                            if (string.IsNullOrWhiteSpace(sCurrentShowCatalogPath) == true)
                            {
                                tbCatalog.Text = sRootCatalogDescription;
                                if (string.IsNullOrWhiteSpace(tbCatalog.Text) == false)
                                {
                                    tbCatalog.SelectionStart = tbCatalog.Text.Length;
                                }
                            }
                            else
                            {
                                tbCatalog.Text = sCurrentShowCatalogPath;
                            }
                            if (string.IsNullOrWhiteSpace(tbCatalog.Text) == false)
                            {
                                tbCatalog.SelectionStart = tbCatalog.Text.Length;
                            }
                        }
                    }
                    else
                    {
                        if (stbCatalogText == sRootCatalogDescription)
                        {
                            ToRefreshGridData();
                            return;
                        }
                        else
                        {
                            string stbCatalogTextTemp = DealWithCatalogCharInfo(stbCatalogText);
                            stbCatalogText = string.IsNullOrWhiteSpace(stbCatalogTextTemp) == false ? stbCatalogTextTemp.Replace(">", "\\") : " ";
                            if (string.IsNullOrWhiteSpace(stbCatalogText) == true)
                            {
                                return;
                            }
                            if (Directory.Exists(stbCatalogText) == true)
                            {
                                RefreshBrowserLocalDataGrid(stbCatalogText);
                                SetParentCatalogPath(stbCatalogText);
                                tbCatalog.Text = stbCatalogTextTemp;
                                if (string.IsNullOrWhiteSpace(tbCatalog.Text) == false)
                                {
                                    tbCatalog.SelectionStart = tbCatalog.Text.Length;
                                }
                            }
                            else
                            {
                                MessageHelper.ShowTips("输入的目录地址无效,跳转失败!");
                                tbCatalog.Text = ExtendMethd.JudgeIsCouldConversionStringOrHasValue(lbInfoTemp.Content) == true ? lbInfoTemp.Content.ToString() : "";
                                if (string.IsNullOrWhiteSpace(tbCatalog.Text) == false)
                                {
                                    tbCatalog.SelectionStart = tbCatalog.Text.Length;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("输入目录获取内容期间失败,异常原因: " + ex + " ; ");
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(stbCatalogText) == false)
                {
                    stbCatalogText = "";
                }
            }
        }
        private string DealWithCatalogCharInfo(string sCatalogInfo)
        {
            string sTempInfo = "";
            string[] aArrayTemp = null;
            try
            {
                if (string.IsNullOrWhiteSpace(sCatalogInfo) == true)
                {
                    return "";
                }
                else
                {
                    aArrayTemp = sCatalogInfo.Split('>');
                    if (aArrayTemp == null || aArrayTemp.Count() <= 0)
                    {
                        return sCatalogInfo;
                    }
                    else
                    {
                        for (int i = 0; i < aArrayTemp.Count(); i++)
                        {
                            if (ExtendMethd.JudgeIsCouldConversionStringOrHasValue(aArrayTemp[i]) == false)
                            {
                                continue;
                            }
                            else
                            {
                                sTempInfo += aArrayTemp[i].ToString().TrimStart().TrimEnd() + ">";
                            }
                        }
                        if (string.IsNullOrWhiteSpace(sTempInfo) == true)
                        {
                            return "";
                        }
                        else
                        {
                            sTempInfo = sTempInfo.TrimStart('>').TrimEnd('>');
                            //LogAPI.Debug("转化后地址: " + sTempInfo + " ; ");
                            return sTempInfo;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug("处理输入的目录地址信息时失败,异常原因:" + ex + " ; ");
                return "";
            }
            finally
            {
                if (aArrayTemp != null && aArrayTemp.Count() <= 0)
                {
                    aArrayTemp = null;
                }
                if (string.IsNullOrWhiteSpace(sTempInfo) == false)
                {
                    sTempInfo = "";
                }
            }
        }
        private void TbCatalog_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                var bc = new BrushConverter();
                tbCatalog.Background = (Brush)bc.ConvertFrom("#CFCFCF");
            }
            catch (Exception ex)
            {
                LogAPI.Debug("鼠标移动时给 TextBox 赋值颜色失败,异常原因: " + ex + " ; ");
                return;
            }
        }
        private void TbCatalog_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                var bc = new BrushConverter();
                tbCatalog.Background = (Brush)bc.ConvertFrom("#F2F2F2");
            }
            catch (Exception ex)
            {
                LogAPI.Debug("鼠标离开时给 TextBox 赋值颜色失败,异常原因: " + ex + " ; ");
                return;
            }
        }
    }
}