using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
namespace Kingo.Plugin.EngineEditor.Common
{
    /// 
    /// 图层符号化的相关操作的API函数
    /// 
    public static class LabelAPI
    {      
        ///
        /// 获取符号信息
        ///    
        /// 要素渲染
        /// 
        /// 符号集合
        /// 
        public static bool GetSymbolInfos(IFeatureRenderer pFeatureRenderer, IFeatureCursor pFeatureCursor, IDictionary pDic, DataTable dtZD = null, string featurename = null)
        {
            if (pFeatureRenderer == null || pFeatureCursor == null || pDic == null)
            {
                return false;
            }
            IUniqueValueRenderer pUniqueValueRenderer = pFeatureRenderer as IUniqueValueRenderer;
            if (pUniqueValueRenderer == null)
            {
                return true;
            }
            int iFieldCount = pUniqueValueRenderer.FieldCount;
            int[] intArray = new int[iFieldCount];
            for (int i = 0; i < iFieldCount; i++)
            {
                intArray[i] = pFeatureCursor.FindField(pUniqueValueRenderer.get_Field(i));
            }
            string sFieldDelimiter = pUniqueValueRenderer.FieldDelimiter;
            StringBuilder stringBuilder = new StringBuilder();
            string sValue, sLabel, sHead;
            IFeature pFeature = null;
            IDictionary pDictionaryTemp = new Dictionary();
            Dictionary lstString = new Dictionary();
            while ((pFeature = pFeatureCursor.NextFeature()) != null)
            {
                //支持多字段唯一值渲染
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Remove(0, stringBuilder.Length);
                }
                object obj = pFeature.get_Value(intArray[0]);
                if (obj == null || string.IsNullOrWhiteSpace(obj.ToString()) || lstString.Keys.Contains(obj.ToString()))
                {
                    continue;
                }
                if (featurename.ToUpper() == "DLTB")
                {
                    lstString.Add(obj.ToString(), pFeature.get_Value(pFeatureCursor.FindField("DLMC")).ToString());
                }
                else
                {
                    lstString.Add(obj.ToString(), obj.ToString());
                }
                stringBuilder.Append(obj);
                for (int i = 1; i < iFieldCount; i++)
                {
                    stringBuilder.Append(sFieldDelimiter);
                    stringBuilder.Append(pFeature.get_Value(i));
                }
                sValue = stringBuilder.ToString();
                if (string.IsNullOrWhiteSpace(sValue))
                {
                    continue;
                }
                try
                {
                    sLabel = pUniqueValueRenderer.get_Label(sValue);
                    try
                    {
                        //查找唯一值是否存在引用值
                        sValue = pUniqueValueRenderer.get_ReferenceValue(sValue);
                    }
                    catch
                    {
                    }
                    if (sLabel != null && !pDictionaryTemp.ContainsKey(sLabel))
                    {
                        pDictionaryTemp.Add(sLabel, pUniqueValueRenderer.get_Symbol(sValue));
                    }
                }
                catch (Exception ex)
                {
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeature);
            }
            //lstString.Clear();
            //lstString = null;
            pDic.Clear();
            //按唯一值渲染类中的符号顺序重新加载符号
            for (int i = 0; i < pUniqueValueRenderer.ValueCount; i++)
            {
                sValue = pUniqueValueRenderer.get_Value(i);
                sLabel = pUniqueValueRenderer.get_Label(sValue);
                sHead = pUniqueValueRenderer.get_Heading(sValue);
                if (!pDictionaryTemp.ContainsKey(sLabel))
                {
                    continue;
                }
                if (dtZD == null || dtZD.Rows.Count <= 0)
                {
                    if (featurename.ToUpper() == "DLTB")
                    {
                        string sLabel2 = lstString.FirstOrDefault(m => m.Key == sValue).Value;
                        pDic.Add(sLabel2, pDictionaryTemp[sLabel]);
                    }
                    else
                    {
                        pDic.Add(sLabel, pDictionaryTemp[sLabel]);
                    }
                    continue;
                }
                if (sHead == "JXLX")
                {
                    if (sLabel == "620200")
                    {
                        pDic.Add("国界", pDictionaryTemp[sLabel]);
                    }
                    else if (sLabel == "630200")
                    {
                        pDic.Add("省界", pDictionaryTemp[sLabel]);
                    }
                    else if (sLabel == "640200")
                    {
                        pDic.Add("地市界", pDictionaryTemp[sLabel]);
                    }
                    else if (sLabel == "650200")
                    {
                        pDic.Add("县界", pDictionaryTemp[sLabel]);
                    }
                    else if (sLabel == "660200")
                    {
                        pDic.Add("乡界", pDictionaryTemp[sLabel]);
                    }
                    else if (sLabel == "670500")
                    {
                        pDic.Add("村界", pDictionaryTemp[sLabel]);
                    }
                    else
                    {
                        DataRow[] drs = dtZD.Select("Type='" + sHead + "' AND Code='" + sLabel + "'");
                        if (drs != null && drs.Length == 1 && !string.IsNullOrEmpty(drs[0]["Name"] as string))
                        {
                            pDic.Add(drs[0]["Name"].ToString(), pDictionaryTemp[sLabel]);
                        }
                        else
                        {
                            pDic.Add(sLabel, pDictionaryTemp[sLabel]);
                        }
                    }
                }
                else
                {
                    DataRow[] drs = dtZD.Select("Type='" + sHead + "' AND Code='" + sLabel + "'");
                    if (drs != null && drs.Length == 1 && !string.IsNullOrEmpty(drs[0]["Name"] as string))
                    {
                        pDic.Add(drs[0]["Name"].ToString(), pDictionaryTemp[sLabel]);
                    }
                    else
                    {
                        pDic.Add(sLabel, pDictionaryTemp[sLabel]);
                    }
                }
            }
            pDictionaryTemp.Clear();
            pDictionaryTemp = null;
            return true;
        }
    }
}