﻿// Copyright (c) 2016 BlueConcord. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;

namespace BlueConcord.IdentityServer.EntityFrameworkCore.Entities
{
    public class ScopeSecret<TKey> where TKey : IEquatable<TKey>
    {
        public virtual TKey Id { get; set; }

        public string Description { get; set; }

        public string Value { get; set; }

        public DateTime? Expiration { get; set; }

        public string Type { get; set; }
        
        public virtual Scope<TKey> Scope { get; set; }

        protected bool Equals(ScopeSecret<TKey> other)
        {
            return EqualityComparer<TKey>.Default.Equals(Id, other.Id) && 
                   string.Equals(Description, other.Description, EfConstants.DefaultStringComparison) && 
                   Expiration.Equals(other.Expiration) && 
                   string.Equals(Type, other.Type, EfConstants.DefaultStringComparison) && 
                   string.Equals(Value, other.Value, EfConstants.DefaultStringComparison);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((ScopeSecret<TKey>)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int hashCode = EqualityComparer<TKey>.Default.GetHashCode(Id);
                hashCode = (hashCode * 397) ^ (Description != null ? Description.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ Expiration.GetHashCode();
                hashCode = (hashCode * 397) ^ (Type != null ? Type.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ (Value != null ? Value.GetHashCode() : 0);
                return hashCode;
            }
        }
    }
}
