var ImmoLeadPlugin = function () {};


/***
 * Add Event
 * @returns {undefined}
 */
ImmoLeadPlugin.Init = function () {
    Event.AddById("btnStartEstimate", "click", ImmoLeadPlugin.Estimate);
    Event.AddById("btnSendContactForm", "click", ImmoLeadPlugin.SendContact);
    Event.AddById("btnSendCalledBackForm", "click", ImmoLeadPlugin.SendCalledBack);
    Event.AddById("btnStartFind", "click", ImmoLeadPlugin.FindProperty);
};

/***
 * Init Front
 */
ImmoLeadPlugin.InitFront = function (app) {
    Event.AddByClass("immoLeadPicto", "click", function (e) {

        Dialog.open('', {"title": e.srcElement.title,
            "app": app,
            "class": "Dialog" + app,
            "method": "ShowActionUser",
            "params": e.srcElement.dataset.type
        });
    });
};

/*
 * Lancement de l'estimation 
 */
ImmoLeadPlugin.Estimate = function () {

    Animation.Hide("#btnStartEstimate");
    Animation.AddLoading("estimateBlock");
    Animation.Hide("#estimateError");

    let descriptionProperty = Dom.GetById("descriptionProperty");

    if (descriptionProperty.value != "") {

        Plugin.SendRequest({"plugin": "ImmoLeadPlugin",
            "method": "Estimate",
            "description": descriptionProperty.value}, (data) => {

            Animation.RemoveLoading("estimateBlock");
            data = JSON.parse(data);

            if (data.status == "error") {

                Animation.Show("#estimateError");
                Animation.Show("#btnStartEstimate");

                if (data.data.length == 0) {
                    Animation.Load("estimateError", Language.GetCode("ImmoLeadPlugin.NeedInformation") + " : Type de bien, surface, localisation");
                } else {
                    Animation.Load("estimateError", Language.GetCode("ImmoLeadPlugin.NeedInformation") + " :" + data.data.join(","));
                }
            } else {

                Animation.Hide("#estimateBlock");
                Animation.Load("estimatePrice", data.data.prix_estime + "&euro;");
                Animation.Load("estimateDescription", data.data.explication);
                Animation.Show("#resultEstimateBlock");
                Animation.Show("#contactBlock");
            }
        });
    } else {
        Animation.Show("#estimateError");
        Animation.Show("#btnStartEstimate");
        Animation.RemoveLoading("estimateBlock");
        Animation.Load("estimateError", Language.GetCode("ImmoLeadPlugin.YouMustEnterADescription"));
    }
};

/***
 * Demande de contact Estimation
 */
ImmoLeadPlugin.SendContact = function () {

    if (Form.IsValid("contactForm"))
    {
        Animation.AddLoading("contactForm");
        Animation.Hide("contactForm");

        Plugin.SendRequest({"plugin": "ImmoLeadPlugin",
            "method": "SendContact",
            "ProspectType": 1,
            "Description": Dom.GetById("descriptionProperty").value + ":" + Dom.GetById("estimateDescription").innerHTML,
            "FirstName": Form.GetValue("contactForm", "FirstName"),
            "Name": Form.GetValue("contactForm", "FirstName"),
            "Email": Form.GetValue("contactForm", "Email"),
            "Phone": Form.GetValue("contactForm", "Phone"),
        }, (data) => {
            //TODO VIDER TOUS LES CHAMSP

            Animation.RemoveLoading("contactForm");
            Animation.Show("contactForm");

            Animation.Notify(Language.GetCode("ImmoLeadPlugin.ContactFormRecevied"));
        });
    }
};

/***
 * Envoie d'une demande de rappel
 * @returns {undefined}
 */
ImmoLeadPlugin.SendCalledBack = function () {


    if (Form.IsValid("calledBackForm"))
    {
        Animation.AddLoading("calledBackForm");
        Animation.Hide("calledBackForm");

        Plugin.SendRequest({"plugin": "ImmoLeadPlugin",
            "method": "SendCallBackForm",
            "ProspectType": 1,
            "Description": Dom.GetById("descriptionProperty").value + ":" + Dom.GetById("estimateDescription").innerHTML,
            "Phone": Form.GetValue("calledBackForm", "Phone"),
            "DateCall": Form.GetValue("calledBackForm", "DateCall"),
            "HourCall": Form.GetValue("calledBackForm", "HourCall"),
        }, (data) => {
            Animation.RemoveLoading("calledBackForm");
            Animation.Show("calledBackForm");

            Animation.Notify(Language.GetCode("ImmoLeadPlugin.CalledBackFormReceveid"));
        });
    }
};

/***
 * Rechecher un bien
 * @returns {undefined}
 */
ImmoLeadPlugin.FindProperty = function () {

    Animation.Hide("btnStartFind");
    Animation.AddLoading("findPropertyBlock");

    let descriptionProperty = Dom.GetById("descriptionProperty");

    if (descriptionProperty.value != "") {

        Plugin.SendRequest({"plugin": "ImmoLeadPlugin",
            "method": "FindProperty",
            "description": descriptionProperty.value}, (data) => {

            Animation.RemoveLoading("findPropertyBlock");
            data = JSON.parse(data);

            if (data.status == "error") {

                Animation.Show("#findPropertyError");
                Animation.Show("#btnStartFind");

                if (data.data.length == 0) {
                    Animation.Load("findPropertyError", Language.GetCode("ImmoLeadPlugin.NeedInformation") + " : Type de bien, surface, localisation");
                } else {
                    Animation.Load("findPropertyError", Language.GetCode("ImmoLeadPlugin.NeedInformation") + " :" + data.data.join(","));
                }
            } else {

                Animation.Hide("#findPropertyBlock");
                // Animation.Load("estimatePrice", data.data.prix_estime + "&euro;");
                // Animation.Load("estimateDescription", data.data.explication);
                Animation.Show("#resultEstimateBlock");
                Animation.Show("#contactBlock");
            }
        });
    } else {
        Animation.Show("findPropertyError");
        Animation.Show("#btnStartFind");
        Animation.RemoveLoading("findPropertyBlock");
        Animation.Load("findPropertyError", Language.GetCode("ImmoLeadPlugin.YouMustEnterADescription"));
    }
};