var CaracteristicPlugin = function(){};

CaracteristicPlugin.Init = function(){
    
    //Admin Event
    Event.AddById('btnAddCaracteristicCategory', "click" , CaracteristicPlugin.AddCategory);
    Event.AddByClass("removeCaracteristic", "click", CaracteristicPlugin.RemoveCaracteristic);
    Event.AddByClass("removeCaracteristicEntity", "click", CaracteristicPlugin.RemoveToEntity);
    EntityGrid.Init("gdCaracteristicCategory");

    //Member Event
    Event.AddById('lstCaracteristicCategory', "change" , CaracteristicPlugin.LoadCaracteristic);
};

/*
* Add a competence
*/
CaracteristicPlugin.AddCategory = function(){
    let name = Dom.GetById("newCaracteristicCategoryName");
    let appName = Dom.GetById("CaracteristicAppName");

    if(name.value != ""){

        Plugin.SendRequest({"plugin": "CaracteristicPlugin",
            "method": "AddCategory",
            "name": name.value, "appName" : appName.value}, (data) => {

            let gdCaracteristicCategory = Dom.GetById("gdCaracteristicCategory");
            gdCaracteristicCategory.parentNode.parentNode.innerHTML = data;
            CaracteristicPlugin.Init();
        });
    } else{
        Animation.Notify("Caracteristic.YouMustEnterName");
    }
};

/***
 * Delete a category
 */
CaracteristicPlugin.DeleteCategory = function(categoryId){

     Animation.Confirm(Language.GetCode("Base.ConfirmRemoveCategory"), function(){

        let appName = Dom.GetById("CaracteristicAppName");

        Plugin.SendRequest({"plugin": "CaracteristicPlugin",
        "method": "DeleteCategory",
        "id" : categoryId,
        "appName" : appName.value
        }, (data) => {

            let gdCaracteristicCategory = Dom.GetById("gdCaracteristicCategory");
            gdCaracteristicCategory.parentNode.parentNode.innerHTML = data;
            CaracteristicPlugin.Init();
      });
    });
};

/***
 * Add Competence to a categorie
 */
CaracteristicPlugin.AddCaracteristic = function(categoryId){

    Animation.Ask(Language.GetCode("Base.Name"), function(response){

        Plugin.SendRequest({"plugin": "CaracteristicPlugin",
        "method": "AddCaracteristic",
        "categoryId" : categoryId,
        "name": response}, (data) => {

            Animation.Load("competenceCategory-" +categoryId , data);
      });
    });
};

/***
 * Remove a Caracteristic
 */
CaracteristicPlugin.RemoveCaracteristic = function(e){

    Animation.Confirm(Language.GetCode("Caracteristic.RemoveCaracteristic"), function(){

        Plugin.SendRequest({"plugin": "CaracteristicPlugin",
        "method": "RemoveCaracteristic",
        "id" : e.srcElement.id,
        }, (data) => {

            let container = e.srcElement.parentNode;
            container.parentNode.removeChild(container);
      });
    });
};

/***
 * Load all Caracteristic of a category
 * @returns {undefined}
 */
CaracteristicPlugin.LoadCaracteristic = function(e){
     
    Plugin.SendRequest({"plugin": "CaracteristicPlugin",
        "method": "GetCaracteristicsByCategory",
        "id" : e.srcElement.value,
        }, (data) => {

            let container = Dom.GetById("CaracteristicContainer");
            container.innerHTML = data;
            
            Event.AddByClass("addCaracteristicEntity", "click", CaracteristicPlugin.AddToEntity);
            Event.AddById("CaracteristicValue", "blur", CaracteristicPlugin.AddToEntity);
      });
 }; 
 
/***
 * Add Caracteristic to a entity
 * @returns {undefined}
 */ 
CaracteristicPlugin.AddToEntity = function(e){
    let appName = Dom.GetById("CaracteristicAppName");
    let entityId = Dom.GetById("CaracteristicEntityId");  
    let value = Dom.GetById("CaracteristicValue");  
    
    Plugin.SendRequest({"plugin": "CaracteristicPlugin",
        "method": "AddToEntity",
        "id" : e.srcElement.id != "CaracteristicValue" ?  e.srcElement.id :  e.srcElement.dataset.categorie,
        "appName" : appName.value,
        "entityId" : entityId.value,
        "description" : value ? value.value : ""
        
    }, (data) => {

            let container = Dom.GetById("CaracteristicEntity");
            container.innerHTML = data;
            
            Event.AddByClass("removeCaracteristicEntity", "click", CaracteristicPlugin.RemoveToEntity);
      });
 };
 
 /***
  * Remove a Caracteristic to a entity
  * @param {type} e
  * @returns {undefined}
  */
 CaracteristicPlugin.RemoveToEntity = function(e){
     
     Animation.Confirm(Language.GetCode("Caracteristic.RemoveCaracteristicEntity"), function(){

        Plugin.SendRequest({"plugin": "CaracteristicPlugin",
        "method": "RemoveToEntity",
        "id" : e.srcElement.id,
        }, (data) => {

            let container = e.srcElement.parentNode;
            container.parentNode.removeChild(container);
      });
    });
 };

 /****
  * Gestion des filtres des caractéristiques 
  */
 CaracteristicPlugin.InitFilter = function(callBack){

    Event.AddById("btnFilterCaracteristic", "click", function(){
        callBack(Form.Serialize("filterContainer"));
    });
 };