using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using ChatController.HauptKlassen;

namespace ChatController
{
    public partial class ChatEmojiiControl
    {
        private ChatMainControl _ChatMainControl;
        private readonly ChatEmoji _ChatEmoji;

        private readonly List<ListBox> _EmojiListBoxes = new List<ListBox>();

        public ChatEmojiiControl()
        {
            InitializeComponent();

            _ChatEmoji = new ChatEmoji();
          
            SetEmojis();
        }

        public void InitChatView(ChatMainControl chatMainControl)
        {
            _ChatMainControl = chatMainControl;
        }

        public void SetEmojis()
        {
            EmojisListBox1.ItemsSource = _ChatEmoji.EmojisListBox1;
            EmojisListBox2.ItemsSource = _ChatEmoji.EmojisListBox2;
            EmojisListBox3.ItemsSource = _ChatEmoji.EmojisListBox3;
            EmojisListBox4.ItemsSource = _ChatEmoji.EmojisListBox4;
            EmojisListBox5.ItemsSource = _ChatEmoji.EmojisListBox5;
            EmojisListBox6.ItemsSource = _ChatEmoji.EmojisListBox6;
            EmojisListBox7.ItemsSource = _ChatEmoji.EmojisListBox7;

            _EmojiListBoxes.AddRange(
                new List<ListBox>
                {
                    EmojisListBox1,
                    EmojisListBox2,
                    EmojisListBox3,
                    EmojisListBox4,
                    EmojisListBox5,
                    EmojisListBox6,
                    EmojisListBox7
                }
            );

            TabItem1.Header = _ChatEmoji.TabItem1;
            TabItem2.Header = _ChatEmoji.TabItem2;
            TabItem3.Header = _ChatEmoji.TabItem3;
            TabItem4.Header = _ChatEmoji.TabItem4;
            TabItem5.Header = _ChatEmoji.TabItem5;
            TabItem6.Header = _ChatEmoji.TabItem6;
            TabItem7.Header = _ChatEmoji.TabItem7;
        }

        private void OnEmojiClick(object sender, RoutedEventArgs e)
        {
            foreach(var emojiListBox in _EmojiListBoxes)
            {
                foreach(var selectedItem in emojiListBox.SelectedItems)
                {
                    var directoryListing = selectedItem as DirectoryListing;

                    if(!string.IsNullOrWhiteSpace(directoryListing?.EmojiCode))
                    {
                        PassEmojiToChatMainControl(directoryListing.EmojiCode);
                    }
                }

                emojiListBox.SelectedIndex = -1;
            }
        }

        public void PassEmojiToChatMainControl(string emojiCode)
        {
            try
            {
                Dispatcher.Invoke(
                    DispatcherPriority.Normal,
                    (ThreadStart) delegate
                    {
                        _ChatMainControl.Chatbox.Focus();
                        _ChatMainControl.Chatbox.Text += emojiCode;
                        _ChatMainControl.Chatbox.SelectionStart = _ChatMainControl.Chatbox.Text.Length;
                    });
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
