using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using edu.jhu.Cas.Util;
using edu.jhu.Cas.Users;

namespace edu.jhu.Cas.Services {
    [WebService(Namespace = "http://Services.Cas.jhu.edu", Name = "Groups", Description = "Provides group management functionality.")]
    public class Groups: System.Web.Services.WebService {
        CasGroups cg = new CasGroups();

        [WebMethod(Description = @"
Creates a new group and returns the groupID of said group.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : Creator (and future owner) of a new group<br/>
string pw : Password of said user<br/>
string groupName : Unique name of group.  A-Z0-9 only please.<br/>
string description : Optional description of the new group.<br/>
<br/>
<b>Output:</b><br/>
long : A unique identifier for the newly created group, also known as GroupID or just gid.
")]
        public long CreateGroup(long wsid, string pw, string groupName, string description) {
            Users.Authenticate(wsid, pw);
            return cg.CreateGroup(wsid, groupName, description);
        }
        [WebMethod(Description = @"
Invites a target user to join the given group.<br/>
Target user is not an official member of the group until the invitation is accepted.<br/>
Only a groups owner (creator) may invite other users to said group.<br/>
This method has no output.<br/>
<br/>
<b>Parameters:</b><br/>
long group_owner : wsid of the owner of the group to which a user is being invited<br/>
string pw : Password of said user<br/>
long user_wsid : wsid of user who is to be invited to group_id<br/>
long group_id : Group identifier (GID) of group to which user_wsid is being invited<br/>
<br/>
<b>Output:</b><br/>
Void
    ")]
        public void InviteToGroup(long group_owner, string group_owner_pw, long user_wsid, long group_id) {
            Users.Authenticate(group_owner, group_owner_pw);
            cg.InviteUser(group_owner, user_wsid, group_id);
        }
        [WebMethod(Description = @"
Removes a user from a group.<br/>
The calling user must either be the owner of the group or the same user who is to be removed.<br/>
This method has no output.<br/>
<br/>
<b>Parameters:</b><br/>
long caller_wsid : The identifier of the user calling RemoveUser()<br/>
string call_pw : Password of said user.<br/>
long user2remove : The identifier of the user to be removed.<br/>
long gid : The identifier of the group from which the user is to be removed.<br/>
<br/>
<b>Output:</b><br/>
Void
")]
        public void RemoveUser(long caller_wsid, string caller_pw, long user2remove, long gid) {
            Users.Authenticate(caller_wsid, caller_pw);
            cg.RemoveUser(caller_wsid, user2remove, gid);
        }
        [WebMethod(Description = @"
Accepts a prior invitation (from InviteUser()) to a group.<br/>
Upon acceptance, users become full members of said group.<br/>
This method has no output.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : User who is accepting the invitation.<br/>
string pw : Password of said user.<br/>
long gid : Identifier of group described by the invitation.<br/>
<br/>
<b>Output:</b><br/>
Void
")]
        public void AcceptInvite(long wsid, string pw, long gid) {
            Users.Authenticate(wsid, pw);
            cg.AcceptInvite(wsid, gid);
        }
        [WebMethod(Description = @"
Declines a prior invitation (from InviteUser()) to a group.<br/>
User will not become a member of the group. <br/>
In order to become a member of this group in the future, this user must be reinvited.<br/>
This method has no output.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : User who is declining the invitation.<br/>
string pw : Password of said user.<br/>
long gid : Identifier of group described by the invitation.<br/>
<br/>
<b>Output:</b><br/>
Void
")]     
        public void DeclineInvite(long wsid, string pw, long gid) {
            Users.Authenticate(wsid, pw);
            cg.DeclineInvite(wsid, gid);
        }
        [WebMethod(Description = @"
Publishes a table from a user's mydb to a group of which he is a member.<br/>
This table will then become available to all members of the group.<br/>
This method has no output.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : User who is publishing a table.<br/>
string pw : Password of said user.<br/>
long gid : Identifier of group to which the table will be published.<br/>
string tableName : Name of the table that will be published.<br/>
<br/>
<b>Output:</b><br/>
Void
")]
        public void PublishTable(long wsid, string pw, long gid, string tableName) {
            Users.Authenticate(wsid, pw);
            cg.PublishTable(wsid, gid, tableName);
        }
        [WebMethod(Description = @"
Target table is no longer available to members of said group.<br/>
This method has no output.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : User who is depublishing a table.<br/>
string pw : Password of said user.<br/>
long gid : Identifier of group from which the table will be depublished.<br/>
string tableName : Name of the table that will be depublished.<br/>
<br/>
<b>Output:</b><br/>
Void
")]
        public void DePublishTable(long wsid, string pw, long gid, string tableName) {
            Users.Authenticate(wsid, pw);
            cg.DePublishTable(wsid, gid, tableName);
        }
        [WebMethod(Description = @"
Disbands a group.  All tables previously published to this group will no longer be visible to ex-members of this group.<br/>
This method has no output.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : User who is disbanding a group.  Must be either the groups owner or an admin user.<br/>
string pw : Password of said user.<br/>
long gid : Identifier of group that will be disbanded.<br/>
<br/>
<b>Output:</b><br/>
Void
")]
        public void DestroyGroup(long wsid, string pw, long gid) {
            Users.Authenticate(wsid, pw);
            cg.DestroyGroup(wsid, gid);
        }
        [WebMethod(Description = @"
Returns an array of strings that contains all group tables available for a user.<br/>
String format is 'groupName.ownerid.tableName', which is the same format used in general CasJobs queries.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : User who is calling this method.<br/>
string pw : Password of said user.<br/>
<br/>
<b>Output:</b><br/>
string[] : An array of all available group tables for a given user.<br/>
")]
        public string[] GetGroupTables(long wsid, string pw) {
            Users.Authenticate(wsid, pw);
            return (new edu.jhu.Cas.Jobs.TableJobs()).GetGroupTables(wsid);
        }
        [WebMethod(Description = @"
Returns a list of all groups 'wsid' is a member of.<br/>
If 'wsid' has the admin priv, returns a list of all groups.<br/>
<br/>
<b>Parameters:</b><br/>
long wsid : User who is calling this method.<br/>
string pw : Password of said user.<br/>
<br/>
<b>Output:</b><br/>
Group[] : An array of relevant groups.<br/>
<br/>
A Group object contains:<br/>
string Name : The name of the group.<br/>
string Description : A description of the group.<br/>
long Owner : The owner of this group.<br/>
long GID : A unique identifier for this group.<br/>
")]
        public Group[] GetGroups(long wsid, string pw) {
            Users.Authenticate(wsid, pw);
            return cg.FindGroups(wsid, "");
        }
    }
}