using System;
using System.Threading;
using System.Windows;

using ChatController.LoginKlassen;

using Point = System.Windows.Point;


namespace ownChat
{
    public partial class MainWindow
    {
        private readonly ChatEmojisView _EmojiView;

        public MainWindow(ChatDatenUebergabe x)
        {
            InitializeComponent();

            ChatMainControl.ContainingWindow = this;

            ChatMainControl.InitMitChatdaten(x);
            ChatMainControl.ResetStyle();

            _EmojiView = new ChatEmojisView(ChatMainControl);
        }

        private void ChatMainControl_OnOnEmojii(System.Windows.Controls.Button emojiButton)
        {
            _EmojiView.Visibility = Visibility.Visible;

            var pointToScreen = emojiButton.PointToScreen(new Point(0, 0));
            var presentationSource = PresentationSource.FromVisual(emojiButton);

            if(presentationSource?.CompositionTarget != null)
            {
                _EmojiView.Top = pointToScreen.Y / presentationSource.CompositionTarget.TransformToDevice.M22 - _EmojiView.Height - 5;
                _EmojiView.Left = pointToScreen.X / presentationSource.CompositionTarget.TransformToDevice.M11 - 270;
            }
        }

        private void MainWindow_OnClosed(object sender, EventArgs e)
        {
            ChatMainControl.DeleteTempFiles();
            ChatMainControl.WriteToNewSyncFile();
            ChatMainControl.ClearNotifications();

            Thread.Sleep(500);

            Environment.Exit(0);
        }
    }
}
