package cluster

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strings"

	fs "github.com/hack-pad/hackpadfs"
)

// OpenFile opens a file on the cluster's config directory with the specified flags
func (m *Cluster) OpenFile(name string, flags int, args ...any) (fs.File, error) {
	if len(args) > 0 {
		name = fmt.Sprintf(name, args...)
	}

	return fs.OpenFile(m.dir, name, flags, 0644)
}

// CreateTruncFile creates or truncates a file on the cluster's config directory
func (m *Cluster) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
	return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
}

// CreateFile creates a file on the cluster's config directory
func (m *Cluster) CreateFile(name string, args ...any) (io.WriteCloser, error) {
	return m.openWriter(name, os.O_CREATE, args...)
}

func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
	f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
	if err != nil {
		return nil, err
	}

	if f, ok := f.(io.WriteCloser); ok {
		return f, nil
	}

	panic("unreachable")
}

// RemoveFile deletes a file from the cluster's config directory
func (m *Cluster) RemoveFile(name string, args ...any) error {
	if len(args) > 0 {
		name = fmt.Sprintf(name, args...)
	}

	err := fs.Remove(m.dir, name)
	switch {
	case os.IsNotExist(err):
		return nil
	default:
		return err
	}
}

// ReadFile reads a file from the cluster's config directory
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
	if len(args) > 0 {
		name = fmt.Sprintf(name, args...)
	}

	return fs.ReadFile(m.dir, name)
}

// ReadLines reads a file from the cluster's config directory,
// split by lines, trimmed, and accepting `#` to comment lines out.
func (m *Cluster) ReadLines(name string, args ...any) ([]string, error) {
	var out []string

	data, err := m.ReadFile(name, args...)
	if err != nil {
		return nil, err
	}

	sc := bufio.NewScanner(bytes.NewReader(data))
	for sc.Scan() {
		s := strings.TrimSpace(sc.Text())
		switch {
		case s == "", strings.HasPrefix(s, "#"):
			// ignore
		default:
			// accepted
			out = append(out, s)
		}
	}

	return out, nil
}

// WriteStringFile writes the given content to a file on the machine's config directory
func (m *Cluster) WriteStringFile(value string, name string, args ...any) error {
	f, err := m.CreateTruncFile(name, args...)
	if err != nil {
		return err
	}
	defer f.Close()

	buf := bytes.NewBufferString(value)
	_, err = buf.WriteTo(f)
	return err
}

// MkdirAll creates directories relative to the cluster's config directory
func (m *Cluster) MkdirAll(name string, args ...any) error {
	if len(args) > 0 {
		name = fmt.Sprintf(name, args...)
	}

	return fs.MkdirAll(m.dir, name, 0755)
}