using CollaborativePlatformMain.DFEntity.MessageSubUtil;
using CollaborativePlatformMain.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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>
    /// CreatLevelForm.xaml 的交互逻辑
    /// </summary>
    public partial class CreatLevelForm : Window
    {

        /// <summary>
        /// 父级窗体
        /// </summary>
        public SeconThreeDimensionalPage SeconThreeDimensionalPage;

        /// <summary>
        /// 数据信息
        /// </summary>
        public ObservableCollection<LevelTableEntity> LevelTableEntities = new ObservableCollection<LevelTableEntity>();


        public CreatLevelForm(SeconThreeDimensionalPage seconThreeDimensionalPage)
        {
            InitializeComponent();
            SeconThreeDimensionalPage = seconThreeDimensionalPage;
        }
        /// <summary>
        /// 操作类型
        /// </summary>
        public string Operation = "";

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LevelTableEntities = new ObservableCollection<LevelTableEntity>();
            LevelTableEntity levelTableCreationEntity = new LevelTableEntity(Guid.NewGuid().ToString(), "1",0.00, 3000);
            LevelTableEntities.Add(levelTableCreationEntity);
            dataGrid1.ItemsSource = LevelTableEntities;
            ComboBox_levelAdd.ItemsSource = new List<string>() { "地上", "地下" };
        }

        /// <summary>
        /// 添加标高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_add_Click(object sender, RoutedEventArgs e)
        {
            CreatLevelUtil.AddLevelFool(ref LevelTableEntities, LevelHight.Text, num.Text, ComboBox_levelAdd.Text);
            dataGrid1.ItemsSource = null;
            dataGrid1.ItemsSource = LevelTableEntities;
        }

        /// <summary>
        /// 层高输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LevelHight_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            string PlatformText = textBox.Text;
            Regex rgx = new Regex(@"^\+?[1-9][0-9]*$");
            if (!rgx.IsMatch(PlatformText))
            {
                MessageBox.Show("输入格式不正确");
                textBox.Text = "3000";
                return;
            }
        }

        /// <summary>
        /// 层数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void num_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            string PlatformText = textBox.Text;
            Regex rgx = new Regex(@"^[0-9]*[1-9][0-9]*$");
            if (!rgx.IsMatch(PlatformText))
            {
                MessageBox.Show("输入格式不正确");
                textBox.Text = "3000";
                return;
            }

        }

        /// <summary>
        /// 修改层号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Operation = "修改层号";
            SetLevelTablecreationForm setLevelTablecreationForm = new SetLevelTablecreationForm(this);
            setLevelTablecreationForm.Show();
        }

        /// <summary>
        /// 修改结构标高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            Operation = "修改结构标高";
            SetLevelTablecreationForm setLevelTablecreationForm = new SetLevelTablecreationForm(this);
            setLevelTablecreationForm.Show();
        }

        /// <summary>
        /// 修改层高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            Operation = "修改层高";
            SetLevelTablecreationForm setLevelTablecreationForm = new SetLevelTablecreationForm(this);
            setLevelTablecreationForm.Show();
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {

            //data选择的行
            List<LevelTableEntity> dataSelect = dataGrid1.SelectedItems.Cast<LevelTableEntity>().ToList();
            for (int i = 0; i < LevelTableEntities.Count; i++)
            {
                if (dataSelect.Select(x => x.JuID).Contains(LevelTableEntities[i].JuID))
                {
                    LevelTableEntities[i].LevelNmae = "删除";
                }
            }
            LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableEntities.Where(x => !x.LevelNmae.Equals("删除")).ToList());
            LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableEntities.Reverse().ToList());


            //结构标高
            double levelHeigth = 0;
            //层高
            double floorHeight = 0;
            for (int i = 0; i < LevelTableEntities.Count; i++)
            {
                LevelTableEntity levelTableCreationEntity = LevelTableEntities[i];
                if (i == 0)
                {
                    levelHeigth = levelTableCreationEntity.StructuralElevation;
                    floorHeight = levelTableCreationEntity.FloorHeight;
                    continue;
                }

                LevelTableEntities[i].StructuralElevation = levelHeigth + (floorHeight / 1000);

                levelHeigth = levelTableCreationEntity.StructuralElevation;
                floorHeight = levelTableCreationEntity.FloorHeight;
            }
            LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableEntities.OrderByDescending(x => x.StructuralElevation).ToList());
            dataGrid1.ItemsSource = null;
            dataGrid1.ItemsSource = LevelTableEntities;
        }

        /// <summary>
        /// 全部清除
        /// </summary>
        /// <param name="sender"></param>
        private void Button_clean_Click_1(object sender, RoutedEventArgs e)
        {
            dataGrid1.ItemsSource = null;
            LevelTableEntities = new System.Collections.ObjectModel.ObservableCollection<LevelTableEntity>(LevelTableEntities.Where(x => x.LevelNmae.Equals("1")));
            dataGrid1.ItemsSource = LevelTableEntities;

        }
        /// <summary>
        /// 创建标高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_CreateLevel_Click(object sender, RoutedEventArgs e)
        {
            SeconThreeDimensionalPage.LevelTableEntities = LevelTableEntities;
            SeconThreeDimensionalPage.SetLevelData();
            this.Close();        
        }

        /// <summary>
        /// 顶部替换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            //CheckBox checkBox = sender as CheckBox;
            //if (checkBox.IsChecked.Value)
            //{
            //    TopConversion.IsEnabled = true;
            //}
            //else
            //{

            //    TopConversion.IsEnabled = false;
            //}

        }

        /// <summary>
        /// 底部替换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            //CheckBox checkBox = sender as CheckBox;
            //if (checkBox.IsChecked.Value)
            //{
            //    dowConversion.IsEnabled = true;
            //}
            //else
            //{

            //    dowConversion.IsEnabled = false;
            //}
        }
    }
}
