using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;
//work cvs
namespace edu.jhu.Cas.Web {
    public class InviteUser : System.Web.UI.Page {
        protected string lmessage;
        protected ListBox memberBox;
        protected long gid, wsid;
        protected string groupName,inviteMe;
        protected System.Web.UI.WebControls.Button InviteButton, RemoveButton;
        protected User[] EligableUsers;
        private void Page_Load(object sender, System.EventArgs e) {
        }
        protected void LoadBoxes() {
        }
        override protected void OnInit(EventArgs e) {
            base.OnInit(e);
            WebUtils.CheckLoggedIn(Session, Response, Request);
            groupName = Request.Params["name"];
            inviteMe = Request.Params["inviteMe"];
            try {
                wsid = (long)Session["USER"];
                gid = (new CasGroups()).FindGroups(wsid, " Name = '" + groupName + "'",false)[0].GID;
                if (inviteMe != null) {
                    User u = (new CasUsers()).FindUser(edu.jhu.Cas.Util.ID.SERVICE_ID, " userid = '" + inviteMe+"'")[0];
                    (new CasGroups()).InviteUser(wsid, u.WebServicesID, gid);
                    lmessage = "Invited " + inviteMe;
                }
                DataSet ExistingUsers = (
                    SqlFactory.select_param(ExistingMembersQry, ExistingMembersQryParams, ExistingMembersQryTypes, ExistingMembersQrySizes,
                    new object[1] { gid }));
                EligableUsers = DataConverter.dataSet2User(
                    SqlFactory.select_param(EligableMembersQry, ExistingMembersQryParams, ExistingMembersQryTypes, ExistingMembersQrySizes,
                    new object[1] { gid }));
                string prefix = "";
                for (int x = 0; x < ExistingUsers.Tables[0].Rows.Count; x++) {
                    prefix = "";
                    switch (ExistingUsers.Tables[0].Rows[x]["Status"].ToString()) {
                        case "1":
                            prefix = "* ";
                            break;
                        default:
                            break;
                    }
                    memberBox.Items.Add(new ListItem(prefix + ExistingUsers.Tables[0].Rows[x]["Name"] +
                        ", [" + ExistingUsers.Tables[0].Rows[x]["UserID"].ToString().Trim() + "]",
                        ExistingUsers.Tables[0].Rows[x]["WebServicesID"].ToString()));
                }
            }
            catch (Exception ex) {
                lmessage = ex.Message;
                return;
            }
            lmessage = Request.Params["lmessage"];
        }
        

        protected void RemoveUsers(Object asdf, EventArgs args) {
            CasGroups cGroups = new CasGroups();
            lmessage += "<p><b>The following users have been removed:</b><br> ";
            foreach (ListItem li in memberBox.Items) {
                if (li.Selected) {
                    cGroups.RemoveUser(wsid, long.Parse(li.Value), gid);
                    lmessage += li.Text + "<br>";
                }
            }
            lmessage += "</p>";
            LoadBoxes();
        }
        private static string[] ExistingMembersQryParams = new string[1] { "@GID" };
        private static SqlDbType[] ExistingMembersQryTypes = new SqlDbType[1] { SqlDbType.BigInt };
        private static int[] ExistingMembersQrySizes = new int[1] { 64 };
        private const string ExistingMembersQry = @"
--existing members
select u.userid, u.webservicesid, u.name ,gm.status
from users u, groupmembers gm
where u.webservicesid = gm.webservicesid
and gm.gid = @GID
order by u.name asc
";
        private const string EligableMembersQry = @"
--eligable members
select * 
from users u 
where u.webservicesid not in (
	select webservicesid from 
	groupmembers gm 
	where gm.gid = @GID
)
and u.gvisible = 1
and (u.privileges not like '%admin%' or u.privileges is null)

";
    }
}

