using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Resources;
using System.Security.Cryptography.X509Certificates;
using System.Text;

public partial class Masters_TA_UI_Master_Sample : System.Web.UI.Page
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    //BiostarAPI objBiostarAPI = new BiostarAPI();
    MultiLingualEventData objMultiLingualEventData = new MultiLingualEventData();
    DataSet dsEmployeeLingual = new DataSet();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Common objcommon = new cls_Common();
    DataSet ds_Checkuser = new DataSet();
    string strSetAlert;
    int EmpID;
    string source_Language;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_GetSession_Click(object sender, EventArgs e)
    {
        string response = string.Empty;
        string postData = "{ \"User\": { \"login_id\": \"admin\", \"password\": \"Admin@123\" }}";
        response = GetSessionID("", postData, "application/json");
        //In active user
        string INActiveuser = string.Empty;
        string postInActive = "{ \"User\": { \"disabled\": \"true\" }}";
        INActiveuser = ActiveorInActiveUsers(response, postInActive, "application/json", "2,3");
        //active user
        string Activeuser = string.Empty;
        string postActive = "{ \"User\": { \"disabled\": \"false\" }}";
        Activeuser = ActiveorInActiveUsers(response, postActive, "application/json", "2,3");
    }
    public string GetSessionID(string apiKey, string postData, string postDataContentType)
    {

        try
        {
            ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
            var webAddr = "https://192.168.0.96/api/login";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);

            httpWebRequest.ContentType = postDataContentType;
            httpWebRequest.Method = "POST";
            httpWebRequest.KeepAlive = false;

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            Stream dataStream = httpWebRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)httpResponse).StatusCode;
            string SessionID = null;
            SessionID = httpResponse.GetResponseHeader("bs-session-id");
            //if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            //{
            //    var text = "Unauthorized - need new token";

            //}
            //else if (!ResponseCode.Equals(HttpStatusCode.OK))
            //{
            //    var text = "Response from web service isn't OK";
            //}

            //StreamReader Reader = new StreamReader(httpResponse.GetResponseStream());
            //string responseLine = Reader.ReadToEnd();
            //Reader.Close();
            return SessionID;
        }
        catch (Exception ex)
        {

        }
        return "error";
    }

    public static bool ValidateServerCertificate(
                                              object sender,
                                              X509Certificate certificate,
                                              X509Chain chain,
                                              SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }

    public string ActiveorInActiveUsers(string SessionID, string postData, string postDataContentType, string UserIDs)
    {

        try
        {
            ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
            if (UserIDs != "")
            {
                string[] userid = UserIDs.Split(',');
                for (int i = 0; i < userid.Length; i++)
                {
                    var webAddr = "https://192.168.0.96/api/users/" + userid[i].ToString();
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);

                    httpWebRequest.ContentType = postDataContentType;
                    httpWebRequest.Headers.Add("bs-session-id", SessionID);
                    httpWebRequest.Method = "PUT";
                    httpWebRequest.KeepAlive = false;

                    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                    Stream dataStream = httpWebRequest.GetRequestStream();
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Close();

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    HttpStatusCode ResponseCode = ((HttpWebResponse)httpResponse).StatusCode;
                    if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
                    {
                        var text = "Unauthorized - need new token";

                    }
                    else if (!ResponseCode.Equals(HttpStatusCode.OK))
                    {
                        var text = "Response from web service isn't OK";
                    }

                    StreamReader Reader = new StreamReader(httpResponse.GetResponseStream());
                    string responseLine = Reader.ReadToEnd();
                    Reader.Close();
                }

            }
            else
            {
                return "UserID Missing";
            }

        }
        catch (Exception ex)
        {

        }
        return "error";
    }
}