// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package at

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"github.com/kovidgoyal/kitty/tools/utils"
	"github.com/kovidgoyal/kitty/tools/utils/style"
)

var nullable_colors = map[string]bool{
	// generated by gen-config.py do not edit
	// NULLABLE_COLORS_START
	"active_border_color":                  true,
	"color100":                             true,
	"color101":                             true,
	"color102":                             true,
	"color103":                             true,
	"color104":                             true,
	"color105":                             true,
	"color106":                             true,
	"color107":                             true,
	"color108":                             true,
	"color109":                             true,
	"color110":                             true,
	"color111":                             true,
	"color112":                             true,
	"color113":                             true,
	"color114":                             true,
	"color115":                             true,
	"color116":                             true,
	"color117":                             true,
	"color118":                             true,
	"color119":                             true,
	"color120":                             true,
	"color121":                             true,
	"color122":                             true,
	"color123":                             true,
	"color124":                             true,
	"color125":                             true,
	"color126":                             true,
	"color127":                             true,
	"color128":                             true,
	"color129":                             true,
	"color130":                             true,
	"color131":                             true,
	"color132":                             true,
	"color133":                             true,
	"color134":                             true,
	"color135":                             true,
	"color136":                             true,
	"color137":                             true,
	"color138":                             true,
	"color139":                             true,
	"color140":                             true,
	"color141":                             true,
	"color142":                             true,
	"color143":                             true,
	"color144":                             true,
	"color145":                             true,
	"color146":                             true,
	"color147":                             true,
	"color148":                             true,
	"color149":                             true,
	"color150":                             true,
	"color151":                             true,
	"color152":                             true,
	"color153":                             true,
	"color154":                             true,
	"color155":                             true,
	"color156":                             true,
	"color157":                             true,
	"color158":                             true,
	"color159":                             true,
	"color16":                              true,
	"color160":                             true,
	"color161":                             true,
	"color162":                             true,
	"color163":                             true,
	"color164":                             true,
	"color165":                             true,
	"color166":                             true,
	"color167":                             true,
	"color168":                             true,
	"color169":                             true,
	"color17":                              true,
	"color170":                             true,
	"color171":                             true,
	"color172":                             true,
	"color173":                             true,
	"color174":                             true,
	"color175":                             true,
	"color176":                             true,
	"color177":                             true,
	"color178":                             true,
	"color179":                             true,
	"color18":                              true,
	"color180":                             true,
	"color181":                             true,
	"color182":                             true,
	"color183":                             true,
	"color184":                             true,
	"color185":                             true,
	"color186":                             true,
	"color187":                             true,
	"color188":                             true,
	"color189":                             true,
	"color19":                              true,
	"color190":                             true,
	"color191":                             true,
	"color192":                             true,
	"color193":                             true,
	"color194":                             true,
	"color195":                             true,
	"color196":                             true,
	"color197":                             true,
	"color198":                             true,
	"color199":                             true,
	"color20":                              true,
	"color200":                             true,
	"color201":                             true,
	"color202":                             true,
	"color203":                             true,
	"color204":                             true,
	"color205":                             true,
	"color206":                             true,
	"color207":                             true,
	"color208":                             true,
	"color209":                             true,
	"color21":                              true,
	"color210":                             true,
	"color211":                             true,
	"color212":                             true,
	"color213":                             true,
	"color214":                             true,
	"color215":                             true,
	"color216":                             true,
	"color217":                             true,
	"color218":                             true,
	"color219":                             true,
	"color22":                              true,
	"color220":                             true,
	"color221":                             true,
	"color222":                             true,
	"color223":                             true,
	"color224":                             true,
	"color225":                             true,
	"color226":                             true,
	"color227":                             true,
	"color228":                             true,
	"color229":                             true,
	"color23":                              true,
	"color230":                             true,
	"color231":                             true,
	"color232":                             true,
	"color233":                             true,
	"color234":                             true,
	"color235":                             true,
	"color236":                             true,
	"color237":                             true,
	"color238":                             true,
	"color239":                             true,
	"color24":                              true,
	"color240":                             true,
	"color241":                             true,
	"color242":                             true,
	"color243":                             true,
	"color244":                             true,
	"color245":                             true,
	"color246":                             true,
	"color247":                             true,
	"color248":                             true,
	"color249":                             true,
	"color25":                              true,
	"color250":                             true,
	"color251":                             true,
	"color252":                             true,
	"color253":                             true,
	"color254":                             true,
	"color255":                             true,
	"color26":                              true,
	"color27":                              true,
	"color28":                              true,
	"color29":                              true,
	"color30":                              true,
	"color31":                              true,
	"color32":                              true,
	"color33":                              true,
	"color34":                              true,
	"color35":                              true,
	"color36":                              true,
	"color37":                              true,
	"color38":                              true,
	"color39":                              true,
	"color40":                              true,
	"color41":                              true,
	"color42":                              true,
	"color43":                              true,
	"color44":                              true,
	"color45":                              true,
	"color46":                              true,
	"color47":                              true,
	"color48":                              true,
	"color49":                              true,
	"color50":                              true,
	"color51":                              true,
	"color52":                              true,
	"color53":                              true,
	"color54":                              true,
	"color55":                              true,
	"color56":                              true,
	"color57":                              true,
	"color58":                              true,
	"color59":                              true,
	"color60":                              true,
	"color61":                              true,
	"color62":                              true,
	"color63":                              true,
	"color64":                              true,
	"color65":                              true,
	"color66":                              true,
	"color67":                              true,
	"color68":                              true,
	"color69":                              true,
	"color70":                              true,
	"color71":                              true,
	"color72":                              true,
	"color73":                              true,
	"color74":                              true,
	"color75":                              true,
	"color76":                              true,
	"color77":                              true,
	"color78":                              true,
	"color79":                              true,
	"color80":                              true,
	"color81":                              true,
	"color82":                              true,
	"color83":                              true,
	"color84":                              true,
	"color85":                              true,
	"color86":                              true,
	"color87":                              true,
	"color88":                              true,
	"color89":                              true,
	"color90":                              true,
	"color91":                              true,
	"color92":                              true,
	"color93":                              true,
	"color94":                              true,
	"color95":                              true,
	"color96":                              true,
	"color97":                              true,
	"color98":                              true,
	"color99":                              true,
	"cursor":                               true,
	"cursor_text_color":                    true,
	"cursor_trail_color":                   true,
	"selection_background":                 true,
	"selection_foreground":                 true,
	"tab_bar_background":                   true,
	"tab_bar_margin_color":                 true,
	"visual_bell_color":                    true,
	"window_title_bar_active_background":   true,
	"window_title_bar_active_foreground":   true,
	"window_title_bar_inactive_background": true,
	"window_title_bar_inactive_foreground": true,
	// NULLABLE_COLORS_END
}

func set_color_in_color_map(key, val string, ans map[string]any, check_nullable, skip_nullable bool) error {
	if val == "none" {
		if check_nullable && !nullable_colors[key] {
			if skip_nullable {
				return nil
			}
			return fmt.Errorf("The color %s cannot be set to none", key)
		}
		ans[key] = nil
	} else if key == "transparent_background_colors" {
		ans[key] = val
	} else {
		col, err := style.ParseColor(val)
		if err != nil {
			return fmt.Errorf("%s is not a valid color", val)
		}
		ans[key] = col.AsRGB()
	}
	return nil
}

func parse_colors_and_files(args []string) (map[string]any, error) {
	ans := make(map[string]any, len(args))
	for _, arg := range args {
		key, val, found := strings.Cut(strings.ToLower(arg), "=")
		if found {
			err := set_color_in_color_map(key, val, ans, true, false)
			if err != nil {
				return nil, err
			}
		} else {
			path := utils.Expanduser(arg)
			f, err := os.Open(path)
			if err != nil {
				return nil, err
			}
			defer f.Close()
			scanner := bufio.NewScanner(f)
			for scanner.Scan() {
				key, val, found := strings.Cut(scanner.Text(), " ")
				if found {
					set_color_in_color_map(strings.ToLower(key), strings.ToLower(strings.TrimSpace(val)), ans, true, true)
				}
			}
		}
	}
	return ans, nil
}
