using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensionsDev
{
    public static class BaseEditExtension
    {
        public static void UseDoubleClickToSelectAll(this BaseEdit ctrl)
        {
            ctrl.DoubleClick += Ctrl_DoubleClick;
        }
        private static void Ctrl_DoubleClick(object sender, EventArgs e)
        {
            var ctrl = (BaseEdit)sender;
            ctrl.SelectAll();
        }
        public static bool CheckDouble(this ButtonEdit @this, DXErrorProvider dxErrorProvider, string msg)
        {
            dxErrorProvider.ClearErrors();
            if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
            {
                dxErrorProvider.SetError(@this, $"{msg}值不能为空!");
                return false;
            }
           
            double val;
            bool isDoubleLon = Double.TryParse(@this.Text.Trim(), out val);
            if (!isDoubleLon)
            {
                dxErrorProvider.SetError(@this, $"{msg}值非有效数字!");
                return false;
            }
            dxErrorProvider.SetError(@this, string.Empty);
            return true;

        }
        public static bool CheckLonLat(this TextEdit @this, DXErrorProvider dxErrorProvider, string msg)
        {
            dxErrorProvider.ClearErrors();
            if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
            {
                dxErrorProvider.SetError(@this, $"{msg}经纬度不能为空!");
                return false;
            }
            var context = @this.Text.Split(',');
            if (context.Length != 2)
            {
                dxErrorProvider.SetError(@this, $"{msg}经度纬度之间用英文逗号隔开!");
                return false;
            }
            double lon;
            bool isDoubleLon = Double.TryParse(context[0], out lon);
            if (!isDoubleLon || lon > 180 || lon < -180)
            {
                dxErrorProvider.SetError(@this, $"{msg}经度范围[180,-180]!");
                return false;
            }
            double lat;
            bool isDoubleLat = Double.TryParse(context[1], out lat);
            if (!isDoubleLat || lat > 90 || lat < -90)
            {
                dxErrorProvider.SetError(@this, $"{msg}纬度范围[90,-90]!");
                return false;
            }
            dxErrorProvider.SetError(@this, string.Empty);
            return true;

        }

        public static (bool, string) CheckLonLat(this TextEdit @this, string msg)
        {
            if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
            {

                return (false, $"{msg}经纬度不能为空!");
            }
            var context = @this.Text.Split(',');
            if (context.Length != 2)
            {

                return (false, $"{msg}经度纬度之间用英文逗号隔开!");
            }
            double lon;
            bool isDoubleLon = Double.TryParse(context[0], out lon);
            if (!isDoubleLon || lon > 180 || lon < -180)
            {
                return (false, $"{msg}经度范围[180,-180]!");
            }
            double lat;
            bool isDoubleLat = Double.TryParse(context[1], out lat);
            if (!isDoubleLat || lat > 90 || lat < -90)
            {
                return (false, $"{msg}纬度范围[90,-90]!");
            }
            return (true, "");

        }

        public static double[] GetLonLat(this TextEdit @this)
        {
            var context = @this.Text.Replace("，", ",").Split(',');
            double lon = Convert.ToDouble(context[0]);
            double lat = Convert.ToDouble(context[1]);
            return new double[3] { lon, lat, 0 };

        }

    }
}
