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

namespace IronIntel.Contractor.Site
{
    public class ChangePasswordBasePage : ContractorBasePage
    {
        protected string UserID;

        protected void ProcessRequest()
        {
            string methodName = Request.Params["MethodName"];
            if (methodName != null)
            {
                switch (methodName.ToUpper())
                {
                    case "CHANGEPASSWORD":
                        ChangePassword();
                        break;
                }
            }
        }

        private void ChangePassword()
        {
            var session = GetCurrentLoginSession();
            if (session == null)
            {
                Response.Write("\"Please login.\"");
                Response.End();
                return;
            }
            var clientdata = Request.Form["ClientData"].Split((char)170);
            var oldpass = HttpUtility.HtmlDecode(clientdata[0]);
            var newpass = HttpUtility.HtmlDecode(clientdata[1]);

            try
            {
                var client = CreateClient<Foresight.Fleet.Services.User.UserQueryClient>();
                client.SessionID = session.SessionID;
                client.ClientHost = Request.UserHostName;
                client.ChangePassword(session.User.UID, oldpass, newpass, session.SessionID);
                Response.Write(string.Empty);
            }
            catch (Exception ex)
            {
                Response.Write(JsonConvert.SerializeObject(ex.Message));
            }
            Response.End();
        }
    }
}