package main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"go.fanir.de/statusline/config"
	"go.fanir.de/statusline/cpu"
	"go.fanir.de/statusline/mem"
)

func Print(s string, err error) {
	if err != nil {
		panic(err) // FIXME
	}
	fmt.Print(s)
}

func Time(format config.Format, timeFormat string) string {
	now := time.Now()

	return fmt.Sprint(format.Default, now.Format(timeFormat))
}

func main() {
	cols := flag.Uint("c", 0, "Window width in columns. 0 prints everything.")
	mode := flag.String("style", "none", "Include style directives in output. Valid values: none tmux")
	flag.Parse()

	format := config.DefaultFormat
	switch *mode {
	case "", "none":
	case "tmux":
		format = config.TmuxFormat
	default:
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(2)
	}

	if *cols == 0 || *cols >= 150 {
		Print(cpu.Load(format, 2))
		Print(mem.Mem(format, true, true))
		fmt.Print(Time(format, " Mon 2006-01-02 15:04"))
	} else if *cols >= 120 {
		Print(cpu.Load(format, 2))
		Print(mem.Mem(format, false, true))
		fmt.Print(Time(format, " 15:04"))
	} else if *cols >= 100 {
		Print(cpu.Load(format, 0))
		Print(mem.Mem(format, false, true))
		fmt.Print(Time(format, " 15:04"))
	} else if *cols >= 80 {
		fmt.Print(Time(format, "15:04"))
	}
}