using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace ChatController.ChatKlassen
{
    public class KontaktMessageBuilder
    {

        public string OriginalImage { get; private set; }

        //Chat Ansicht 
        public KontaktMessageBuilder(string username, string message, string sendtime, bool isme, DateTime convertedSendTimeString)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            ConvertetSendtimeString = convertedSendTimeString;

            if (isme)
            {
                ChatColor = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
                Posi = "Right";
            }
            else
            {
                ChatColor = new BrushConverter().ConvertFromString("#cccccc") as SolidColorBrush;
                Posi = "Left";
            }

            if (IsHyperlink(message))
            {
                HyperlinkVisibility = Visibility.Visible;
                ChatVisibility = Visibility.Collapsed;
            }
            else { 
                ChatVisibility = Visibility.Visible;
                HyperlinkVisibility = Visibility.Collapsed;
            }


            SoundVisibility = Visibility.Collapsed;
            _pictureVisibility = Visibility.Collapsed;
            DokumentVisibility = Visibility.Collapsed;
            _herunterladen = Visibility.Collapsed;
            _gifPictureVisibility = Visibility.Collapsed;
        }
        
        // IMage Ansicht 
        public KontaktMessageBuilder(string username, string message, string sendtime, bool isme, ImageSource image, DateTime convertedSendTimeString,string originalImage)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            ImageSources = image;
            ConvertetSendtimeString = convertedSendTimeString;
            OriginalImage = originalImage;
          //  Dokpfad = 

            if (isme)
            {
                ChatColor = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
                Posi = "Right";
            }
            else
            {
                ChatColor = new BrushConverter().ConvertFromString("#cccccc") as SolidColorBrush;
                Posi = "Left";
            }

            //if (!isGif)
            //{
            _pictureVisibility = Visibility.Visible;
            //_gifPictureVisibility = Visibility.Collapsed;
            //}
            //else
            //{
            //    _pictureVisibility = Visibility.Collapsed;
            //    _gifPictureVisibility = Visibility.Visible;
            //}


            ChatVisibility = Visibility.Collapsed;
            SoundVisibility = Visibility.Collapsed;
            HyperlinkVisibility = Visibility.Collapsed;
            DokumentVisibility = Visibility.Collapsed;
            _herunterladen = Visibility.Collapsed;
        }

        //Dokument Ansicht
        public KontaktMessageBuilder(string username, string message, string sendtime, bool isme, string url, DateTime convertedSendTimeString)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            ConvertetSendtimeString = convertedSendTimeString;

            Dokpfad = (object) url; 

            if (isme)
            {
                ChatColor = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
                Posi = "Right";
            }
            else
            {
                ChatColor = new BrushConverter().ConvertFromString("#cccccc") as SolidColorBrush;
                Posi = "Left";
            }

            
            DokumentVisibility = Visibility.Visible;

            _pictureVisibility = Visibility.Collapsed;           
            ChatVisibility = Visibility.Collapsed;
            SoundVisibility = Visibility.Collapsed;
            _herunterladen = Visibility.Collapsed;
            HyperlinkVisibility = Visibility.Collapsed;
        }

        #region Hyperlink Detection

        private static readonly Regex UrlRegex = new Regex(@"(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&amp;(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?");

        public static bool IsHyperlink(string word)
        {
            // First check to make sure the word has at least one of the characters we need to make a hyperlink
            try
            {
                if (word.IndexOfAny(@":.\/".ToCharArray()) != -1)
                {
                    if (Uri.IsWellFormedUriString(word, UriKind.Absolute))
                    {
                        // The string is an Absolute URI
                        if (word.StartsWith("http://")) { 
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else if (UrlRegex.IsMatch(word))
                    {
                        //hier der Bereich hin der die uri splittet wenn neben dem link auch text gesendet wird  


                        Uri uri = new Uri(word, UriKind.RelativeOrAbsolute);

                        if (!uri.IsAbsoluteUri)
                        {
                            // rebuild it it with http to turn it into an Absolute URI
                            uri = new Uri(@"http://" + word, UriKind.Absolute);
                        }

                        if (uri.IsAbsoluteUri)
                        {
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                return false;
            }

            return false;
        }

        #endregion


        #region kram der sortiert werden muss
        //Alter Kram 
        public KontaktMessageBuilder(string username, string message, string sendtime, long messagePersonOid, bool isme, bool loadnext, object loadObject)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            MessagePersonOid = messagePersonOid;

            if (messagePersonOid == 0 && !loadnext)
            {
                ChatColor = new SolidColorBrush(Colors.AliceBlue);  //CornflowerBlue
                Posi = "Center";
            }
            else
                if (isme)
            {
                ChatColor = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
                Posi = "Right";
            }
            else
            {
                ChatColor = new BrushConverter().ConvertFromString("#cccccc") as SolidColorBrush;
                Posi = "Left";
            }


            if (loadnext)
            {
                Posi = "Right";
                LoadNextVisibility = Visibility.Visible;
                ChatVisibility = Visibility.Collapsed;
                LoadObject = loadObject;
                MainVisibility = Visibility.Collapsed;
            }
            else
            {
                LoadNextVisibility = Visibility.Collapsed;
                ChatVisibility = Visibility.Visible;
                LoadObject = null;
                MainVisibility = Visibility.Visible;
            }


            SoundVisibility = Visibility.Collapsed;
            _pictureVisibility = Visibility.Collapsed;
            DokumentVisibility = Visibility.Collapsed;
            _herunterladen = Visibility.Collapsed;

        }

        public KontaktMessageBuilder(string username, string message, string sendtime, long messagePersonOid, bool isme,
            bool isSoundDatei, bool isPicDatei, bool isDokument, ImageSource source, string fulldokpfad, byte[] dokarray, bool isHerunterladen, object[] herunterladbares, long? chatMessageOid)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            MessagePersonOid = messagePersonOid;
            ChatMessageOid = chatMessageOid;


            if (isme && messagePersonOid == 0)
            {
                ChatColor = new SolidColorBrush(Colors.CornflowerBlue);
                Posi = "Center";
            }
            else if (isme)
            {

                ChatColor = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
                Posi = "Right";
            }
            else
            {

                ChatColor = new BrushConverter().ConvertFromString("#cccccc") as SolidColorBrush;
                Posi = "Left";
            }

            if (isSoundDatei)
            {
                ChatVisibility = Visibility.Collapsed;
                SoundVisibility = Visibility.Visible;
                _pictureVisibility = Visibility.Collapsed;
                DokumentVisibility = Visibility.Collapsed;
                _herunterladen = Visibility.Collapsed;

            }
            else if (isDokument)
            {
                ChatVisibility = Visibility.Collapsed;
                SoundVisibility = Visibility.Collapsed;
                _pictureVisibility = Visibility.Collapsed;
                DokumentVisibility = Visibility.Visible;
                _herunterladen = Visibility.Collapsed;

            }
            else if (isPicDatei && source != null)
            {
                ChatVisibility = Visibility.Collapsed;
                SoundVisibility = Visibility.Collapsed;
                _pictureVisibility = Visibility.Visible;
                DokumentVisibility = Visibility.Collapsed;
                _herunterladen = Visibility.Collapsed;
            }
            else if (isHerunterladen)
            {
                _herunterladen = Visibility.Visible;
                ChatVisibility = Visibility.Collapsed;
                SoundVisibility = Visibility.Collapsed;
                _pictureVisibility = Visibility.Collapsed;
                DokumentVisibility = Visibility.Collapsed;

            }
            else
            {
                ChatVisibility = Visibility.Visible;
                SoundVisibility = Visibility.Collapsed;
                _pictureVisibility = Visibility.Collapsed;
                DokumentVisibility = Visibility.Collapsed;
                _herunterladen = Visibility.Collapsed;

            }


            if (source != null)
            {
                ImageSources = source;
            }


            //Dokument 
            object[] array1 = new object[3];

            if (fulldokpfad != null && chatMessageOid == 0)
            {
                Dokpfad = fulldokpfad;
            }
            else if (chatMessageOid > 0 && fulldokpfad != null)
            {
                array1[0] = fulldokpfad;
                array1[1] = dokarray;
                array1[2] = chatMessageOid;

                Dokpfad = array1;
            }


            if (herunterladbares != null)
            {
                Herunterladbares = herunterladbares;
            }

            //array übergabe für Sound ermöglichen
            SoundArray = null;
            LoadNextVisibility = Visibility.Collapsed;
            MainVisibility = Visibility.Visible;
        }

        public string Username { get; private set; }
        public string UserMessage { get; private set; }
        public string SendTime { get; private set; }
        public long MessagePersonOid { get; private set; }
        public Brush ChatColor { get; private set; }
        public int ChatPosition { get; private set; }
        public string Posi { get; private set; }

        public DateTime ConvertetSendtimeString { get; set; }

        public Visibility MainVisibility { get; set; }
        public Visibility ChatVisibility { get; set; }
        public Visibility HyperlinkVisibility { get; set; }

        public Visibility LoadNextVisibility { get; set; }
        public object LoadObject { get; set; }

        public Visibility SoundVisibility { get; set; }

        private Visibility _pictureVisibility;
        public Visibility PictureVisibility { get { return _pictureVisibility; } set { _pictureVisibility = value; OnPropertyChanged("PictureVisibility"); } }

        private Visibility _gifPictureVisibility;
        public Visibility GifPictureVisibility { get { return _gifPictureVisibility; } set { _gifPictureVisibility = value; OnPropertyChanged("GifPictureVisibility"); } }


        public Visibility DokumentVisibility { get; set; }
        public ImageSource ImageSources { get; private set; }
        public object Dokpfad { get; private set; }
        public byte[] SoundArray { get; private set; }

        public long? ChatMessageOid { get; private set; }

        private Visibility _herunterladen;
        public Visibility Herunterladen
        {
            get { return _herunterladen; }
            set { _herunterladen = value; OnPropertyChanged("Herunterladen"); }
        }

        public object[] Herunterladbares { get; private set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion


    }
}
