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");
// }

// for (let i = 0; i < allDesktops.length; i++) {
//     let desktop = allDesktops[i];
//
//     // Add the widget to the desktop
//     let widget = desktop.addWidget("luisbocanegra.desktop.wallpaper.effects");
//
//     widget.currentConfigGroup = ["General"];
//     widget.writeConfig("colorizationColor", "#1a7597");
//     widget.writeConfig("hideWidget", true);
// }

let desktop = allDesktops[0];

// Add the widget to the desktop
let widget = desktop.addWidget("luisbocanegra.desktop.wallpaper.effects");

widget.currentConfigGroup = ["General"];
widget.writeConfig("colorizationColor", "#1a7597");
widget.writeConfig("hideWidget", true);

/*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*/
let baseApps = [
    "melawy-welcome.desktop",
    "systemsettings.desktop",
    "pamac.desktop",
    "org.kde.dolphin.desktop",
    "org.kde.konsole.desktop",
    "org.kde.kcalc.desktop",
    "org.kde.kate.desktop"
];

let addApps = [
    ["code.desktop", "com.visualstudio.code.desktop"],
    ["org.telegram.desktop.desktop", "org.telegram.desktop.desktop"],
    ["discord.desktop", "com.discordapp.Discord.desktop"],
    ["Zoom.desktop", "us.zoom.Zoom.desktop"],
    ["firefoxdeveloperedition.desktop", "firefoxdeveloperedition.desktop"],
    ["firefox-developer-edition.desktop", "firefox-developer-edition.desktop"],
    ["firefox.desktop", "org.mozilla.firefox.desktop"],
    ["brave-browser.desktop", "com.brave.Browser.desktop"],
    ["google-chrome.desktop", "com.google.Chrome.desktop"]
];


let applications = '';

baseApps.forEach((i) => {
    if (applicationExists(i))  {
        applications += i + ',';
    }
});

addApps.forEach((i) => {
    if (applicationExists(i[0]))  {
        applications += i[0] + ',';
    } else if (applicationExists(i[1]))  {
        applications += i[1] + ',';
    }
});

applications = applications.substring(0, applications.length - 1).split(',');

//start_menu = panel.addWidget("org.kde.plasma.kickoff");
start_menu = panel.addWidget("com.github.adhec.Menu11");

start_menu.PreloadWeight = 100;
start_menu.popupHeight = 720;
start_menu.popupWidth =1000;

start_menu.currentConfigGroup = ["General"]
start_menu.writeConfig("favoritesPortedToKAstats", "true");
start_menu.writeConfig("icon", "start-here-kde");
start_menu.writeConfig("showActionButtonCaptions", "false");

//start_menu.writeConfig("systemFavorites", "suspend\\,hibernate\\,reboot\\,shutdown");
//start_menu.writeConfig("systemFavorites", "shutdown");
start_menu.writeConfig("systemFavorites", "");

start_menu.writeConfig("favorites", `${applications}`);
start_menu.writeConfig("appDescription", "hidden");
start_menu.writeConfig("appListWidth", "400");
start_menu.writeConfig("compactMode", "true");
start_menu.writeConfig("defaultAppListView", "TilesOnly");
start_menu.writeConfig("defaultTileColor", "#e4606060");
start_menu.writeConfig("defaultTileGradient", "true");
start_menu.writeConfig("favGridCols", "7");
start_menu.writeConfig("floating", "true");
start_menu.writeConfig("fullscreen", "true");
start_menu.writeConfig("groupLabelAlignment", "center");
start_menu.writeConfig("menuItemHeight", "24");
start_menu.writeConfig("numRecentApps", "10");
start_menu.writeConfig("popupHeight", "700");
start_menu.writeConfig("searchFieldFollowsTheme", "true");
start_menu.writeConfig("searchFieldHeight", "36");
start_menu.writeConfig("searchResultsMerged", "false");
start_menu.writeConfig("sidebarButtonSize", "36");
start_menu.writeConfig("sidebarFollowsTheme", "true");
start_menu.writeConfig("sidebarIconSize", "24");
start_menu.writeConfig("tileLabelAlignment", "center");
start_menu.reloadConfig();

/*Desktops button*/
panel.addWidget("org.kde.plasma.pager");
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", "12");
panel_clock.writeConfig("autoFontAndSize", "true");
panel_clock.writeConfig("showSeconds", "Always");

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

/*Hide all windows*/
// panel.addWidget("org.kde.plasma.showdesktop");
win7showdesktop = panel.addWidget("org.kde.plasma.win7showdesktop");
win7showdesktop.currentConfigGroup = ["General"];
win7showdesktop.writeConfig("edgeColor", "#66888888");
win7showdesktop.writeConfig("hoveredColor", "#66777777");
win7showdesktop.writeConfig("pressedColor", "#66555555");
win7showdesktop.writeConfig("size", "7");

/* 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);
