using CollaborativePlatformMain.DFEntity.MessageSubUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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.Shapes;

namespace CollaborativePlatformMain.Form.MessageSubPage.CreatLevel
{
    /// <summary>
    /// SetLevelTablecreationForm.xaml 的交互逻辑
    /// </summary>
    public partial class SetLevelTablecreationForm : Window
    {

        public CreatLevelForm LevelTableForm { get; }


        public SetLevelTablecreationForm(CreatLevelForm levelTablecreationForm)
        {
            InitializeComponent();

            LevelTableForm = levelTablecreationForm;
            tit.Content = LevelTableForm.Operation;
            if (LevelTableForm.Operation.Equals("修改层号"))
            {
                this.Company.Content = "";
            }
            else if (LevelTableForm.Operation.Equals("修改结构标高"))
            {
                this.Company.Content = "M";
            }
            else
            {
                this.Company.Content = "MM";
            }
        }

        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //聚焦
            StoryHeightText.Focus();
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void StoryHeightText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter) return;

            TextBox textBox = sender as TextBox;

            if (LevelTableForm.Operation.Equals("修改层号"))
            {
                //data选择的行
                List<string> dataSelect = LevelTableForm.dataGrid1.SelectedItems.Cast<LevelTableEntity>().Select(x => x.JuID).ToList();
                foreach (var item in LevelTableForm.LevelTableEntities)
                {
                    if (dataSelect.Contains(item.JuID) && !item.LevelNmae.Equals("F1"))
                    {
                        item.LevelNmae = textBox.Text;
                    }

                }

            }

            if (LevelTableForm.Operation.Equals("修改层高"))
            {
                Regex rgx = new Regex(@"^\+?[1-9][0-9]*$");
                if (!rgx.IsMatch(textBox.Text))
                {
                    MessageBox.Show("输入格式不正确");
                    return;
                }
                //data选择的行
                List<LevelTableEntity> dataSelect = LevelTableForm.dataGrid1.SelectedItems.Cast<LevelTableEntity>().ToList();
                if (dataSelect.Count == 0)
                {
                    MessageBox.Show("至少选择一行");
                    return;
                }
                //修改的数据//地上
                List<LevelTableEntity> topLevelTableCreations = new List<LevelTableEntity>();
                //修改的数据//地下
                List<LevelTableEntity> downLevelTableCreations = new List<LevelTableEntity>();
                foreach (var data in dataSelect)
                {
                    if (data.StructuralElevation >= 0)
                    {
                        topLevelTableCreations.Add(data);
                    }
                    else
                    {

                        downLevelTableCreations.Add(data);
                    }

                }

                foreach (var item in LevelTableForm.LevelTableEntities)
                {
                    if (dataSelect.Select(x => x.JuID).Contains(item.JuID))
                    {

                        item.FloorHeight = Convert.ToDouble(textBox.Text);
                    }

                }
                //清空数据
                LevelTableForm.dataGrid1.ItemsSource = null;
                LevelTableEntity toplast = topLevelTableCreations.OrderBy(x => x.StructuralElevation).FirstOrDefault();
                LevelTableEntity dowlast = downLevelTableCreations.OrderByDescending(x => x.StructuralElevation).FirstOrDefault();

                if (dowlast == null)
                {
                    dowlast = new LevelTableEntity();
                }
                if (toplast == null)
                {
                    toplast = new LevelTableEntity();
                }
                LevelTableForm.LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableForm.LevelTableEntities.Reverse().ToList());
                List<LevelTableEntity> toplasts = new List<LevelTableEntity>();
                for (int i = 0; i < LevelTableForm.LevelTableEntities.Count; i++)
                {
                    //正数往上
                    if (LevelTableForm.LevelTableEntities[i].StructuralElevation > toplast.StructuralElevation)
                    {
                        LevelTableEntity top = LevelTableForm.LevelTableEntities[i - 1];
                        double structuralElevation = top.FloorHeight / 1000 + top.StructuralElevation;
                        LevelTableForm.LevelTableEntities[i].StructuralElevation = structuralElevation;

                    }


                }
                LevelTableForm.LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableForm.LevelTableEntities.Reverse().ToList());
                for (int i = 0; i < LevelTableForm.LevelTableEntities.Count; i++)
                {
                    //负数往下
                    if (LevelTableForm.LevelTableEntities[i].StructuralElevation < dowlast.StructuralElevation)
                    {
                        LevelTableEntity top = LevelTableForm.LevelTableEntities[i - 1];
                        double structuralElevation = top.FloorHeight / 1000 - top.StructuralElevation;
                        LevelTableForm.LevelTableEntities[i].StructuralElevation = structuralElevation;
                    }

                }

                //清空数据
                LevelTableForm.dataGrid1.ItemsSource = LevelTableForm.LevelTableEntities;

            }

            if (LevelTableForm.Operation.Equals("修改结构标高"))
            {
                Regex rgx = new Regex(@"^(\-|\+)?\d+(\.\d+)?$");
                if (!rgx.IsMatch(textBox.Text))
                {
                    MessageBox.Show("输入格式不正确");
                    return;
                }
                //data选择的行
                List<LevelTableEntity> dataSelect = LevelTableForm.dataGrid1.SelectedItems.Cast<LevelTableEntity>().ToList();
                if (dataSelect.Count == 0)
                {
                    MessageBox.Show("至少选择一行");
                    return;
                }
                if (dataSelect.Count > 1)
                {
                    MessageBox.Show("修改结构板标高不允许选择多条");
                    return;
                }

                foreach (var item in LevelTableForm.LevelTableEntities)
                {
                    if (dataSelect.Select(x => x.JuID).Contains(item.JuID))
                    {

                        item.StructuralElevation = Convert.ToDouble(textBox.Text);
                    }

                }
                //修改的数据//地上
                List<LevelTableEntity> topLevelTableCreations = new List<LevelTableEntity>();
                //修改的数据//地下
                List<LevelTableEntity> downLevelTableCreations = new List<LevelTableEntity>();
                foreach (var data in dataSelect)
                {
                    if (data.StructuralElevation >= 0)
                    {
                        topLevelTableCreations.Add(data);
                    }
                    else
                    {

                        downLevelTableCreations.Add(data);
                    }

                }

                //清空数据
                LevelTableForm.dataGrid1.ItemsSource = null;
                LevelTableEntity toplast = topLevelTableCreations.OrderBy(x => x.StructuralElevation).FirstOrDefault();
                LevelTableEntity dowlast = downLevelTableCreations.OrderByDescending(x => x.StructuralElevation).FirstOrDefault();

                if (dowlast == null)
                {
                    dowlast = new LevelTableEntity();
                }
                if (toplast == null)
                {
                    toplast = new LevelTableEntity();
                }
                LevelTableForm.LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableForm.LevelTableEntities.Reverse().ToList());
                List<LevelTableEntity> toplasts = new List<LevelTableEntity>();
                for (int i = 0; i < LevelTableForm.LevelTableEntities.Count; i++)
                {
                    //正数往上
                    if (LevelTableForm.LevelTableEntities[i].StructuralElevation > toplast.StructuralElevation)
                    {
                        LevelTableEntity top = LevelTableForm.LevelTableEntities[i - 1];
                        double structuralElevation = top.FloorHeight / 1000 + top.StructuralElevation;
                        LevelTableForm.LevelTableEntities[i].StructuralElevation = structuralElevation;

                    }


                }
                LevelTableForm.LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableForm.LevelTableEntities.Reverse().ToList());
                for (int i = 0; i < LevelTableForm.LevelTableEntities.Count; i++)
                {
                    //负数往下
                    if (LevelTableForm.LevelTableEntities[i].StructuralElevation < dowlast.StructuralElevation)
                    {
                        LevelTableEntity top = LevelTableForm.LevelTableEntities[i - 1];
                        double structuralElevation = top.FloorHeight / 1000 - top.StructuralElevation;
                        LevelTableForm.LevelTableEntities[i].StructuralElevation = structuralElevation;
                    }

                }

                //清空数据
                LevelTableForm.dataGrid1.ItemsSource = LevelTableForm.LevelTableEntities;
            }
            this.Close();

        }

        private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
        {

        }
    }
}
