using DSInternals.Common.Data;
using DSInternals.DataStore;
using DSInternals.PowerShell.Properties;
using System;
using System.Management.Automation;

namespace DSInternals.PowerShell.Commands
{
    [Cmdlet(VerbsCommon.Set, "ADDBPrimaryGroup")]
    [OutputType("None")]
    public class SetADDBPrimaryGroupCommand : ADDBModifyPrincipalCommandBase
    {
        [Parameter(Mandatory = true, HelpMessage = "TODO", ValueFromPipelineByPropertyName = true)]
        [Alias("gid", "Group", "PrimaryGroup", "GroupId")]
        [ValidateRange(DirectoryAgent.RidMin, DirectoryAgent.RidMax)]
        public int PrimaryGroupId
        {
            get;
            set;
        }

        protected override void ProcessRecord()
        {
            //TODO: Exception handling: Object not found, malformed DN, ...
            // TODO: Extract as Resource
            string verboseMessage = "Setting the primary group of account {0}.";
            bool hasChanged;
            switch (this.ParameterSetName)
            {
                case parameterSetByDN:
                    this.WriteVerbose(String.Format(verboseMessage, this.DistinguishedName));
                    var dn = new DistinguishedName(this.DistinguishedName);
                    hasChanged = this.DirectoryAgent.SetPrimaryGroupId(dn, this.PrimaryGroupId, this.SkipMetaUpdate);
                    break;

                case parameterSetByName:
                    this.WriteVerbose(String.Format(verboseMessage, this.SamAccountName));
                    hasChanged = this.DirectoryAgent.SetPrimaryGroupId(this.SamAccountName, this.PrimaryGroupId, this.SkipMetaUpdate);
                    break;

                case parameterSetByGuid:
                    this.WriteVerbose(String.Format(verboseMessage, this.ObjectGuid));
                    hasChanged = this.DirectoryAgent.SetPrimaryGroupId(this.ObjectGuid, this.PrimaryGroupId, this.SkipMetaUpdate);
                    break;

                case parameterSetBySid:
                    this.WriteVerbose(String.Format(verboseMessage, this.ObjectSid));
                    hasChanged = this.DirectoryAgent.SetPrimaryGroupId(this.ObjectSid, this.PrimaryGroupId, this.SkipMetaUpdate);
                    break;

                default:
                    // This should never happen:
                    throw new PSInvalidOperationException(Resources.InvalidParameterSetMessage);
            }
            this.WriteVerboseResult(hasChanged);
        }
    }
}