import * as FS from 'fs/promises'
import * as Path from 'path'
import * as SASS from 'sass'

let bar = {}

// const Section = {
//     START: 'modules-left',
//     MIDDLE: 'modules-center',
//     END: 'modules-right'
// }
const Section = {
    START: 'modules-left',
    MIDDLE: 'modules-center',
    END: 'modules-right'
}

function main() {
    bar[Section.START] = []
    bar[Section.MIDDLE] = []
    bar[Section.END] = []

    // bar.mode = 'overlay'
    // bar.exclusive = true
    // bar.position = 'top'
    // bar.height = 28
    bar.layer = 'top'
    bar.position = 'left'
    // bar.width = 28
    bar.width = 38
    bar.spacing = 0

    module('custom/power', {
        format: "\ue8ac",
        'on-click': "pkill rofi || rofi-power",
        // 'exec': "~/.config/waybar/watch-power-menu-state",
        'return-type': "json"
    }, Section.START)

    module('custom/rofication', {
        format: "{icon}",
        'format-icons': {
                "none": "\ue7f4",
                "some": "\uf4fe",
                "critical": "\uf4fe",
                "error": "\ue004"
        },
        'interval': 2,
        'on-click': "pkill rofication-gui || rofication-gui",
        'exec': "rofication-status",
        'return-type': "json"
    }, Section.START)

    module('keyboard-state', {
        "numlock": true,
        "capslock": true,
        "format": {
            "numlock": "N{icon}",
            "capslock": "C{icon}"
        },
        "format-icons": {
            "locked": "",
            "unlocked": ""
        }
    }, Section.START)

    module('niri/workspaces', {
        // format: ""
    }, Section.START)

    module('niri/window', {
        format: "{}",
        rotate: 90
    }, Section.MIDDLE)

    // module('temperature', {
    //     format: "{temperatureC}°C"
    // }, Section.END)

    group('group/audio', {
        orientation: 'vertical',
        drawer: {
            'transition-left-to-right': false,
            'transition-duration': 200
        }
    }, [
        component('pulseaudio#main', {
            format: "{icon}",
                // 'format-bluetooth': "{icon}\n\ue1a8",
                "format-icons": {
                    "alsa_output.pci-0000_00_1f.3.analog-stereo": "\ue050",
                    "alsa_output.pci-0000_00_1f.3.analog-stereo-muted": "\ue04f",
                    "headphone": "\uf01f",
                    "hands-free": "\uf324",
                    "headset": "\ue311",
                    "phone": "\ue9cd",
                    "phone-muted": "\ue9cc",
                    "default": ["\ue04e", "\ue04d", "\ue050" ]
                },
                "format-muted": "\ue04f",
                // "format-icons": {
                //     "mute": "\ue04e",
                //     "quiet": "\ue04d",
                //     "medium": "\ue050",
                //     "loud": "\ue050"
                // },
                // states: [ "mute", "quiet", "medium", "loud" ],
                'scroll-step': 2,
                'on-click': 'pavucontrol'
        }),
        component('pulseaudio#preview', {
            format: "{volume}%",
                // rotate: 90
        }),
        component('pulseaudio/slider', {
            "min": 0,
            "max": 100,
            "orientation": "vertical"
        })
    ], Section.END)

    module('tray', {
        'icon-size': 20,
        'show-passive-items': true,
        'spacing': 3
    }, Section.END)

    module('bluetooth', {
        "format": "\ue1a7",
        "format-connected": "\ue1a8",
        // "format-connected-battery": "\ue1a8",
        "tooltip-format": "{controller_alias}\t{controller_address}\n\n{num_connections} connected",
        "tooltip-format-connected": "{controller_alias}\t{controller_address}\n\n{num_connections} connected\n\n{device_enumerate}",
        "tooltip-format-enumerate-connected": "{device_alias}\t{device_address}",
        "tooltip-format-enumerate-connected-battery": "{device_alias}\t{device_address}\t{device_battery_percentage}%"
    }, Section.END)

    // module('upower', {
    //     "icon-size": 20,
    //     "hide-if-empty": true,
    //     "tooltip": true,
    //     "tooltip-spacing": 20,
    //     // 'show-icon': false,
    // }, Section.END)


    const clockCommon = {
        tooltip: true,
        "tooltip-format": "{:%Y-%m-%d}",
        "max-length": 5,
        "interval": 60,
    }
    module('clock#hour', {
        "format": "{:%I}",
        ...clockCommon
    }, Section.END)

    module('clock#minutes', {
        "format": "{:%M}",
        ...clockCommon
    }, Section.END)

    // module('power', {})
}

function module(name, config, position) {
    component(name, config)
    bar[position].push(name)
}

function component(name, config) {
    bar[name] = config
    return name
}

function group(name, options, components, position) {
    let d = {
        ...options,
        modules: components
    }
    module(name, d, position)
}

main()
await FS.writeFile(
    Path.join(import.meta.dirname, 'config.jsonc'),
    JSON.stringify(bar, null, 4)
)
// await FS.writeFile(
//     Path.join(import.meta.dirname, 'style.css'),
//     await SASS.compileAsync('style.scss').then(r => r.css)
// )
