using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

using BeWo.ServiceProxy;
using BeWo.ViewModel;

using BS.Shared.Extensions;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Controls;

namespace BeWo.View
{
    public partial class MiniChatView
    {
        public UserVM ViewModel { get; set; }
        public int abr = 0;

        //protected ArrayList m_workerSocketList;
        protected ChatView viewChat;

        String incommingString;

        String incommingName;

        long incommingEmpfangOid;


        String namenInfo;
        Boolean AppendTrue = false;
        List<string> myListDS = new List<string>();
        //################## Variablen zum Zählen aktueller nachricht ###########
        protected int anzahlDS = 0;
        //#######################################################################

        public MiniChatView(String CLientName, String chat/*, ArrayList m_workerSocketList*/, ChatView view , long oid)
        {
            InitializeComponent();


            namenInfo = CLientName;

            viewChat = view;

            rootGroupBox.Header = CLientName;
            Minichat.AppendText(chat);

            incommingString = chat;
            incommingName = CLientName;
            incommingEmpfangOid = oid;
           // this.m_workerSocketList = m_workerSocketList;

            closeWindow();

            setWindowlocationLeftBottom();


            Minichat.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
            Minichat.ScrollToEnd();
            Minichat.IsReadOnly = true;
            Minichat.IsEnabled = true;


        }


        public void setWindowlocationLeftBottom() {
            this.WindowStartupLocation = WindowStartupLocation.Manual;
            this.Left = System.Windows.SystemParameters.WorkArea.Width - this.Width;
            this.Top = System.Windows.SystemParameters.WorkArea.Height - this.Height;
        }

        private void RootGroupBox_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                // DragMove();
                var myArray = myListDS.ToArray();
                anzahlDS = 0;
                //abfrage ob viewchat gesetzt ist 
                viewChat.SetTextFromMiniV(myArray, incommingName,incommingEmpfangOid);
                viewChat.Visibility = Visibility.Visible;
                myListDS.Clear();
                Visibility = Visibility.Collapsed;

            }
            catch (Exception) { }
        }

        public void closeWindow() {
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) =>
            {
                abr = 1;

                //Close();
                if (Visibility != Visibility.Collapsed)
                {
                    var myArray = myListDS.ToArray();

                    anzahlDS = 0;
                    //abfrage ob viewchat gesetzt ist 
                    viewChat.SetTextFromMiniV(myArray, incommingName,incommingEmpfangOid);
                    viewChat.Visibility = Visibility.Visible;
                    myListDS.Clear();
                    Visibility = Visibility.Collapsed;
                }
                    
            }));
        }


        public void SetText(String text, int nac)
        {
            //Hier das zählen aktueller nachrichten anzahl und dessen ausgabe
            anzahlDS = anzahlDS + nac;

            //Setzte die anzahl der aktuellen nachrichten
            Title = anzahlDS + " " + namenInfo;

            incommingString = text;
            myListDS.Add(incommingString);
            Minichat.Text = namenInfo+text+"\n";    // may the fehler hier sein 
            Minichat.ScrollToEnd();
        }

        //Wenn noch offen append Text zu kur text
        public void AppendTextwhileViewOpen(String text, int nac)
        {
            AppendTrue = true;
            //Hier das zählen aktueller nachrichten anzahl und dessen ausgabe
            anzahlDS = anzahlDS + nac;

            //Setzte die anzahl der aktuellen nachrichten
            Title = anzahlDS + " " + namenInfo;


            incommingString = text;

            myListDS.Add(incommingString);

            Minichat.AppendText(namenInfo+": "+text+"\n");    // may the fehler hier sein 
            Minichat.ScrollToEnd();

        }


        private void Minichat_OnGotFocus(object sender, RoutedEventArgs e)
        {

            var myArray = myListDS.ToArray();

            anzahlDS = 0;
            //abfrage ob viewchat gesetzt ist 
            viewChat.SetTextFromMiniV(myArray, incommingName,incommingEmpfangOid);
            viewChat.Visibility = Visibility.Visible;
            myListDS.Clear();
            Visibility = Visibility.Collapsed;

        }


     /*  
      * Sollte der Wunsch bestehen das man im kleinen fenster antworten möchte
      * private void SendButton_Click(object sender, RoutedEventArgs e)
        { 
            //Minichat.AppendText("Server: "+MiniChatbox.Text+"\n");
            SendMessage(); 
           
            Minichat.ScrollToEnd();

            Visibility = Visibility.Collapsed;
        }




        //############################## Serveranwendungsgedöns ########################

        //Sendet eine Nachricht zum Clienten
        public void SendMessage()
        {
            try
            {
                string msg = MiniChatbox.Text;
                Minichat.AppendText("Server: " + msg + "\n");
                msg = "Server: " + msg + "\n";
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
                Socket workerSocket = null;
                for (int i = 0; i < m_workerSocketList.Count; i++)
                {
                    workerSocket = (Socket)m_workerSocketList[i];
                    if (workerSocket != null)
                    {
                        if (workerSocket.Connected)
                        {
                            workerSocket.Send(byData);
                        }
                    }
                }

                MiniChatbox.Text = "";
            }
            catch (Exception xe)
            {
                Console.WriteLine(xe.Message);
          
                /*Dispatcher.Invoke(
                DispatcherPriority.Normal,
                (ThreadStart)delegate { ServerStats.Text = "Ein Fehler im Bereich Message"; });
                
            }

        }*/
        //###############################################################################


        
    }

}
