using CollaborativePlatformMain.DFEntity.MessageSubUtil;
using CollaborativePlatformMain.Form.MessageSubPage.Project;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Shapes;

namespace CollaborativePlatformMain.Form.MessageSubPage
{
    /// <summary>
    /// SeconMessagePage.xaml 的交互逻辑
    /// </summary>
    public partial class SeconMessagePage : Page
    {
        /// <summary>
        /// 父级窗体
        /// </summary>
        public HomepageForm Homepage;

        /// <summary>
        ///  有参构造
        /// </summary>
        /// <param name="homepageForm">父级窗体</param>
        public SeconMessagePage(HomepageForm homepageForm)
        {
            InitializeComponent();
            Homepage = homepageForm;
        }


        public ObservableCollection<MessageData> tongzhis1 = new ObservableCollection<MessageData>();
        public ObservableCollection<MessageData> tongzhis2 = new ObservableCollection<MessageData>();

        public ObservableCollection<MessageData> xiangmu1 = new ObservableCollection<MessageData>();
        public ObservableCollection<MessageData> xiangmu2 = new ObservableCollection<MessageData>();

        public List<MessageData> xitong1 = new List<MessageData>();
        public List<MessageData> xitong2 = new List<MessageData>();

        /// <summary>
        /// 消息更新
        /// </summary>
        public static List<MessageData> MessageDatas = new List<MessageData>();


        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<string> secondaryNodes = new List<string>() { "通知公告", "项目", "系统" };

            for (int i = 0; i < secondaryNodes.Count; i++)
            {
                if (i == 0)
                {

                    List<string> projects = new List<string>() { "国博城项目总消息", "北京大兴项目总消息", "绿地项目总消息" };

                    MessageData messageData = new MessageData();
                    messageData.SeconNode = secondaryNodes[i];
                    messageData.ThreeNodes = projects;
                    tongzhis1.Add(messageData);

                    for (int j = 0; j < projects.Count; j++)
                    {
                        MessageData threeData = new MessageData();
                        threeData.SeconNode = projects[j];
                        if (j == 0)
                        {
                            List<string> nodes = new List<string>() { "按甲方要求,国博城项目务必在4月15号之前完成报审版。" };
                            threeData.ThreeNodes = nodes;
                        }
                        else if (j == 1)
                        {
                            List<string> nodes = new List<string>() { "按甲方要求,北京大兴项目务必在4月22号之前完成报审版。" };
                            threeData.ThreeNodes = nodes;
                        }
                        else if (j == 2)
                        {
                            List<string> nodes = new List<string>() { "按甲方要求,绿地项目务必在4月30号之前完成报审版。" };
                            threeData.ThreeNodes = nodes;
                        }
                        tongzhis2.Add(threeData);
                    }
                }
                else if (i == 1)
                {

                    List<string> projects = new List<string>() { };
                    //添加新增数据
                    projects.AddRange(MessageDatas.Select(x => x.SeconNode).ToList());

                    MessageData messageData = new MessageData();
                    messageData.SeconNode = secondaryNodes[i];
                    messageData.ThreeNodes = projects;
                    xiangmu1.Add(messageData);

                    for (int j = 0; j < projects.Count; j++)
                    {
                        MessageData threeData = new MessageData();
                        threeData.SeconNode = projects[j];
                        //if (j == 0)
                        //{
                        //    List<string> nodes = new List<string>() { "国博城项目建筑1#楼3层更新。", "国博城项目建筑1#楼5层更新", "国博城项目建筑2#楼3层更新。" };
                        //    threeData.ThreeNodes = nodes;
                        //}
                        //else if (j == 1)
                        //{
                        //    List<string> nodes = new List<string>() { "北京大兴项目结构2#楼1层墙柱平法图更新。", "北京大兴项目结构1#楼3层墙柱平法图更新。" };
                        //    threeData.ThreeNodes = nodes;
                        //}
                        //else if (j == 2)
                        //{
                        //    List<string> nodes = new List<string>() { "绿地项目地下人防图更新。", "绿地项目暖通3#楼3层更新" };
                        //    threeData.ThreeNodes = nodes;
                        //}
                        //else
                        //{
                        foreach (var item in MessageDatas)
                        {
                            if (item.SeconNode.Equals(threeData.SeconNode))
                            {
                                threeData.ThreeNodes = item.ThreeNodes;
                                break;
                            }
                        }
                        //}
                        xiangmu2.Add(threeData);
                    }
                }
                else if (i == 2)
                {
                    List<string> projects = new List<string>() { "系统更新", "停服升级" };

                    MessageData messageData = new MessageData();
                    messageData.SeconNode = secondaryNodes[i];
                    messageData.ThreeNodes = projects;
                    xitong1.Add(messageData);

                    for (int j = 0; j < projects.Count; j++)
                    {
                        MessageData threeData = new MessageData();
                        threeData.SeconNode = projects[j];
                        if (j == 0)
                        {
                            List<string> nodes = new List<string>() { "交互平台版本更新至2.0版本",
                                                                      "  本次更新了:",
                                                                      "    1.主界面消息分组。",
                                                                      "    2.修改了登陆界面。",
                                                                      "   请及时更新！！",};
                            threeData.ThreeNodes = nodes;
                        }
                        else if (j == 1)
                        {
                            List<string> nodes = new List<string>() { "交互平台版本将在4月13日8点20分至4月13日8点50分进行停服更新",
                                                                       "本次停服更新不会导致您的数据丢失。"};
                            threeData.ThreeNodes = nodes;
                        }
                        xitong2.Add(threeData);
                    }
                }
            }

            me_message.Children.Clear();
            foreach (var item in tongzhis2)
            {
                Button button = new Button();
                button.Content = item.SeconNode;
                button.BorderThickness = new Thickness(0);
                button.Width = 180;
                button.Height = 30;
                button.Tag = item;
                button.Click += new RoutedEventHandler(messageBtnClick);
                button.HorizontalContentAlignment = HorizontalAlignment.Left;
                me_message.Children.Add(button);
            }
        }

        /// <summary>
        /// 二级菜单选中的元素
        /// </summary>
        public static string SelectNode = "";

        /// <summary>
        /// 通知公告
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_announcementClick(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            if (menu == null) return;

            me_message.Children.Clear();
            foreach (var item in tongzhis2)
            {
                Button button = new Button();
                button.Content = item.SeconNode;
                button.BorderThickness = new Thickness(0);
                button.Width = 180;
                button.Height = 30;
                button.Background = Brushes.WhiteSmoke;
                button.Visibility = Visibility.Visible;
                button.Click += new RoutedEventHandler(messageBtnClick);
                button.Tag = item;
                button.HorizontalContentAlignment = HorizontalAlignment.Left;
                me_message.Children.Add(button);
            }

            SelectNode = menu.Header.ToString();
        }

        /// <summary>
        /// 项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_projectClick(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            if (menu == null) return;

            me_message.Children.Clear();
            foreach (var item in xiangmu2)
            {
                Button button = new Button();
                button.Content = item.SeconNode;
                button.BorderThickness = new Thickness(0);
                button.Width = 180;
                button.Height = 30;
                button.Tag = item;
                button.Click += new RoutedEventHandler(messageBtnClick);
                button.HorizontalContentAlignment = HorizontalAlignment.Left;
                me_message.Children.Add(button);
            }

            SelectNode = menu.Header.ToString();
        }

        /// <summary>
        /// 系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_systemClick(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            if (menu == null) return;

            me_message.Children.Clear();
            foreach (var item in xitong2)
            {
                Button button = new Button();
                button.Content = item.SeconNode;
                button.BorderThickness = new Thickness(0);
                button.Width = 180;
                button.Height = 30;
                button.Tag = item;
                button.Click += new RoutedEventHandler(messageBtnClick);
                button.HorizontalContentAlignment = HorizontalAlignment.Left;
                me_message.Children.Add(button);
            }

            SelectNode = menu.Header.ToString();
        }


        /// <summary>
        /// 具体消息按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void messageBtnClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;

            MessageData announcement = button.Tag as MessageData;
            if (announcement == null) return;
            List<string> specificContent = announcement.ThreeNodes;

            me_right.Children.Clear();
            foreach (var specificConten in specificContent)
            {
                
               
                //ScrollViewer scrollViewer = new ScrollViewer();
                //scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                //me_right.Children.Add(scrollViewer);
                if (SelectNode.Equals("项目"))
                {
                    DockPanel dockPanel = new DockPanel();
                    dockPanel.Margin = new Thickness(0, 10, 30, 0);
                    dockPanel.HorizontalAlignment = HorizontalAlignment.Right;

                    TextBlock textBlock1 = new TextBlock();
                    textBlock1.Text = "查看";
                    textBlock1.FontSize = 13;
                    textBlock1.Foreground = new SolidColorBrush(Colors.Green);
                    textBlock1.TextDecorations = TextDecorations.Underline;
                    textBlock1.Margin = new Thickness(0, 0, 10, 0);
                    textBlock1.FontFamily = new FontFamily("黑体");
                    textBlock1.DataContext = specificConten;
                    textBlock1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Message_Look);
                    textBlock1.HorizontalAlignment = HorizontalAlignment.Right;
                    dockPanel.Children.Add(textBlock1);

                    TextBlock textBlock2 = new TextBlock();
                    textBlock2.Text = "忽略";
                    textBlock2.FontSize = 13;
                    textBlock2.FontFamily = new FontFamily("黑体");
                    textBlock2.Foreground = new SolidColorBrush(Colors.Green);
                    textBlock2.TextDecorations = TextDecorations.Underline;
                    textBlock2.DataContext = specificConten;
                    textBlock2.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Message_Ignore);

                    dockPanel.Children.Add(textBlock2);
                    dockPanel.HorizontalAlignment = HorizontalAlignment.Right;
                    StackPanel stackPanel1 = new StackPanel();

                    TextBlock textBlock3 = new TextBlock();
                    textBlock3.Text = specificConten;
                    textBlock3.FontSize = 15;
                    textBlock3.FontFamily = new FontFamily("黑体");
                    textBlock3.DataContext = specificConten;
                    textBlock3.HorizontalAlignment = HorizontalAlignment.Left;
                    stackPanel1.DataContext = specificConten;
                    stackPanel1.Children.Add(textBlock3);
                    stackPanel1.Children.Add(dockPanel);
                    stackPanel1.Width = 480;
                    me_right.Children.Add(stackPanel1);
                }
                else
                {
                    TextBlock textBlock = new TextBlock();
                    textBlock.Text = specificConten;
                    textBlock.FontSize = 15;
                    textBlock.FontFamily = new FontFamily("黑体");
                    textBlock.DataContext = announcement;
                    me_right.Children.Add(textBlock);
                }
            }


        }


        /// <summary>
        /// 忽略事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Message_Ignore(object sender, RoutedEventArgs e)
        {
            int count = xiangmu2.Count;
            TextBlock textBlock = sender as TextBlock;
            string messageData = textBlock.DataContext as string;
            bool isEqual = false;

            //删除单独一条消息
            foreach (var item in xiangmu2)
            {
                for (int i = 0; i < item.ThreeNodes.Count; i++)
                {
                    string iThreeNode = item.ThreeNodes[i];

                    if (iThreeNode.Equals(messageData))
                    {
                        item.ThreeNodes.RemoveAt(i);
                        isEqual = true;
                        break;
                    }
                }
                if (isEqual) break;
            }
            for (int i = 0; i < me_right.Children.Count; i++)
            {
                if (me_right.Children[i] is StackPanel stackPanel)
                {
                    for (int j = 0; j < stackPanel.Children.Count; j++)
                    {
                        string stackMessageData = stackPanel.DataContext as string;
                        if (stackMessageData.Equals(messageData))
                        {
                            me_right.Children.RemoveAt(i);
                            i--;
                            break;
                        }
                    }
                }
            }
            xiangmu2 = new ObservableCollection<MessageData>(xiangmu2.Where(x => x.ThreeNodes.Count > 0));

            //一条信息没有父级删除
            if (count != xiangmu2.Count)
            {
                me_message.Children.Clear();
                foreach (var item in xiangmu2)
                {
                    Button button = new Button();
                    button.Content = item.SeconNode;
                    button.BorderThickness = new Thickness(0);
                    button.Width = 180;
                    button.Height = 30;
                    button.Tag = item;
                    button.Click += new RoutedEventHandler(messageBtnClick);
                    button.HorizontalContentAlignment = HorizontalAlignment.Left;
                    me_message.Children.Add(button);
                }
            }

        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Message_Look(object sender, RoutedEventArgs e)
        {
            TextBlock textBlock = sender as TextBlock;
            string messageData = textBlock.DataContext as string;

            Dictionary<string, FullProfessionPage> keyValuePairs = SeconProjectPage.floor_quanPages;

            FullProfessionPage fullProfessionPage = null;
            foreach (var item in keyValuePairs)
            {
                string probBuliName = item.Key.Replace(" ", "");
                messageData = messageData.Replace("项目", "");
                messageData = messageData.Replace(" ", "");
                messageData = messageData.Replace("更新", "");
                messageData = messageData.Replace("建筑", "");
                if (messageData.Contains(probBuliName))
                {
                    fullProfessionPage = item.Value;
                    break;
                }
            }

            if (fullProfessionPage != null)
            {
                BigFullProfessiongForm bigFullProfessiongForm = new BigFullProfessiongForm(Homepage, OperatingFloorPage.FullProfessionPage, messageData);
                bigFullProfessiongForm.ShowDialog();
            }

        }
    }
}
