using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Services;
using System.Web.Mail;
using edu.jhu.Cas.Util;
using edu.jhu.Cas.Servers;
namespace edu.jhu.Cas.Users {
    public class CasNotes {
        private void ConvertMyDBObject(string objname, long wsid, out string context, out string newobjname) {
            Match m = FilterPower.grpRegx.Match(objname);
            if (m.Success) {//then this is a group table
                newobjname = m.Result("$2");
                context = m.Result("$1");
            }
            else {//then its not a group table
                CasUsers cu = new CasUsers();
                User author = cu.FindUser(ID.SERVICE_ID, " webservicesid = " + wsid)[0];
                context = author.UserID;
                newobjname = objname;
            }
        }
        public void PurgeObjNotes(long wsid, string context, string objname, CasDBObjType type) {
            if (context.ToUpper() == "MYDB")
                ConvertMyDBObject(objname, wsid, out context, out objname);
            SqlFactory.nonselect_param(PurgeNotesQry, PurgbeNotesQryParams, PurgbeNotesQryTypes, PurgbeNotesQrySizes,
                         new object[4] { wsid, objname, (short)type, context });
        }
        public DataSet GetObjNotes(long wsid, string context, string objname, CasDBObjType type){
            if (context.ToUpper() == "MYDB")
                ConvertMyDBObject(objname, wsid, out context, out objname);
            DataSet ds = SqlFactory.select_param(GetNotesQry, GetNotesQryParams, GetNotesQryTypes, GetNotesQrySizes,
                           new object[3] { objname, (short)type, context });
            return ds;
        }
        public string GetObjNotesHTML(long wsid, string context, string objname, CasDBObjType type) {
            DataSet ds = GetObjNotes(wsid, context, objname, type);
            if (ds.Tables[0].Rows.Count == 0)
                return "No notes found";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in ds.Tables[0].Rows) {
                sb.AppendLine("<div><table class=\"notesTab\"><tr><td>");
                sb.Append(dr["userid"] as string);
                if ((long)dr["author"] == wsid) {
                    //sb.Append("<input type=\"button\" value=\"Remove Note\"/>");
                }
                sb.AppendLine("</td><td style=\"width:100%;\">&nbsp;</td><td>");
                sb.Append(dr["time"].ToString());
                sb.AppendLine("</td></tr></table></div>");
                //sb.AppendLine("<pre>");
                string note = dr["note"] as string;
                note = Regex.Replace(note,@"\</?script\>","");
                sb.AppendLine(note);
                //sb.AppendLine("</pre>");
            }
            return sb.ToString();
        }
        public void AddNote(long author_wsid, string context,string cjobjname, string note, CasDBObjType cjobjtype) {
            if (note == null) throw new CasJobsException("Note cannot be null");
            note = note.Trim();
            if (note.Length == 0) throw new Exception("Cannot add an empty note");
            if (context.ToUpper() == "MYDB")
                ConvertMyDBObject(cjobjname, author_wsid, out context, out cjobjname);
            SqlFactory.nonselect_param(AddNotesQry, AddNotesQryParams, AddNotesQryTypes, AddNotesQrySizes, 
                new object[5] { note, author_wsid, cjobjname, (short)cjobjtype, context});
        }
        public const string PurgeNotesQry = @"
delete from notes 
where author = @author
and objname = @objname
and objtype = @objtype
and context = @context
";
        private static string[] PurgbeNotesQryParams = new string[4] {"@author", "@objname", "@objtype", "@context" };
        private static SqlDbType[] PurgbeNotesQryTypes = new SqlDbType[4] { 
            SqlDbType.BigInt, SqlDbType.VarChar, SqlDbType.TinyInt, SqlDbType.VarChar };
        private static int[] PurgbeNotesQrySizes = new int[4] { 64, 100, 8, 50 };
        public const string AddNotesQry = @"
insert into notes(note,time,author,objname,objtype,context) values (@note,getdate(),@author,@objname,@objtype,@context)
";
        private static string[] AddNotesQryParams = new string[5] { "@note", "@author", "@objname", "@objtype", "@context" };
        private static SqlDbType[] AddNotesQryTypes = new SqlDbType[5] { 
            SqlDbType.VarChar, SqlDbType.BigInt, SqlDbType.VarChar, SqlDbType.TinyInt, SqlDbType.VarChar };
        private static int[] AddNotesQrySizes = new int[5] { 2000, 64, 50, 8, 50 };
        private const string GetNotesQry = @"
select n.note,n.time,n.author,n.objname,n.objtype,n.context,u.userid from notes n, users u
where objname = @objname and objtype = @objtype and context = @context
and n.author = u.webservicesid
order by time asc
";
        private static string[] GetNotesQryParams = new string[3] { "@objname", "@objtype", "@context" };
        private static SqlDbType[] GetNotesQryTypes = new SqlDbType[3] { SqlDbType.VarChar, SqlDbType.TinyInt, SqlDbType.VarChar };
        private static int[] GetNotesQrySizes = new int[3] { 50, 8, 50 };
    }
}
