using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Media;
using ChatController.LoginKlassen;
using ChatController.Utilities;
using Brush = System.Windows.Media.Brush;

namespace ChatController.ChatKlassen
{
    public class Contact : INotifyPropertyChanged
    {
        public string Name { get; }

        private GroupLatestMessage _ReceivedMessage;
        public GroupLatestMessage ReceivedMessage
        {
            get => _ReceivedMessage;

            set
            {
                _ReceivedMessage = value;
                OnPropertyChanged(nameof(ReceivedMessage));
            }
        }

        public bool IsNewMessage { get; set; }

        private Brush _Color;
        public Brush Color
        {
            get => _Color;
            
            set
            {
                _Color = value;
                OnPropertyChanged(nameof(Color));
            }
        }

        private bool _HasUnreadMessages;
        public bool HasUnreadMessages
        {
            get => _HasUnreadMessages;

            set
            {
                _HasUnreadMessages = value;
                    
                if (_HasUnreadMessages)
                {
                    _Color = new BrushConverter().ConvertFromString("#ff5e00") as SolidColorBrush;
                }
                else
                {
                    _Color = new SolidColorBrush(Colors.Gray);
                }

                OnPropertyChanged(nameof(HasUnreadMessages));
                OnPropertyChanged(nameof(Color));
            }
        }

        private ImageSource _Image;
        public ImageSource Image
        {
            get => _Image;
            set
            {
                if(!Equals(_Image, value))
                {
                    _Image = value;
                    OnPropertyChanged(nameof(Image));
                }
            }
        }

        public DateTime TimeStamp { get; set; }

        public int GroupId { get; }

        public string UserIdManage { get; }

        public bool IsChatMessageInputGridVisible { get; }

        public Contact(string pName, GroupLatestMessage pLatestMessage, DateTime pTimeStamp, int pGroupId, string pUserIdManage, bool pOnlyEmployees, int pUserCount, bool isChatMessageInputGridVisible, string accentColorBrushString, string profilePicturePath, string key, Dictionary<long, string> userId2Uri)
        {
            Name              = pName;
            ReceivedMessage   = pLatestMessage;
            TimeStamp         = pTimeStamp;
            GroupId           = pGroupId;
            UserIdManage      = pUserIdManage;
            HasUnreadMessages = false;

            IsChatMessageInputGridVisible = isChatMessageInputGridVisible;

            Utils.DownloadUserProfilePicturesAsync(userId2Uri);

            if (accentColorBrushString is null)
            {
                if(!(pUserIdManage is null))
                {
                    accentColorBrushString = "3B7799";
                }
                else if(pOnlyEmployees && pUserCount > 2)
                {
                    accentColorBrushString = "45993B";
                }
                else
                {
                    accentColorBrushString = "000000";
                }
            }
            
            AccentColorBrush = new BrushConverter().ConvertFromString($"#{accentColorBrushString}") as SolidColorBrush;

            IsGroupChat = pUserCount > 2;

            Utils.DownloadGroupProfilePictureAsync(profilePicturePath, key, delegate(ImageSource imageSource)
            {
                Image = imageSource;
            });
        }

        public bool IsGroupChat { get; }

        public override bool Equals(object obj)
        {
            if(obj is Contact y)
            {
                if(ReferenceEquals(this, y))
                {
                    return true;
                }

                if(GetType() != y.GetType())
                {
                    return false;
                }

                return Name == y.Name &&
                       IsNewMessage == y.IsNewMessage &&
                       TimeStamp.Equals(y.TimeStamp) &&
                       GroupId == y.GroupId &&
                       UserIdManage == y.UserIdManage &&
                       IsChatMessageInputGridVisible == y.IsChatMessageInputGridVisible;
            }

            return false;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = Name != null ? Name.GetHashCode() : 0;

                hashCode = (hashCode * 397) ^ GroupId;
                hashCode = (hashCode * 397) ^ (UserIdManage != null ? UserIdManage.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ IsChatMessageInputGridVisible.GetHashCode();

                return hashCode;
            }
        }

        public SolidColorBrush AccentColorBrush { get; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }      
    }
}
