var ClickTrackPlugin = function () {};

/***
 * Init the plugin
 * @returns {undefined}
 */
ClickTrackPlugin.Init = function () {

    Event.AddByClass("clickTrackPicto", "click", ClickTrackPlugin.OpenAction);
};

ClickTrackPlugin.OpenAction = function (e) {
    Dialog.open('', {"title": e.srcElement.title,
        "html": "<div id='ClickTrackPluginDialog'></div>",
    });

    ClickTrackPlugin.entityName = document.querySelector("#clickTrackPlugin #EntityName").value;
    ClickTrackPlugin.entityId = document.querySelector("#clickTrackPlugin #EntityId").value;

    Plugin.SendRequest({"plugin": "ClickTrackPlugin",
        "method": "OpenAction",
        "Action": e.srcElement.dataset.type,
        "EntityName": ClickTrackPlugin.entityName,
        "EntityId": ClickTrackPlugin.entityId
    }
    , (data) => {
        Animation.Load("ClickTrackPluginDialog", data);

        Dialog.Reposition();

        Event.AddById("btnSendContactForm", "click", ClickTrackPlugin.SendContactForm);

    });

    ClickTrackPlugin.SendContactForm = function () {

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

            Plugin.SendRequest({"plugin": "ClickTrackPlugin",
                "method": "SendContactForm",
                "FirstName": Form.GetValue("contactForm", "FirstName"),
                "Name": Form.GetValue("contactForm", "Name"),
                "Email": Form.GetValue("contactForm", "Email"),
                "Phone": Form.GetValue("contactForm", "Phone"),
                "Message": Form.GetValue("contactForm", "Message"),
                "EntityName": ClickTrackPlugin.entityName,
                "EntityId": ClickTrackPlugin.entityId
            }
            , (data) => {
                Animation.RemoveLoading("contactForm");
                Animation.Load("contactForm", data);
                Animation.Show("contactForm");
            });
          }
    };
};