using System.Collections.Generic;
using System.Linq;

namespace BS.Shared.DataContracts
{
    public partial class UserDC
    {
        public override bool Equals(object obj)
        {
            if (obj is UserDC)
            {
                var y = (UserDC)obj;
                if (this.UserOid == null && y.UserOid == null)
                {
                    return this.GetHashCode() == y.GetHashCode();
                }

                if (this.UserOid != null && y.UserOid != null)
                {
                    return this.UserOid == y.UserOid;
                }
            }

            return false;
        }

        public List<UserRightType> GetGrantedRights()
        {
            return (this.UserGroups != null) ? this.UserGroups.Where(g => g.Rights != null).SelectMany(g => g.Rights).Distinct().ToList() : new List<UserRightType>();
        }

        public override int GetHashCode()
        {
            return this.UserOid.HasValue ? this.GetType().Name.GetHashCode() ^ this.UserOid.GetHashCode() : base.GetHashCode();
        }

        public SettingsDC GetSettingValue(SettingsType pType)
        {
            if (this.Settings != null)
            {
                return this.Settings.SingleOrDefault(s => s.Type == pType);
            }

            return null;
        }

        public string GetSettingValue(SettingsType pType, string key)
        {
            SettingsDC dc = this.GetSettingValue(pType);
            if (dc != null && dc.Value.IndexOf(";") == -1 && dc.Value.IndexOf("=") == -1)
            {
                return dc.Value;
            }
            else
            {
                string[] keyValuePairs = dc.Value.Split(';');

                foreach (string pair in keyValuePairs)
                {
                    string[] keyValuePair = pair.Split('=');
                    if (keyValuePair.Length == 2)
                    {
                        if (keyValuePair[0] == key)
                        {
                            return keyValuePair[1];
                        }
                    }
                }
            }

            return null;
        }

        public bool HasRight(UserRightType right)
        {
            return this.GetGrantedRights().Contains(right);
        }

        public void SetSettingValue(SettingsType pType, string key, string val)
        {
            SettingsDC dc = this.GetSettingValue(pType);

            if (dc == null)
            {
                SettingsDC newDc = new SettingsDC();
                newDc.Type = pType;
                newDc.Value = key + "=" + val;
                this.Settings.Add(newDc);
            }
            else
            {
                string[] keyValuePairs = dc.Value.Split(';');
                string newValue = string.Empty;
                foreach (string pair in keyValuePairs)
                {
                    if (pair.IndexOf(key + "=") != 0)
                    {
                        if (newValue.Length > 0)
                        {
                            newValue += ";";
                        }

                        newValue += pair;
                    }
                }

                if (newValue.Length > 0)
                {
                    newValue += ";";
                }

                newValue += key + "=" + val;
                dc.Value = newValue;
            }
        }

        public override string ToString()
        {
            return this.Employee.ToString();
        }
    }
}