using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Data;
using System.Web;
using ConsulenzaEvoluta;
using PrometeiaMessage.bancafideuram.nac.anagrafica;

public partial class reportMappatura : System.Web.UI.Page
{
    public string codCliente;
    public string tipoRete;
    public string codPB;
    public string codAs;
    public string codiceUtenteQueryString;
    public string token;
    public string userName = string.Empty;
    public string codiceApplicazione = string.Empty;
    public string checkPrivacy;

    private Nucleo _nucleo;
    public string pathNav;
    public int idReport;              //indica il modello (fixed) COMPLETO di QUESTO report da cui costruire la treeview iniziale
    private string sUtente;
    public int chiaveClientePB;
    public static int tpReport = 8;        //valore proprietario (fixed) di QUESTO report //DA VALUTARE!!
    public static int idReportAlbero = 16; //valore proprietario (fixed) di QUESTO report //DA VALUTARE!!
    public static int idReportBase = 16;
    public static int idReportCompleto = 16;
    public string codiceRete;
    public string codicePB;
    public string ambiente;
    public int lastSavedReportId;
    public static string nomeReport = "PATRIMONIOIMMOBILLIARE";                   //valore proprietario (fixed) di QUESTO report
    public static string nomeReportCompleto = "Nucleo completo";  //valore proprietario (fixed) di QUESTO report 
    public static string nomeReportRischio = "NUCLEO";                   //valore proprietario (fixed) di QUESTO report
    public static string nomeReportBaseRischio = "Nucleo base";          //valore proprietario (fixed) di QUESTO report 
    public static string nomeReportCompletoRischio = "Nucleo completo";  //valore proprietario (fixed) di QUESTO report 

    string nomeReportPerLabel;
    string descrizioneReportPerLabel;
    public int reportIdToLoadBack = -1;
    public string endOfjob = "0";

    //private bool reportNominativo = false;
    int IDReportToLoad = -1;
    public string baseCompleto;
    public string reportName;

    private int IdModello;
    private GestReport m = new GestReport();

    

    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Debug.iSDebug && Request.QueryString.Get("tipoRete") == null)
        {
            //Response.Redirect("reportMappatura.aspx?codPB=" + Debug.Agente + "&chiaveClientePB=681774&token=&tipoRete=" + Debug.Rete);
            //Response.Redirect("reportMappatura.aspx?codPB=014803&chiaveClientePB=348333&token=&tipoRete=F");
            Response.Redirect("reportMappatura.aspx?codPB=015312&chiaveClientePB=1452612&token=&tipoRete=F");
            return;
        }

        token = Request.QueryString.Get("token");
        codiceRete = Request.QueryString.Get("tipoRete");
        tipoRete = Request.QueryString.Get("tiporete").ToUpper();
        codicePB = Request.QueryString.Get("codPB");
        codPB = Request.QueryString.Get("codpb").ToUpper();
        codAs = Request.QueryString.Get("codas") != null ? Request.QueryString.Get("codas").ToUpper() : string.Empty;
        codiceUtenteQueryString = tipoRete.Trim() + codicePB.Trim();
        chiaveClientePB = Convert.ToInt32(Request.QueryString["chiaveClientePB"]);
        IdModello = codiceRete.Equals("S") ? 10 : 9;

       
        if (!Page.IsPostBack)
        {
           

            #region From Gestione Modelli
            //string loadingReport = Request.QueryString.Get("idReportToLoad");
            //nomeReportPerLabel = Request.QueryString.Get("nomeInizialeReport");
            //descrizioneReportPerLabel = Request.QueryString.Get("descrizioneInizialeReport");
            #endregion

            if (HFERRORMSG.Value.Equals(""))
            {                 
                m.TipoRete = codiceRete.Equals("F") ? "Banca Fideuram" : "SanPaolo Invest";
                m.AcquisisciModello(IdModello);
                m.AcquisisciElencoFabbricatiMappatura(chiaveClientePB);
            }


            List<string> vincoli = m.GetVincoliSchedeMappatura(chiaveClientePB);             
            m.SetEnableSchede(vincoli);
            if (vincoli.Exists(item => item == "flagnosoloabitazionicatasto"))
            {
                foreach (SchedaReport sr in m.Modello)
                {
                    if (sr.Sezioni == "135")
                    {
                        sr.Vincoli += "&SEN_Per stampare la scheda 8 si necessita la deselezione di almeno un immobile della scheda 7, altrimenti eliminare la scheda 8 dal report";
                        break;
                    }
                }
            }

            DatiHidden.Value = m.GetHtmlClient();


            SessionGeneratorPdf spdf = new SessionGeneratorPdf(chiaveClientePB, null, "mappatura", null);

            if (Debug.iSDebug) {
                Debug.LogFunzionalita("iSDebug", "return forzato");
                return; }

            
          
            #region Recupero token da AnagraficaWS.GetVersion

            if (string.IsNullOrEmpty(token))
            {
                string codPortal = WebConfigParameter.getParameter("CODICEPORTALEPERVERSIONESIMPB");

                InfoClient infoCliente = new InfoClient();
                infoCliente.codiceApplicazione = WebConfigParameter.getParameter("CODICEAPPLICAZIONE").ToString();
                infoCliente.codiceRete = codiceRete;
                infoCliente.versioneDatabaseSIMPB = 0;

                AnagraficaWS wsana = new AnagraficaWS();
                PBRes risposta = wsana.GetVersion(infoCliente, codPortal);
                token = risposta.info.versioneDatabaseSIMPB.ToString();
            }

            #endregion

            #region InfoClient e ChiaveCliente di GlobalPosition e Anagrafica

            // Global Position
            PrometeiaMessage.bancafideuram.nac.globalposition.InfoClient _infoCliente_GlobalPosition = new PrometeiaMessage.bancafideuram.nac.globalposition.InfoClient();
            _infoCliente_GlobalPosition.codiceApplicazione = WebConfigParameter.getParameter("CODICEAPPLICAZIONE").ToString();
            _infoCliente_GlobalPosition.versioneDatabaseSIMPB = int.Parse(token);


            PrometeiaMessage.bancafideuram.nac.globalposition.ChiaveCliente _chiaveCliente_GlobalPosition = new PrometeiaMessage.bancafideuram.nac.globalposition.ChiaveCliente();
            _chiaveCliente_GlobalPosition.potenzialeClienteNAC = true;

            #endregion

           

            #region Gestione Autorizzazione

            if (WebConfigParameter.getParameter("Ambiente") == "esterno")
                userName = User.Identity.Name;
            else
                userName = WebConfigParameter.getParameter("UtenzaWindowsAccessoHP");

            //1) utente loggato (VINCENZO UT.PROVA 2 MAGRI)
            //2) utente impersonificato (VIOTTI GIANCARLO)

            string AbilitazionePrimaria = string.Empty;

            codiceApplicazione = WebConfigParameter.getParameter("CodiceApplicazione");

            string codiceUtente = tipoRete + Utente.GetCodiceUtente(userName); // è il codiceutente con cui andrò a interrogare la ConsulenzaBase.dbo.PromotoreBancario per recuperare le informazioni dell'utente loggato

            Utente utenteLoggato_WS = Utente.GetAutorizzazioniAccessoGestioneControlli(userName, codiceApplicazione); // utente loggato (VINCENZO UT.PROVA 2 MAGRI), informazioni prese dai WS
            Utente utenteLoggato = Utente.GetProfiloUtente(codiceUtente, utenteLoggato_WS.ProfiloUtenteLoggato); // utente loggato (VINCENZO UT.PROVA 2 MAGRI) , informazioni prese da DB
            utenteLoggato.ProfiloUtenteLoggato = utenteLoggato_WS.ProfiloUtenteLoggato;
            utenteLoggato.AbilitazionePrimaria = utenteLoggato_WS.AbilitazionePrimaria;
            utenteLoggato.IsUtenteImpersonificato = utenteLoggato_WS.IsUtenteImpersonificato;
            utenteLoggato.UtenteAutorizzatoApplicazione = utenteLoggato_WS.UtenteAutorizzatoApplicazione;

            Utente utenteImpersonificato = Utente.GetProfiloUtente(codiceUtenteQueryString, null); // utente impersonificato (VIOTTI GIANCARLO)
            utenteImpersonificato.AbilitazionePrimaria = utenteLoggato.AbilitazionePrimaria;
            utenteImpersonificato.IsUtenteImpersonificato = utenteLoggato.IsUtenteImpersonificato;
            utenteImpersonificato.UtenteAutorizzatoApplicazione = utenteLoggato.UtenteAutorizzatoApplicazione;

            // FIX da valutare per TK T-300675-L4Y3
            if (!codAs.Equals(codPB))
                codiceUtenteQueryString = codiceUtente;

            Utente utenteSoloPerAutorizzazione = Utente.GetAutorizzazioni(userName, codiceApplicazione, tipoRete, codiceUtenteQueryString, Convert.ToInt32(token));

            if (utenteSoloPerAutorizzazione.UtenteAutorizzatoApplicazione)
            {
                if (Request.Browser.Cookies)
                {
                    #region utente Loggato (UtenteLoggatoDiSessione)

                    string utenteLoggatoString = Utente.SerializeMessage(utenteLoggato);

                    utenteLoggatoString = HttpUtility.UrlDecode(utenteLoggatoString.TrimStart().TrimEnd());
                    Session.Add("UtenteLoggatoDiSessione", utenteLoggatoString);

                    #endregion

                    #region utente Impersonificato (UtenteImpersonificatoDiSessione)

                    string utenteImpersonificatoString = Utente.SerializeMessage(utenteImpersonificato);

                    utenteImpersonificatoString = HttpUtility.UrlDecode(utenteImpersonificatoString.TrimStart().TrimEnd());
                    Session.Add("UtenteImpersonificatoDiSessione", utenteImpersonificatoString);

                    #endregion
                }
                else
                {
                    AbilitazionePrimaria = string.Empty;
                    if (utenteLoggato != null)
                        if (utenteLoggato.AbilitazionePrimaria != null)
                            AbilitazionePrimaria = utenteLoggato.AbilitazionePrimaria;

                    Response.Redirect("error.aspx?UtenteWindows=" + userName + "&UtentePB=" + codiceUtenteQueryString + "&CodiceApplicazione=" + WebConfigParameter.getParameter("CODICEAPPLICAZIONE") + "&VersioneSIMPB=" + token.ToString() + "&AbilitazionePrimaria=" + AbilitazionePrimaria);
                }
            }
            else
            {
                AbilitazionePrimaria = string.Empty;
                if (utenteLoggato != null)
                    if (utenteLoggato.AbilitazionePrimaria != null)
                        AbilitazionePrimaria = utenteLoggato.AbilitazionePrimaria;

                Response.Redirect("error.aspx?UtenteWindows=" + userName + "&UtentePB=" + codiceUtenteQueryString + "&CodiceApplicazione=" + WebConfigParameter.getParameter("CODICEAPPLICAZIONE") + "&VersioneSIMPB=" + token.ToString() + "&AbilitazionePrimaria=" + AbilitazionePrimaria + "&UtenteAutorizzato=" + utenteLoggato_WS.UtenteAutorizzatoApplicazione.ToString());
            }


            #endregion

            
        }
    }

     
 
     

    

}