using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;

using System.Web;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using ChatController;
using ChatController.ChatKlassen;
using ChatController.HauptKlassen;
using ChatController.Klassen;

using Brush = System.Windows.Media.Brush;
using Button = System.Windows.Forms.Button;
using ContextMenu = System.Windows.Controls.ContextMenu;
using Image = System.Windows.Controls.Image;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MenuItem = System.Windows.Controls.MenuItem;
using MessageBox = System.Windows.MessageBox;
using Path = System.IO.Path;
using Point = System.Windows.Point;


namespace ownChat
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly Chat _chat;
        private AktuellerKontakt _aktuellerKontakt;

        private bool isChatBoxOnFocus = false;

        private ChatEmojisView _emojiView;

        private IOrderedEnumerable<ChatMessage> _Orderedmessagelist;
        private IOrderedEnumerable<KontaktlistBuilder> _kontaktlist;

        private readonly List<ChatMessage> _messagelist = new List<ChatMessage>();
        private Thread _sendt;
        private Thread _erhaltent;

        public MainWindow(ChatDatenUebergabe x)
        {
            InitializeComponent();

            
            ChatMainControl.InitMitChatdaten(x);
            ChatMainControl.SetzteStyleAufNull();
            //ChatMainControl.AddContextMenu("In Dokumentation übernehmen", ChatMainControl_OnClickContextMenuItem);


        }

        private void ChatMainControl_OnOnEmojii(System.Windows.Controls.Button emojiButton)
        {
            if (_emojiView == null) { 
                _emojiView = new ChatEmojisView(ChatMainControl);

                var x = emojiButton.PointToScreen(new Point(0, 0));
                PresentationSource source = PresentationSource.FromVisual(emojiButton);

                _emojiView.Top = (x.Y / source.CompositionTarget.TransformToDevice.M22) - _emojiView.Height - 5; //- 5
                _emojiView.Left = (x.X / source.CompositionTarget.TransformToDevice.M11) - 270; //x.X / source.CompositionTarget.TransformToDevice.M11


                _emojiView.Show();
            }
            else
            {
                if (_emojiView.Visibility == Visibility.Visible)
                {
                    _emojiView.Visibility = Visibility.Collapsed;
                }
                else
                {
                    _emojiView.Visibility = Visibility.Visible;

                    var x = emojiButton.PointToScreen(new Point(0, 0));
                    PresentationSource source = PresentationSource.FromVisual(emojiButton);

                    _emojiView.Top = (x.Y / source.CompositionTarget.TransformToDevice.M22) - _emojiView.Height - 5;  //- 5
                    _emojiView.Left = (x.X / source.CompositionTarget.TransformToDevice.M11) - 270; //x.X / source.CompositionTarget.TransformToDevice.M11
                }
            }
        }

        private void MainWindow_OnClosed(object sender, EventArgs e)
        {
            //_emojiView = null;
            //ChatMainControl.SpeichereMessageInfoInDatei();
            //// Environment.Exit(0);
            //LoginMaske newLogin = new LoginMaske();
            //newLogin.Show();

            ChatMainControl.SpeichereMessageInfoInDatei();
            Environment.Exit(0);
        }

        private void ChatMainControl_OnOnClose()
        {
            //this.Close();
            ChatMainControl.SpeichereMessageInfoInDatei();
            Environment.Exit(0);
        }
    }
    
}
