var plasma = getApiVersion(1);

let allDesktops = desktops();
// for (i=0;i<allDesktops.length;i++) {
//     desktop = allDesktops[i];
//     desktop.wallpaperPlugin = "org.kde.slideshow";
//     desktop.currentConfigGroup = Array("Wallpaper", "org.kde.slideshow", "General");
//     desktop.writeConfig("SlidePaths", "/usr/share/plasma/wallpapers,/usr/share/wallpapers");
// }

// for (i=0;i<allDesktops.length;i++) {
//     desktop = allDesktops[i];
//     desktop.wallpaperPlugin = "org.kde.image";
//     desktop.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");
//     desktop.writeConfig("Image", "/usr/share/wallpapers/Melawy");
//     desktop.writeConfig("SlidePaths", "/usr/share/plasma/wallpapers,/usr/share/wallpapers");
// }

/*General*/
var panel = new Panel;
panel.offset = 0;
panel.location = "floating";
panel.alignment = "center";
// panel.length = 105;

var panelScreen = panel.screen;
var freeEdges = {"bottom": true, "top": true, "left": true, "right": true};

for (i = 0; i < panelIds.length; ++i) {
    var tmpPanel = panelById(panelIds[i]);
    if (tmpPanel.screen == panelScreen) {
        // Ignore the new panel
        if (tmpPanel.id != panel.id) {
            freeEdges[tmpPanel.location] = false;
        }
    }
}

if (freeEdges["bottom"] == true) {
    panel.location = "bottom";
} else if (freeEdges["top"] == true) {
    panel.location = "top";
} else if (freeEdges["left"] == true) {
    panel.location = "left";
} else if (freeEdges["right"] == true) {
    panel.location = "right";
} else {
    // There is no free edge, so leave the default value
    panel.location = "top";
}

// Restrict horizontal panel to a maximum size of a 21:9 monitor
const maximumAspectRatio = 21/9;
if (panel.formFactor === "horizontal") {
    const geo = screenGeometry(panelScreen);
    const maximumWidth = Math.ceil(geo.height * maximumAspectRatio);

    if (geo.width > maximumWidth) {
        panel.alignment = "center";
        panel.minimumLength = maximumWidth;
        panel.maximumLength = maximumWidth;
    }
}

// For an Icons-Only Task Manager on the bottom, *3 is too much, *2 is too little
// Round down to next highest even number since the Panel size widget only displays
// even numbers
panel.height = Math.floor(gridUnit * 2);
panel.hiding = "none";

/*spacer for center view*/
// panel.addWidget("org.kde.plasma.panelspacer");

/*App Launcher*/
panel.addWidget("com.github.adhec.Menu11");

/*Find-Widget*/
panel.addWidget("org.kde.milou");

/*Desktops button*/
panel_desktops = panel.addWidget("com.github.zren.presentwindows");
panel_desktops.currentConfigGroup = ["General"];
panel_desktops.writeConfig("icon", "multitasking-view");
// panel_desktops.writeConfig("icon", "presentwindows-24px");
panel.addWidget("org.kde.plasma.icontasks");
panel.addWidget("org.kde.plasma.panelspacer");

/* Next up is determining whether to add the Input Method Panel
 * widget to the panel or not. This is done based on whether
 * the system locale's language id is a member of the following
 * white list of languages which are known to pull in one of
 * our supported IME backends when chosen during installation
 * of common distributions. */

var langIds = ["as",    // Assamese
               "bn",    // Bengali
               "bo",    // Tibetan
               "brx",   // Bodo
               "doi",   // Dogri
               "gu",    // Gujarati
               "hi",    // Hindi
               "ja",    // Japanese
               "kn",    // Kannada
               "ko",    // Korean
               "kok",   // Konkani
               "ks",    // Kashmiri
               "lep",   // Lepcha
               "mai",   // Maithili
               "ml",    // Malayalam
               "mni",   // Manipuri
               "mr",    // Marathi
               "ne",    // Nepali
               "or",    // Odia
               "pa",    // Punjabi
               "sa",    // Sanskrit
               "sat",   // Santali
               "sd",    // Sindhi
               "si",    // Sinhala
               "ta",    // Tamil
               "te",    // Telugu
               "th",    // Thai
               "ur",    // Urdu
               "vi",    // Vietnamese
               "zh_CN", // Simplified Chinese
               "zh_TW"]; // Traditional Chinese

if (langIds.indexOf(languageId) != -1) {
    panel.addWidget("org.kde.plasma.kimpanel");
}

function arrayItemRemove(arr, value) {

    return arr.filter(function(elem){
        return elem != value;
    });
}

/*systemtray*/
let systrayWidget = panel.addWidget("org.kde.plasma.systemtray");
let systemtrayId = systrayWidget.readConfig("SystrayContainmentId");
let systray = desktopById(systemtrayId);
systray.currentConfigGroup = ["General"];
let extraItems = systray.readConfig("extraItems").split(",");
extraItems = arrayItemRemove(extraItems, "org.kde.plasma.notifications");
systray.writeConfig("extraItems", extraItems);
systray.writeConfig("iconSpacing", 2);
systray.reloadConfig();

// systray.writeConfig("shownItems", "org.kde.plasma.mediacontroller,org.kde.plasma.volume,org.kde.plasma.networkmanagement,org.kde.plasma.weather,org.kde.plasma.battery");

/*Cambiando configuracion Dolphin*/
const IconsStatic_dolphin = ConfigFile('dolphinrc');
IconsStatic_dolphin.group = 'KFileDialog Settings';
IconsStatic_dolphin.writeEntry('Places Icons Static Size', 16);
const PlacesPanel = ConfigFile('dolphinrc');
PlacesPanel.group = 'PlacesPanel';
PlacesPanel.writeEntry('IconSize', 16);
/******************************/

/*Clock*/
panel_clock = panel.addWidget("org.kde.plasma.digitalclock");
panel_clock.currentConfigGroup = ["Appearance"];
panel_clock.writeConfig("fontSize", "11");
panel_clock.writeConfig("autoFontAndSize", "false");
panel_clock.writeConfig("showSeconds", "true");

/*Notification*/
panel.addWidget("org.kde.plasma.notifications");

/*Hide all windows*/
win7showdesktop = panel.addWidget("com.github.zren.win7showdesktop");
win7showdesktop.currentConfigGroup = ["General"];
win7showdesktop.writeConfig("edgeColor", "#66888888");
win7showdesktop.writeConfig("size", "5");

/* accent color config*/
ColorAccetFile = ConfigFile("kdeglobals");
ColorAccetFile.group = "General";
ColorAccetFile.deleteEntry("AccentColor");
ColorAccetFile.deleteEntry("LastUsedCustomAccentColor");
ColorAccetFile.writeEntry("accentColorFromWallpaper", "true");

/*Buttons of aurorae*/
Buttons = ConfigFile("kwinrc");
Buttons.group = "org.kde.kdecoration2";
Buttons.writeEntry("ButtonsOnLeft", "");
Buttons.writeEntry("ButtonsOnRight", "IAX");
// plasma.loadSerializedLayout(layout);
