var Calendar = function () {};

/*
 * Add Event when the App loaded
 */
Calendar.Load = function (parameter)
{
    this.LoadEvent();
    Calendar.InitTabEvent();
    Calendar.InitTabConfig();
};

/*
 * Add event
 */
Calendar.LoadEvent = function ()
{
    Dashboard.AddEventAppMenu(Calendar.Execute, "", "Calendar");
    Dashboard.AddEventWindowsTool("Calendar");
};

/***
 * Init Event Tab
 */
Calendar.InitTabEvent = function(){
    //Catégory
    Event.AddById("btnAddEvent", "click", () => {
        Calendar.ShowAddEvent("");
    });
    EntityGrid.Initialise('gdEvent');
};

/**
 * Edit event
 * @param {type} eventId
 * @returns {undefined}
 */
Calendar.EditEvent = function (eventId) {
    Calendar.ShowAddEvent(eventId);
};

/**
 * Dialogue d'ajout d'event
 */
Calendar.ShowAddEvent = function (eventId) {

    Dialog.open('', {"title": Dashboard.GetCode("Calendar.AddEvent"),
        "app": "Calendar",
        "class": "DialogAdminCalendar",
        "method": "AddEvent",
        "params": eventId
    });
};

/***
 * Rafraichit la tab des event
 * @returns {undefined}
 */
Calendar.ReloadEvent = function (data) {
    let gdEvent = Dom.GetById("gdEvent");
    gdEvent.parentNode.innerHTML = data;
    Calendar.InitTabEvent();
};

/***
 * Delete a event
 */
Calendar.DeleteEvent = function(eventId){

    Animation.Confirm(Language.GetCode("Calendar.ConfirmRemoveEvent"), function(){

        let request = new Http("Calendar", "DeleteEvent");
        request.Add("EventId", eventId);
        request.Post(data => {

            data = JSON.parse(data);

            if(data.status == "error"){
            } else{
                 Calendar.ReloadEvent(data.data.eventGrid);
            }
        });
    });
};

/***
 * edit user
 * @param {type} eventId
 * @returns {undefined}
 */
Calendar.EditEventUsers = function(eventId){
     Dialog.open('', {"title": Dashboard.GetCode("Calendar.AddEvent"),
        "app": "Calendar",
        "class": "DialogAdminCalendar",
        "method": "EditEventUsers",
        "params": eventId
    });
};

Calendar.InitTabConfig = function(){
    Event.AddById("btnAddPlugin", "click",     Calendar.AddPlugin);
    Event.AddByClass("removePlugin", "click" , Calendar.RemovePlugin);

    Calendar.RegisterPlugin();
};

/***
 * Registert the plugin
 * @returns {undefined}
 */
Calendar.RegisterPlugin = function(){
    let pluginScript = Dom.GetById("pluginScript");
    
    if(pluginScript){
        eval(pluginScript.innerHTML);
    }
};

/***
 * Ajout d'un plugin
 */
Calendar.AddPlugin = function(e){

    e.preventDefault();
    
        Dialog.open('', {"title": Dashboard.GetCode("Calendar.AddPlugin"),
        "app": "EeApp",
        "class": "DialogEeApp",
        "method": "AddPluginApp",
        "params": "Calendar",
        "type" : "right"
        });
};

/***
 * Refresh 
 * @returns {undefined}
 */
Calendar.RefreshPlugin = function(){
    Dashboard.StartApp("", "Calendar", "");
};

/***
  * Remove plugin to the calendar 
  * @returns {undefined}
  */
Calendar.RemovePlugin = function(e){
     
    Animation.Confirm(Language.GetCode("Calendar.RemovePlugin"), function(){
        let container = e.srcElement.parentNode;
        
        let  data = "Class=EeApp&Methode=RemovePluginApp&App=EeApp";
             data += "&pluginId="+e.srcElement.id;
             
        Request.Post("Ajax.php", data).then(data => {
            container.parentNode.removeChild(container);
        });
    });
};


/***
 * Start the Event for Member 
 */
Calendar.LoadMember = function () {
};

/*
 * Execute a function
 */
Calendar.Execute = function (e)
{
    //Call the function
    Dashboard.Execute(this, e, "Calendar");
    return false;
};

/***
 * Init the app front page
 */
Calendar.Init = function (app, method) {
    if (app == "Calendar") {
        switch (method) {
            case "Event" :
                CalendarEventUserForm.Init();
        
                break;
            default :
                break;
        }
    }
};

/***
 * Init the Admin Widget
 */
Calendar.InitAdminWidget = function () {
    Event.AddByClass("showUserCalendar","click", function(e){
        Calendar.EditEventUsers(e.srcElement.id);
    });
};