using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using KGIS.Framework.AE.ExtensionMethod;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.Views;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UIShell.OSGi;
namespace Kingo.Plugin.MapView.Views
{
    /// 
    /// V_AddLayerGroup.xaml 的交互逻辑
    /// 
    public partial class V_AddLayerGroup : BaseWindow
    {
        private IHookHelper _Hook { get; set; }
        private IDataCatalogService _DataCatalog = null;
        public V_AddLayerGroup(IHookHelper hook)
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.ShowInTaskbar = false;
            this._Hook = hook;
        }
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_DataCatalog == null)
                    _DataCatalog = BundleRuntime.Instance.GetFirstOrDefaultService();
                if (_DataCatalog == null)
                    return;
                if (_Hook == null)
                {
                    return;
                }
                if (string.IsNullOrWhiteSpace(txtGroupName.Text))
                {
                    MessageHelper.Show("请输入分组名称!");
                    return;
                }
                object pTargetObj = _Hook.GetCustomProperty();
                if (pTargetObj is IBasicMap)
                {
                    ILayer groupLayer = new GroupLayer() { Name = txtGroupName.Text };
                    ILayerGeneralProperties layerGeneralProperties = groupLayer as ILayerGeneralProperties;
                    layerGeneralProperties.LayerDescription = txtGroupName.Text;
                    LayerCfg GroupLayerInfo = _DataCatalog.AddLayer(groupLayer) as LayerCfg;
                    //IGroupLayer group = new GroupLayerClass();
                    //group.Name = txtGroupName.Text;
                    //_Hook.FocusMap.AddLayer(group);
                }
                else if (pTargetObj is IGroupLayer)//暂时没有分组下添加分组
                {
                    ILayer layerQS = MapsManager.Instance.MapService.GetGroupLayer((pTargetObj as ILayerGeneralProperties).LayerDescription);//获取参考数据图层组
                    LayerCfg GroupLayerInfo = null;
                    if (_DataCatalog != null)
                    {
                        _DataCatalog.GetNodeByLayer(layerQS);
                        LayerCfg rootLayer = _DataCatalog.CurrentLayers as LayerCfg;
                        List AllLayers = rootLayer.GetAllItem();
                        GroupLayerInfo = AllLayers.FirstOrDefault(f => f.LayerName == layerQS.Name);
                    }
                    ILayer groupLayer = new GroupLayer() { Name = txtGroupName.Text };
                    ILayerGeneralProperties layerGeneralProperties = groupLayer as ILayerGeneralProperties;
                    layerGeneralProperties.LayerDescription = txtGroupName.Text;
                    _DataCatalog.AddLayer(groupLayer, GroupLayerInfo);
                    //IGroupLayer group = new GroupLayerClass();
                    //group.Name = txtGroupName.Text;
                    //(pTargetObj as IGroupLayer).Add(group);
                }
                _DataCatalog.UpdateTree();
                Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { MsgType = "SaveProject" });
                this.Close();
                //MapsManager.Instance.MapService.CtlsSynchronizer.ActivateMap();
                //_Hook.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
            catch (Exception ex)
            {
                MessageHelper.ShowError("添加图层组失败!");
                LogAPI.Debug("添加图层组异常:" + ex);
            }
        }
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}