using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using ProjectBase.Data.BaseDAL;
using SIASUN.Autopilot.BLL;
using SIASUN.Autopilot.Model;
using ProjectBase.Data.Logs;

namespace SIASUN.Autopilot.View
{
    public partial class FormInitMap : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// 图片宽度
        /// </summary>
        private int ImageWidth;
        /// <summary>
        /// 图片高度
        /// </summary>
        private int ImageHeight;
        /// <summary>
        /// 鼠标位置
        /// </summary>
        private Point mousePoint = new Point();   //鼠标位置

        public FormInitMap()
        {
            InitializeComponent();
        }

        private void FormInitMap_Load(object sender, EventArgs e)
        {
            this.picExControl1.SetImage(global::SIASUN.Autopilot.View.Properties.Resources.地图1, true);
            this.picExControl1.MouseMoveTooltipEvent += new PicExControl.MouseMoveTooltip(picExControl1_MouseMove);
            ImageWidth = global::SIASUN.Autopilot.View.Properties.Resources.地图1.Width;
            ImageHeight = global::SIASUN.Autopilot.View.Properties.Resources.地图1.Height;
            this.txt_X1.Properties.ReadOnly = true;
            this.txt_X2.Properties.ReadOnly = true;
            this.txt_Y1.Properties.ReadOnly = true;
            this.txt_Y2.Properties.ReadOnly = true;
            string mapId = System.Configuration.ConfigurationManager.AppSettings["MapId"];
            this.txt_MapId.Text = mapId;
        }

        /// <summary>
        /// 鼠标移动显示经纬度
        /// </summary>
        /// <param name="e"></param>
        private void picExControl1_MouseMove(MouseEventArgs e)
        {
            if (mousePoint.X != e.X || mousePoint.Y != e.Y)
            {
                ToolTipControllerShowEventArgs args = this.toolTip_Message.CreateShowArgs();
                float x = (e.X - this.picExControl1.StartPoint.X) / this.picExControl1.Wrate;
                float y = (e.Y - this.picExControl1.StartPoint.Y) / this.picExControl1.Hrate;
                args.ToolTip = string.Format("x:{0}\ny:{1}", x.ToString(), y.ToString());
                args.SelectedControl = this.picExControl1;
                //args.Title = "提示";
                this.toolTip_Message.ShowHint(args, System.Windows.Forms.Control.MousePosition);
                mousePoint.X = e.X;
                mousePoint.Y = e.Y;
            }
            else
            {
                mousePoint.X = e.X;
                mousePoint.Y = e.Y;
            }
            //pointStandard.Add(new PointF((e.X - this.picExControl1.StartPoint.X) / this.picExControl1.Wrate,
            //              (e.Y - this.picExControl1.StartPoint.Y) / this.picExControl1.Hrate));
        }

        private void picExControl1_MouseLeave(object sender, EventArgs e)
        {
            this.toolTip_Message.HideHint();
        }

        /// <summary>
        /// 地图控件鼠标单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picExControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                float x = (e.X - this.picExControl1.StartPoint.X) / this.picExControl1.Wrate;
                float y = (e.Y - this.picExControl1.StartPoint.Y) / this.picExControl1.Hrate;
                if (string.IsNullOrEmpty(this.txt_X1.Text) && string.IsNullOrEmpty(this.txt_Y1.Text) )
                {
                    this.txt_X1.Text = x.ToString();
                    this.txt_Y1.Text = y.ToString();
                }
                else if (string.IsNullOrEmpty(this.txt_X2.Text) && string.IsNullOrEmpty(this.txt_Y2.Text))
                {
                    this.txt_X2.Text = x.ToString();
                    this.txt_Y2.Text = y.ToString();
                }
            }
        }

       /// <summary>
       /// 清零按钮事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void btn_Clear_Click(object sender, EventArgs e)
        {
            this.txt_X1.Text = null;
            this.txt_X2.Text = null;
            this.txt_Y1.Text = null;
            this.txt_Y2.Text = null;
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (!Vaildate())
            {
                return;
            }
            try
            {
                MoLocationMap mo = new MoLocationMap();
                mo.MapId = this.txt_MapId.Text.Trim();
                mo.LocationLon1 = Convert.ToSingle(this.txt_Lon1.Text.Trim());
                mo.LocationLat1 = Convert.ToSingle(this.txt_Lat1.Text.Trim());
                mo.LocationLon2 = Convert.ToSingle(this.txt_Lon2.Text.Trim());
                mo.LocationLat2 = Convert.ToSingle(this.txt_Lat2.Text.Trim());
                mo.LocationX1 = Convert.ToSingle(this.txt_X1.Text.Trim());
                mo.LocationY1 = Convert.ToSingle(this.txt_Y1.Text.Trim());
                mo.LocationX2 = Convert.ToSingle(this.txt_X2.Text.Trim());
                mo.LocationY2 = Convert.ToSingle(this.txt_Y2.Text.Trim());

                bool flag = BLLFactory<BlLocationMap>.Instance.InsertUpdate(mo, new string[] { mo.MapId });
                if (flag)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("保存成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LogHelper.log.Error(string.Format("插入或更新数据库location_map出现错误：{0}", ex));
            }
        }

        /// <summary>
        /// 验证输入信息是否完整
        /// </summary>
        /// <returns></returns>
        private bool Vaildate()
        {
            if (string.IsNullOrEmpty(this.txt_MapId.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入地图名称。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_X1.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择点1的X坐标。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_Y1.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择点1的Y坐标。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_Lon1.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入点1的经度。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_Lat1.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入点1的纬度。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_X2.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择点2的X坐标。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_Y2.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择点2的Y坐标。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_Lon2.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入点2的经度。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(this.txt_Lat2.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入点2的纬度。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }


            return true;
        }
    }
}