using System.Collections.Generic;
using System.Security.Claims;
using Valg.EVA.Skanning.Identity.Area;

namespace Valg.EVA.Skanning.Identity
{
    public class Role
    {
        public Role()
        {
            this.ClaimsInRole = new List<Claim>();
        }

        public string RoleName { get; set; }

        public string ContestName { get; set; }

        public string ContestId { get; set; }

        public string ElectionName { get; set; }

        public string ReportingUnitIdentifier { get; set; }

        public AreaDescription CountyArea { get; set; }

        public AreaDescription Area { get; set; }

        public List<Claim> ClaimsInRole { get; set; }

        public string ElectionGroup { get; set; }

        public string ElectionGroupName { get; set; }

        public string ElectionCategory { get; set; }

        public string ElectionIdentifier { get; set; }

        public bool IsSubArea { get; set; }

        public string CountingEmlAreaContextId
        {
            get
            {
                return IsSubArea
                    ? CountyArea.EmlAreaContextId
                    : Area.EmlAreaContextId;
            }
        }

        public string RoleId { get; set; }

        public string UserId { get; set; }

        public string TransferAreaContextId
        {
            get { return TransferAreaContextIdParser.Parse(this); }
        }

        public bool IsEqual(Role role2)
        {
            var roleValuesIsEqual = this.ContestId == role2.ContestId
                   && this.ReportingUnitIdentifier == role2.ReportingUnitIdentifier
                   && this.ContestName == role2.ContestName                   
                   && this.ElectionCategory == role2.ElectionCategory
                   && this.ElectionGroup == role2.ElectionGroup
                   && this.ElectionGroupName == role2.ElectionGroupName
                   && this.ElectionIdentifier == role2.ElectionIdentifier
                   && this.IsSubArea == role2.IsSubArea
                   && this.RoleName == role2.RoleName
                   && this.ElectionName == role2.ElectionName;

            bool areaValuesIsEqual;
            if (this.Area == null && role2.Area == null)
            {
                areaValuesIsEqual = true;
            }
            else if (this.Area != null && role2.Area != null)
            {
                areaValuesIsEqual = this.Area.EmlAreaContextId == role2.Area.EmlAreaContextId;  
            }
            else
            {
                areaValuesIsEqual = false;
            }

            return roleValuesIsEqual && areaValuesIsEqual;
        }
    }
}
