using DevExpress.Xpf.Core;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.ExtensionMethod;
using Kingo.Plugin.EngineEditor.Model;
using KGIS.Framework.Utils.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
namespace Kingo.Plugin.EngineEditor.Views
{
    /// 
    /// FrmDrawDoublePolyline.xaml 的交互逻辑
    /// 
    public partial class FrmDrawDoublePolyline : DXWindow
    {
        private DrawParam DrawParam = new DrawParam();
        public delegate void StartDrawHandler(DrawParam drawParam);
        /// 
        /// 开始绘制事件
        /// 
        public event StartDrawHandler StartDrawEvent;
        public FrmDrawDoublePolyline()
        {
            InitializeComponent();
            //绘制方式(默认左右两边绘制)
            DrawParam.DrawMode = DrawMode.LeftAndRight;
            DrawParam.Width = 10;
            DrawParam.OutType = "Polyline";
            DrawParam.DLBM = "1004-城镇村道路用地";
            //FrmDrawDoublePolyline fddpl = new FrmDrawDoublePolyline();
            //fddpl.Show();
        }
        public FrmDrawDoublePolyline(DrawParam pDrawParam, int p = 0)
        {
            InitializeComponent();
            if (p == -1)
            {
                paraXWKD.Visibility = System.Windows.Visibility.Collapsed;
                spAutoCheckbox.Visibility = System.Windows.Visibility.Collapsed;
                //this.Height = 75;
            }
            //else
            //{
            //    spAutoCheckbox.Visibility = System.Windows.Visibility.Visible;
            //    this.Height = 95;
            //}
            List dic = new List();
            dic.Add(new DataDicTionary() { CODE = "LeftAndRight", NAME = "中心线" });
            dic.Add(new DataDicTionary() { CODE = "Left", NAME = "左边线" });
            dic.Add(new DataDicTionary() { CODE = "Right", NAME = "右边线" });
            combDrawMode.ItemsSource = dic;
            //绘制方式(默认左右两边绘制)
            this.DrawParam = pDrawParam;
            if (string.IsNullOrWhiteSpace(this.DrawParam.DLBM))
            {
                this.DrawParam.DLBM = "1004-城镇村道路用地";
            }
            this.DataContext = this.DrawParam;
            //记录之前选中过的地类编码
            if (this.DrawParam != null && !string.IsNullOrWhiteSpace(this.DrawParam.DLBM))
            {
                for (int i = 0; i < cmbDL.Items.Count; i++)
                {
                    ComboBoxItem comBox = cmbDL.Items[i] as ComboBoxItem;
                    if (comBox.Content.Equals(DrawParam.DLBM))
                    {
                        cmbDL.SelectedIndex = i;
                    }
                }
            }
            this.cbAutoClose.IsChecked = DrawParam.IsAutoClose;
            this.cbAutoStep.IsChecked = DrawParam.IsAutoStep;
            this.cbAutoTrack.IsChecked = DrawParam.IsAutoTrack;
            //有记录值时再赋值,防止值变化导致变更事件判断错误
            if (DrawParam.SetWidth >= 0.1 && DrawParam.SetWidth <= 10)
            {
                this.txtSetWidth.Text = DrawParam.SetWidth.ToString();
            }
            //给出DrawParam里微调值的默认值
            else
            {
                DrawParam.SetWidth = this.txtSetWidth.Text.Trim().ToDouble();
            }
            this.SetParam(false, true);
            this.ShowInMainForm();
            //this.spinScale.EditValue = this.DrawParam.ToloranceScale;
        }
        //public void SetVisible(bool isVisible)
        //{
        //    if (isVisible)
        //    {
        //        cmbDL.Visibility = System.Windows.Visibility.Visible;
        //        cbAutoClose.Visibility = System.Windows.Visibility.Collapsed;
        //    }
        //    else
        //    {
        //        cbAutoClose.Visibility = System.Windows.Visibility.Visible;
        //        cmbDL.Visibility = System.Windows.Visibility.Collapsed;
        //    }
        //}
        public void SetParam(bool IsImgIdentity, bool IsPolygon)
        {
            try
            {
                if (IsImgIdentity)
                {
                    paraAutoIdentity.Visibility = System.Windows.Visibility.Visible;
                    paraXWKD.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    paraAutoIdentity.Visibility = System.Windows.Visibility.Collapsed;
                    paraXWKD.Visibility = System.Windows.Visibility.Visible;
                }
                if (IsPolygon)
                {
                    cmbDL.Visibility = System.Windows.Visibility.Visible;
                    cbAutoClose.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    cbAutoClose.Visibility = System.Windows.Visibility.Visible;
                    cmbDL.Visibility = System.Windows.Visibility.Collapsed;
                }
                if (!IsImgIdentity && IsPolygon)
                {
                    laSetWidth.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    laSetWidth.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                //Common.Utility.LogAPI.Debug(ex);
                LogAPI.Debug("画双线期间 设置参数 时异常,异常信息如下:");
                LogAPI.Debug(ex);
                LogAPI.Debug("画双线期间 设置参数 时异常信息结束");
            }
        }
        /// 
        /// 控制控件状态
        /// 
        /// 
        /// 
        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            DrawParam.DrawMode = (DrawMode)System.Enum.Parse(typeof(DrawMode), btn.Tag.ToString());
        }
        /// 
        /// 开始绘制按钮事件
        /// 
        /// 
        /// 
        private void btnStartDraw_Click(object sender, RoutedEventArgs e)
        {
            //DrawParam.IsAutoClose = cbAutoClose.IsChecked == true ? true : false;
            if (StartDrawEvent != null)
            {
                StartDrawEvent(DrawParam);
            }
        }
        private void OutTypeRadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            DrawParam.OutType = btn.Tag.ToString();
            switch (btn.Tag.ToString())
            {
                case "Polyline":
                    cbAutoClose.IsEnabled = true;
                    break;
                case "Polygon":
                    cbAutoClose.IsEnabled = false;
                    cbAutoClose.IsChecked = true;
                    break;
                default:
                    break;
            }
        }
        private void cbAutoClose_Click(object sender, RoutedEventArgs e)
        {
            DrawParam.IsAutoClose = cbAutoClose.IsChecked == true ? true : false;
        }
        private void spinScale_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {
            //this.DrawParam.ToloranceScale = (double)spinScale.Value;
        }
        private void combDrawMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //this.DrawParam.DrawMode = (DrawMode)Enum.Parse(typeof(DrawMode), combDrawMode.SelectedValue.ToString());
        }
        private void cbAutoTrack_Click(object sender, RoutedEventArgs e)
        {
            if (cbAutoTrack.IsChecked == true)
            {
                DrawParam.IsAutoTrack = true;
            }
            else
            {
                DrawParam.IsAutoTrack = false;
            }
        }
        private void cbAutoStep_Click(object sender, RoutedEventArgs e)
        {
            if (cbAutoStep.IsChecked == true)
            {
                DrawParam.IsAutoStep = true;
            }
            else
            {
                DrawParam.IsAutoStep = false;
            }
        }
        private void cmbDL_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem item = (ComboBoxItem)cmbDL.Items[cmbDL.SelectedIndex];
            if (item.Content != null)
            {
                DrawParam.DLBM = item.Content.ToString();
            }
            else
            {
                DrawParam.DLBM = string.Empty;
            }
        }
        private void paraWidth_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {
            decimal widthTxt = 10;
            if (decimal.TryParse(paraWidth.Text, out widthTxt))
            {
                this.paraWidth.Value = Math.Round(widthTxt, 2, MidpointRounding.AwayFromZero);
            }
        }
        //更换判断事件,原事件在修改过程中就会判断,容易导致判断错误
        private void txtSetWidth_MouseLeave(object sender, MouseEventArgs e)
        {
            double setWidth;
            if (!double.TryParse(txtSetWidth.Text.Trim(), out setWidth))
            {
                MessageHelper.ShowError("数值不合法!");
                return;
            }
            double RoundSetWidth = Math.Round(setWidth, 1);
            //增加微调值范围控制
            if ((RoundSetWidth < 0.1 || RoundSetWidth > 10))
            {
                MessageHelper.ShowTips("微调值须在0.1-10之间!");
                //微调值输入不合法时,恢复上一次设置的微调值
                txtSetWidth.Text = DrawParam.SetWidth.ToString();
                return;
            }
            txtSetWidth.Text = RoundSetWidth.ToString();
            DrawParam.SetWidth = RoundSetWidth;
        }
        private void DXWindow_Closed(object sender, EventArgs e)
        {
            BindingOperations.ClearAllBindings(this);
        }
    }
    /// 
    /// 绘制方式
    /// 
    public enum DrawMode
    {
        /// 
        /// 左边绘制
        /// 
        Left = 1,
        /// 
        /// 右边绘制
        /// 
        Right = 2,
        /// 
        /// 两边绘制
        /// 
        LeftAndRight = 3
    }
    public class DrawParam : INotifyPropertyChanged
    {
        /// 
        /// 绘制方式
        /// 
        public DrawMode DrawMode { get; set; }
        private double _Width;
        /// 
        /// 宽度
        /// 
        public double Width
        {
            get
            {
                return _Width;
            }
            set
            {
                _Width = Math.Round(value, 2, MidpointRounding.AwayFromZero);
                //if (_Width > 15)
                //{
                //    this.ToloranceScale = 0.3;
                //}
                //else if (_Width <= 15 && _Width > 0)
                //{
                //    this.ToloranceScale = 0.1;
                //}
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Width"));
                }
            }
        }
        private double _setWdth;
        /// 
        /// 微调值
        /// 
        public double SetWidth
        {
            get
            {
                return _setWdth;
            }
            set
            {
                _setWdth = value;
            }
        }
        /// 
        /// 是否自动封闭
        /// 
        public bool IsAutoClose { get; set; }
        /// 
        /// 输出类型
        /// 
        public string OutType { get; set; }
        private double _ToloranceScale;
        /// 
        /// 容差比例(自动识别度)
        /// 
        public double ToloranceScale
        {
            get
            {
                return _ToloranceScale;
            }
            set
            {
                _ToloranceScale = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ToloranceScale"));
                }
            }
        }
        private bool _IsAutoTrack { get; set; }
        public bool IsAutoTrack
        {
            get
            {
                return _IsAutoTrack;
            }
            set
            {
                _IsAutoTrack = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsAutoTrack"));
                }
            }
        }
        private bool _IsAutoStep;
        public bool IsAutoStep
        {
            get { return _IsAutoStep; }
            set
            {
                _IsAutoStep = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsAutoStep"));
                }
            }
        }
        private string _DLBM;
        public string DLBM
        {
            get { return _DLBM; }
            set
            {
                _DLBM = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("DLBM"));
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}