using System;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media;

namespace ChatController.ChatKlassen
{
    public class ChatMessage
    {
        //Chat Ansicht 
        public ChatMessage(string username, string message, DateTime sendtime, bool isme)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            IsMyMessage = isme;

            Farbgebung(isme);
            
            if (IsHyperlink(message))
            {
                VisibilityRegelung("HyperlinkMessage");   
            }
            else {
                VisibilityRegelung("Message");
            }        
        }
        
        // Image Ansicht  
        public ChatMessage(string username, string message, DateTime sendtime, bool isme, ImageSource image, string originalImage, string imageName)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            ImageSources = image;
            IsMyMessage = isme;

            OriginalImage = originalImage;
            OriginalImageName = imageName;
         
            Farbgebung(isme);

            VisibilityRegelung("Image");         
        }

        //Image PlaceHolder | wird nicht benutzt und wird nicht gebraucht ?  
        public ChatMessage(string username, string message, DateTime sendtime, bool isme, string originalImage, string imageName)
        {
            Username = username;
            UserMessage = message;
            SendTime = sendtime;
            IsMyMessage = isme;
            OriginalImage = originalImage;
            OriginalImageName = imageName;
            
            Farbgebung(isme);

            VisibilityRegelung("Default_Image");           
        }

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

            Dokpfad = (object) url;

            Farbgebung(isme);

            VisibilityRegelung("Dokumente");           
        }

        #region Farbgebung
        private void Farbgebung(bool isme)
        {
            if (isme)
            {
                ChatColor = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
                Posi = "Right";
                _farbeRechtsColor = new BrushConverter().ConvertFromString("#505050") as SolidColorBrush;
                _farbeLinksColor = new BrushConverter().ConvertFromString("#505050") as SolidColorBrush;
            }
            else
            {
                ChatColor = new BrushConverter().ConvertFromString("#FFFFFF") as SolidColorBrush; //"#cccccc"
                Posi = "Left";
                _farbeRechtsColor = new BrushConverter().ConvertFromString("#505050") as SolidColorBrush;
                _farbeLinksColor = new BrushConverter().ConvertFromString("#505050") as SolidColorBrush;
            }
        }
        #endregion

        #region Visibility Regelung
        private void VisibilityRegelung(string Ansicht)
        {
            switch (Ansicht)
            {
                case "Message":
                        ChatVisibility = Visibility.Visible;

                        HyperlinkVisibility = Visibility.Collapsed;
                        _pictureVisibility = Visibility.Collapsed;
                        DokumentVisibility = Visibility.Collapsed;
                        _gifPictureVisibility = Visibility.Collapsed;
                        _picturePlaceHolderVisibility = Visibility.Collapsed;
                    break;
                case "HyperlinkMessage":
                        HyperlinkVisibility = Visibility.Visible;

                        ChatVisibility = Visibility.Collapsed;
                        _pictureVisibility = Visibility.Collapsed;
                        DokumentVisibility = Visibility.Collapsed;
                        _gifPictureVisibility = Visibility.Collapsed;
                        _picturePlaceHolderVisibility = Visibility.Collapsed;
                    break;
                case "Image":
                        _pictureVisibility = Visibility.Visible;

                        ChatVisibility = Visibility.Collapsed;
                        HyperlinkVisibility = Visibility.Collapsed;
                        DokumentVisibility = Visibility.Collapsed;
                        _picturePlaceHolderVisibility = Visibility.Collapsed;
                    break;
                case "Default_Image":
                        _picturePlaceHolderVisibility = Visibility.Visible;

                        _pictureVisibility = Visibility.Collapsed;
                        ChatVisibility = Visibility.Collapsed;
                        HyperlinkVisibility = Visibility.Collapsed;
                        DokumentVisibility = Visibility.Collapsed;                 
                    break;
                case "Dokumente":
                        DokumentVisibility = Visibility.Visible;

                        _pictureVisibility = Visibility.Collapsed;
                        ChatVisibility = Visibility.Collapsed;
                        HyperlinkVisibility = Visibility.Collapsed;
                        _picturePlaceHolderVisibility = Visibility.Collapsed;
                    break;
            }
        }
        #endregion

        #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 Variablen

        public bool IsMyMessage { get; set; }
        public string Username { get; private set; }
        public string UserMessage { get; private set; }
        public DateTime SendTime { get; private set; }

        public string UserTimeStringLeft
        {
            get
            {
                if (IsMyMessage)
                {
                    return null;
                }
                return Username; //String.Format("{0} {1}", Username, SendTimeString);
            }
        }

        public string UserTimeStringRight
        {
            get
            {
                //if (!IsMyMessage)
                //{
                //    return null;
                //}
                return SendTimeString;
            }
        }

        public string SendTimeString
        {
            get
            {
                if (SendTime.Date == DateTime.Today)
                {
                    return String.Format("Heute {0:HH:mm}", SendTime);
                }
                return SendTime.ToString("dd.MMM HH:mm");
            }
        }
        //public long MessagePersonOid { get; private set; }

        public Brush ChatColor { get; private set; }
        
        public string Posi { get; private set; }

        public string OriginalImage { get; private set; }
        public string OriginalImageName { get; private set; }
        
        public Visibility ChatVisibility { get; set; }
        public Visibility HyperlinkVisibility { get; set; }
        

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

        private Visibility _picturePlaceHolderVisibility;
        public Visibility PicturePlaceHolderVisibility { get { return _picturePlaceHolderVisibility; } set { _picturePlaceHolderVisibility = value; OnPropertyChanged("PicturePlaceHolderVisibility"); } }

        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 long? ChatMessageOid { get; private set; }
        

        //Farbe Links MEssageHeader
        private Brush _farbeLinksColor;
        public Brush FarbeLinksColor
        {
            get
            {
                return _farbeLinksColor;
            }
            set
            {

                _farbeLinksColor = value;

                OnPropertyChanged("FarbeLinksColor");
            }
        }

        //Farbe Rechts MessageHeader
        private Brush _farbeRechtsColor;
        public Brush FarbeRechtsColor
        {
            get
            {
                return _farbeRechtsColor;
            }
            set
            {

                _farbeRechtsColor = value;

                OnPropertyChanged("FarbeRechtsColor");
            }
        }


       

        public event PropertyChangedEventHandler PropertyChanged;

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


    }
}
