using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using IronIntel.Contractor.Users;
using Newtonsoft.Json;

namespace IronIntel.Contractor.Site.Security
{
    public class UserToContractorPage : ContractorBasePage
    {
        public const char splitChar = (char)170;
        protected void ProcessRequest(string methodName)
        {
            if (methodName != null)
            {
                switch (methodName.ToUpper())
                {
                    case "GETUSERS":
                        GetUsers();
                        break;
                    case "GETGROUPS":
                        GetGroups();
                        break;
                    case "GETCONTRACTORS":
                        GetContractors();
                        break;
                    case "SAVECONTRACTOR":
                        SaveContractor();
                        break;
                }
            }
            Response.End();
        }

        protected override bool AllowCurrentLoginSessionEnter()
        {
            var f = base.AllowCurrentLoginSessionEnter();
            if (!f)
            {
                return false;
            }

            // check whether you are admin.
            var session = GetCurrentLoginSession();
            if (session == null || session.User == null)
            {
                return false;
            }
            var ui = UserManagement.GetUserByIID(session.User.UID);
            return ui != null && ui.UserType >= UserTypes.Admin;
        }

        private void GetUsers()
        {
            string json = "";
            var users = UserManagement.GetUnmanagementUsers().OrderBy(u => u.DisplayName).ToArray();
            json = JsonConvert.SerializeObject(users);
            Response.Write(json);
            Response.End();
        }

        private void GetGroups()
        {
            string json = "";
            var groups = UserManagement.GetGroups().OrderBy(u => u.Name).ToArray();
            json = JsonConvert.SerializeObject(groups);
            Response.Write(json);
            Response.End();
        }
        private void GetContractors()
        {
            string iid = Request.Params["uiid"] == null ? string.Empty : Request.Params["uiid"];
            int flag = Int32.Parse(Request.Params["Flag"]);
            string json = "";
            UserToContractorInfo[] contractors = UserManagement.GetContractorsByIId(iid, flag);
            var cntractrssort = contractors.OrderBy(c => c.Name).ToArray();
            json = JsonConvert.SerializeObject(cntractrssort);
            Response.Write(json);
            Response.End();
        }
        private void SaveContractor()
        {
            string iid = Request.Params["id"];
            string contractorstr = Request.Params["contractors"];
            string[] contractors = JsonConvert.DeserializeObject<string[]>(contractorstr);
            for (int i = 0; i < contractors.Length; i++)
            {
                contractors[i] = HttpUtility.UrlDecode(contractors[i]);
            }
            UserManagement.AddUserToContractor(iid, contractors);
            Response.Write("Save contractors info successfully.");
            Response.End();
        }
    }
}
