// let plasma = getApiVersion(1);

let allDesktops = desktops();

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("FillMode", 0);

    desktop.wallpaperPlugin = "a2n.blur";
    desktop.currentConfigGroup = Array("Wallpaper", "a2n.blur", "General");
    desktop.writeConfig("FillMode", 0);
}

let desktopsArray = desktopsForActivity(currentActivity());
for(j = 0; j < desktopsArray.length; j++) {
    desktopsArray[j].wallpaperPlugin = 'org.kde.image';
    desktopsArray[j].currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");
    desktopsArray[j].writeConfig("FillMode", 0);

    desktopsArray[j].wallpaperPlugin = 'a2n.blur';
    desktopsArray[j].currentConfigGroup = Array("Wallpaper", "a2n.blur", "General");
    desktopsArray[j].writeConfig("FillMode", 0);
}

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


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

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

for (i = 0; i < panelIds.length; ++i) {
    let 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;
const geo = screenGeometry(panelScreen);

if (panel.formFactor === "horizontal") {
    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*/
// let max_width = 0;
// let max_height = 0;
//
// for (count = 0; count < screenCount; count++) {
//     geo = screenGeometry(count);
//     if (geo.width > max_width) {
//         max_width = geo.width;
//     }
//
//     if (geo.height > max_height) {
//         max_height = geo.height;
//     }
// }
//
// let aspect_ratio = max_width / max_height;

let aspect_ratio = geo.width / geo.height;

if (panel.formFactor === "horizontal" && geo.width > 1366 && aspect_ratio > 1.8) {
    panel.addWidget("org.kde.plasma.panelspacer");
}


/*App Launcher*/
let baseApps = [
"melawy-linux-welcome.desktop",
"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"],
["legcord.desktop", "goofcord.desktop"],
["Zoom.desktop", "us.zoom.Zoom.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 = panel.addWidget("DeepinMenu.Classic");
//start_menu = panel.addWidget("AndromedaLauncher");

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

if (geo.height > 768) {
    start_menu.writeConfig("numberRows", 5);
} else {
    start_menu.writeConfig("numberRows", 3);
}

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.writeConfig("useSystemFontSettings", "true");
start_menu.writeConfig("appsIconSize", "0");

if (aspect_ratio > 1.8) {
    // 0 = Auto, 1 = Center, 2 = Center Bottom
    start_menu.writeConfig("displayPosition", "2");

    // 0 = Auto, 1 = Center Bottom, 2 = Center
    start_menu.writeConfig("launcherPosition", "1");
} else {
    // 0 = Auto, 1 = Center, 2 = Center Bottom
    start_menu.writeConfig("displayPosition", "0");

    // 0 = Auto, 1 = Center Bottom, 2 = Center
    start_menu.writeConfig("launcherPosition", "0");
}


/*Desktops button*/
//panel.addWidget("org.kde.plasma.pager");
panel.addWidget("com.himdek.kde.plasma.overview");

icontasks = panel.addWidget("org.kde.plasma.icontasks");
icontasks.currentConfigGroup = [];
icontasks.currentConfigGroup = ["General"];
icontasks.writeConfig("indicateAudioStreams", "true");
icontasks.writeConfig("iconSpacing", "0");
// // icontasks.writeConfig("launchers", "");
// icontasks.writeConfig("launchers", `${applications}`);
icontasks.writeConfig("maxStripes", "1");


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. */

let 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");
systrayWidget.currentConfigGroup = [];
systrayWidget.currentConfigGroup = ["General"];


let extraItems = systrayWidget.readConfig("extraItems").split(",");
extraItems = arrayItemRemove(extraItems, "org.kde.plasma.notifications");
systrayWidget.writeConfig("extraItems", extraItems);
systrayWidget.writeConfig("iconSpacing", 2);
systrayWidget.writeConfig("shownItems", "org.kde.plasma.mediacontroller,org.kde.plasma.volume,org.kde.plasma.networkmanagement,org.kde.plasma.keyboardlayout");
systrayWidget.writeConfig("hiddenItems", "firewall-applet,org.kde.plasma.bluetooth,org.kde.kdeconnect,org.kde.plasma.brightness,org.kde.plasma.keyboardindicator,org.kde.plasma.clipboard,org.kde.plasma.weather,org.kde.plasma.vault,org.kde.plasma.printmanager,org.kde.kscreen,org.kde.plasma.manage-inputmethod");

/* Icons configuration 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*/
// showdesktop = panel.addWidget("org.kde.plasma.showdesktop");
showdesktop = panel.addWidget("org.kde.plasma.minimizeall");
// showdesktop = panel.addWidget("org.kde.plasma.win7showdesktop");
showdesktop.currentConfigGroup = ["General"];
showdesktop.writeConfig("edgeColor", "#66888888");
showdesktop.writeConfig("hoveredColor", "#66777777");
showdesktop.writeConfig("pressedColor", "#66555555");
showdesktop.writeConfig("size", 7);


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


/* widgetStyle config*/
WidgetStyleFile = ConfigFile("kdeglobals");
WidgetStyleFile.group = "KDE";
WidgetStyleFile.writeEntry("widgetStyle", "Klassy");


/* Buttons of aurorae */
Buttons = ConfigFile("kwinrc");
Buttons.group = "org.kde.kdecoration2";
Buttons.writeEntry("ButtonsOnLeft", "MFS");
// Buttons.deleteEntry("ButtonsOnLeft");
Buttons.writeEntry("ButtonsOnRight", "HIAX");
// Buttons.deleteEntry("ButtonsOnRight");


/* Decorator of aurorae */
Decorator = ConfigFile("kwinrc");
Decorator.group = "org.kde.kdecoration2";
Decorator.writeEntry("BorderSize", "Normal");
Decorator.writeEntry("BorderSizeAuto", "false");
Decorator.writeEntry("library", "org.kde.klassy");
Decorator.writeEntry("theme", "Klassy");


/* Plugins of aurorae */
Plugins = ConfigFile("kwinrc");
Plugins.group = "Plugins";
Plugins.writeEntry("cubeEnabled", "true");
Plugins.writeEntry("desktopchangeosdEnabled", "true");
Plugins.writeEntry("diminactiveEnabled", "false");
Plugins.deleteEntry("diminactiveEnabled");
Plugins.writeEntry("dimscreenEnabled", "true");
Plugins.writeEntry("fadedesktopEnabled", "true");

let forceblur = false;

if (forceblur) {
    Plugins.writeEntry("forceblurEnabled", "true");
    Plugins.writeEntry("blurEnabled", "false");
    Plugins.writeEntry("contrastEnabled", "false");
    Plugins.writeEntry("kwin4_effect_shapecornersEnabled", "false");
} else {
    Plugins.writeEntry("forceblurEnabled", "false");
    Plugins.writeEntry("blurEnabled", "true");
    Plugins.writeEntry("contrastEnabled", "true");
    Plugins.writeEntry("kwin4_effect_shapecornersEnabled", "true");
}

let kinetic = true;

if (kinetic) {
    Plugins.writeEntry("kinetic_maximizeEnabled", "true");
    Plugins.writeEntry("maximizeEnabled", "false");

    Plugins.writeEntry("kinetic_fadingpopupsEnabled", "true");
    Plugins.writeEntry("fadingpopupsEnabled", "false");

    Plugins.writeEntry("kinetic_scaleEnabled", "true");
    Plugins.writeEntry("scaleEnabled", "false");

    Plugins.writeEntry("kinetic_squashEnabled", "true");
    Plugins.writeEntry("squashEnabled", "false");
} else {
    Plugins.writeEntry("maximizeEnabled", "true");
    Plugins.writeEntry("kinetic_maximizeEnabled", "false");

    Plugins.writeEntry("fadingpopupsEnabled", "true");
    Plugins.writeEntry("kinetic_fadingpopupsEnabled", "false");

    Plugins.writeEntry("scaleEnabled", "true");
    Plugins.writeEntry("kinetic_scaleEnabled", "false");

    Plugins.writeEntry("squashEnabled", "true");
    Plugins.writeEntry("kinetic_squashEnabled", "false");
}


Plugins.writeEntry("kwin4_effect_cornersshaderEnabled", "true");
Plugins.writeEntry("kwin4_effect_fadedesktopEnabled", "true");
Plugins.writeEntry("kwin4_effect_translucencyEnabled", "true");
Plugins.writeEntry("kwin_effect_lightlyshadersEnabled", "false");
Plugins.deleteEntry("kwin_effect_lightlyshadersEnabled");

Plugins.writeEntry("presentwindowsEnabled", "false");
Plugins.deleteEntry("presentwindowsEnabled");
Plugins.writeEntry("slideEnabled", "false");
Plugins.writeEntry("translucencyEnabled", "true");


/* Effect-blurplus */
Overview = ConfigFile("kwinrc");
Overview.group = "Effect-blurplus";
Overview.writeEntry("BlurDecorations", "true");
Overview.writeEntry("BlurMenus", "true");
Overview.writeEntry("PaintAsTranslucent", "true");
Overview.writeEntry("TopCornerRadius", 5);
Overview.writeEntry("BottomCornerRadius", 5);
Overview.writeEntry("DockCornerRadius", 5);
Overview.writeEntry("MenuCornerRadius", 5);


/* Effect-overview */
Overview = ConfigFile("kwinrc");
Overview.group = "Effect-overview";
Overview.writeEntry("BorderActivate", 9);


/* Effect-translucency */
Translucency = ConfigFile("kwinrc");
Translucency.group = "Effect-translucency";
// Translucency.writeEntry("Inactive", 95);
Translucency.deleteEntry("Inactive");
Translucency.writeEntry("MoveResize", 85);


/* PrimaryOutline */
PrimaryOutline = ConfigFile("kwinrc");
PrimaryOutline.group = "PrimaryOutline";
PrimaryOutline.writeEntry("InactiveOutlineColor", "80,80,80");
PrimaryOutline.writeEntry("OutlineColor", "80,80,80");


/* Windows of aurorae */
Windows = ConfigFile("kwinrc");
Windows.group = "Windows";
Windows.writeEntry("BorderlessMaximizedWindows", "false");
Windows.deleteEntry("BorderlessMaximizedWindows");



// plasma.loadSerializedLayout(layout);
