using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;
using Microsoft.Extensions.Options;
using Microsoft.Reporting.WebForms;

namespace OccuCare.Report.Pages
{
    public partial class Pages_Common_Report_DefaultReportViewer : System.Web.UI.Page
    {
        public static int LastPageNumber = 0;
        public static string DefaultOrgId;
        private clConn objC
        {
            get
            {
                return new clConn();
            }
            set { }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {

            //if (Request.Browser.Browser == "Chrome")
            //    //Page.MasterPageFile = "~/Pages/Common/MasterPages/ChromeReportMasterPage.master";
            //else
            //    Page.MasterPageFile = "~/Pages/Common/MasterPages/ReportMasterPage.master";
        }
        private const string AntiXsrfTokenKey = "__AntiXsrfToken";
        private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
        private string _antiXsrfTokenValue;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (SecuritySession.OrgId == null)
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Session Timeout');window.close();", true);
                //_MessageBox.Show("No Records found for the selected criteria", MessageType.Error);
                return;
            }
            var requestCookie = Request.Cookies[AntiXsrfTokenKey];
            Guid requestCookieGuidValue;
            if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
            {
                _antiXsrfTokenValue = requestCookie.Value;
                Page.ViewStateUserKey = _antiXsrfTokenValue;
            }
            else
            {
                _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
                Page.ViewStateUserKey = _antiXsrfTokenValue;

                var responseCookie = new HttpCookie(AntiXsrfTokenKey)
                {
                    HttpOnly = true,
                    Value = _antiXsrfTokenValue
                };
                if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
                {
                    responseCookie.Secure = true;
                }
                Response.Cookies.Set(responseCookie);
            }
            Page.PreLoad += master_Page_PreLoad;
            
            if (!IsPostBack)
            {
                //GetReportDataStatic();
                if (ReportSession.ReportDetail != null)
                {
                    BindReport();
                }
            }
        }

        public void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            string ExaminationIdText;
            string PolicyId;
            string PermitNumber;
            switch (e.ReportPath)
            {
                case "EmploymentDetail":
                    ExaminationIdText = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("Employment", FetchHealthCardEmployment(ExaminationIdText)));
                    break;
                case "HealthCardFullGeneralSub":
                    ExaminationIdText = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("HealthCardFull_General", FetchHealthCardGeneral(ExaminationIdText)));
                    break;
                case "HealthCardFullGeneralSub_New2":
                    ExaminationIdText = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("HealthCardFull_General", FetchHealthCardGeneral(ExaminationIdText)));
                    break;
                case "HealthCardFullTestSub":
                    ExaminationIdText = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("HealthCardFull_Test", FetchHealthCardFullTestSub(ExaminationIdText)));
                    break;
                case "PulmanaryResult":
                    ExaminationIdText = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("PulmonaryResult", FetchPulmonaryResult(ExaminationIdText)));
                    break;
                case "AudiometryTest":
                    ExaminationIdText = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("Audiometry", FetchAudiometry(ExaminationIdText)));
                    break;
                case "HealthCardFullGeneralSub_Filter":
                    ExaminationIdText = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("HealthCardFull_General", FetchHealthCardGeneral(ExaminationIdText)));
                    break;
                case "ClaimStatus":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimStatus", FetchClaimStatus(PolicyId)));
                    break;
                case "ClaimRelationship":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimRelationship", FetchClaimRelationship(PolicyId)));
                    break;
                case "ClaimCare":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimCare", FetchClaimCare(PolicyId)));
                    break;
                case "ClaimTopHospitals":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimTopHospitals", FetchClaimTopHospitals(PolicyId)));
                    break;
                case "ClaimTopAilments":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimTopAilments", FetchClaimTopAilments(PolicyId)));
                    break;
                case "ClaimTreatment":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimTreatment", FetchClaimTreatment(PolicyId)));
                    break;
                case "ClaimTopAilmentCare":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimTopAilmentCare", FetchClaimTopAilmentCare(PolicyId)));
                    break;
                case "ClaimTopAilmentRelationship":
                    PolicyId = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ClaimTopAilmentRelationship", FetchClaimTopAilmentRelationship(PolicyId)));
                    break;
                case "ApproversList":
                    PermitNumber = e.Parameters[0].Values[0].ToString();
                    e.DataSources.Add(new ReportDataSource("ApproversList", FetchPermitNumber(PermitNumber)));
                    break;

            }
        }
        protected void master_Page_PreLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
                ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
            }
            else
            {
                if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
                    || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
                {
                    throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
                }
            }
        }


        protected override void OnPreInit(EventArgs e)
        {
            //Page.Theme = "Default";
            base.OnPreInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (ReportSession.ReportDetail != null)
                //{

                //}

            }

        }

        private void BindReport()
        {
            if (ReportSession.ReportDetail != null)
            {
                LocalReport d = new LocalReport();
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                ReportViewer1.LocalReport.ReportPath = Server.MapPath(ReportSession.ReportDetail.ReportLocationPath.Replace("~", "")); //Server.MapPath(ReportSession.ReportDetail.ReportLocationPath);

                ReportDataSource datasource = new ReportDataSource(ReportSession.ReportDetail.DataSetName, ReportSession.ReportDetail.DataSet.Tables[0]);

                RDLCSubReportProcessing subReport = new RDLCSubReportProcessing(DefaultOrgId);
                subReport.AddSubReportDataSource(datasource, ReportViewer1);

                ReportViewer1.LocalReport.DataSources.Add(datasource);
                ReportViewer1.ZoomMode = Microsoft.Reporting.WebForms.ZoomMode.PageWidth;

                if (ReportSession.ReportDetail.DataSetName2 != null && ReportSession.ReportDetail.DataSet2 != null)
                {
                    ReportDataSource datasource2 = new ReportDataSource(ReportSession.ReportDetail.DataSetName2, ReportSession.ReportDetail.DataSet2.Tables[0]);
                    ReportViewer1.LocalReport.DataSources.Add(datasource2);
                }
                ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

            }
        }
        private DataTable FetchPermitNumber(string PermitNumber)
        {
            DataSet ds = new DataSet();
            string sql = "[sPermit].[pApproverPermitListGet]";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@PermitNumber", SqlDbType.NVarChar, PermitNumber)
                );

            return ds.Tables[0];
        }
        private DataTable FetchClaimStatus(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "[sSupport].[pRptClaimStatus]";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }
        private DataTable FetchClaimRelationship(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "[sSupport].[pRptClaimRelationShip]";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }

        private DataTable FetchClaimCare(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "sSupport.pRptClaimCare";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }
        private DataTable FetchClaimTopHospitals(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "sSupport.pRptClaimTopHospitals";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }
        private DataTable FetchClaimTopAilments(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "sSupport.pRptClaimTopAilments";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }
        private DataTable FetchClaimTreatment(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "sSupport.pRptClaimTreatment";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }
        private DataTable FetchClaimTopAilmentCare(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "sSupport.pRptClaimTopAilmentCareTypeWise";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }
        private DataTable FetchClaimTopAilmentRelationship(string PolicyId)
        {
            DataSet ds = new DataSet();
            string sql = "sSupport.pRptClaimTopAilmentRelationWise";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rPolicyId", SqlDbType.Int, PolicyId)
                );

            return ds.Tables[0];
        }

        private DataTable FetchHealthCardGeneral(string ExaminationIdText)
        {
            DataSet ds = new DataSet();
            string sql = "sExamination.pRptGetHealthCardSubGeneralParameters";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rExaminationId", SqlDbType.BigInt, ExaminationIdText)
                );

            return ds.Tables[0];
        }

        private DataTable FetchHealthCardEmployment(string ExaminationIdText)
        {
            DataSet ds = new DataSet();
            string sql = "[sExamination].[pRptGetHealthCardEmploymentDetail]";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rExaminationId", SqlDbType.BigInt, ExaminationIdText)
                );

            return ds.Tables[0];
        }

        private DataTable FetchHealthCardFullTestSub(string ExaminationIdText)
        {
            DataSet ds = new DataSet();
            string sql = "sExamination.pRptGetHealthCardSubTestsParameters";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rExaminationId", SqlDbType.BigInt, ExaminationIdText)
                );

            return ds.Tables[0];
        }

        private DataTable FetchPulmonaryResult(string ExaminationIdText)
        {
            DataSet ds = new DataSet();
            string sql = "sExamination.pRptPulmonaryResult";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rExaminationId", SqlDbType.BigInt, ExaminationIdText)
                );

            return ds.Tables[0];
        }

        private DataTable FetchAudiometry(string ExaminationIdText)
        {
            DataSet ds = new DataSet();
            string sql = "sExamination.pRptAudiometrytest";

            ds = objC.QuerySQL(sql,
                objC.GetSqlParam("@rExaminationId", SqlDbType.BigInt, ExaminationIdText)
                );

            return ds.Tables[0];
        }

    }
}