package starship

import (
	"Sithego/themer"
	"os"
	"path/filepath"
	"text/template"
)

type Adapter struct {
	OutputDir string
}

type Option func(*Adapter)

func WithOutputDir(dir string) Option {
	return func(a *Adapter) {
		a.OutputDir = dir
	}
}

func New(opt ...Option) *Adapter {
	home, _ := os.UserHomeDir()
	a := &Adapter{
		OutputDir: home + "/.config/",
	}
	for _, o := range opt {
		o(a)
	}
	return a
}

func (a *Adapter) Name() string {
	return "Starship"
}

func (a *Adapter) Generate(t themer.Theme) error {
	templatePath := filepath.Join("themer/adapters/starship", "starship.toml")
	templ, err := template.ParseFiles(templatePath)
	if err != nil {
		return err
	}
	outputPath := filepath.Join(a.OutputDir, "starship.toml")

	file, err := os.Create(outputPath)
	if err != nil {
		return err
	}

	err = templ.Execute(file, struct {
		Theme themer.Theme
	}{
		Theme: t,
	})
	_ = file.Close()
	if err != nil {
		return err
	}
	return nil
}
