package assets

import (
	"embed"
	"gitgud.io/mike/mpv-manager/pkg/locale"
	"io"
	"os"
	"path/filepath"
)

//go:embed mpv.conf
var MPVConfig embed.FS

//go:embed locales.json
var Locales embed.FS

//go:embed create-shortcut.vbs
var CreateShortcutVBS embed.FS

//go:embed mpv-manager.ico
var InstallMPVIco embed.FS

//go:embed icon-128.png
var Icon128PNG embed.FS

//go:embed mpv-manager.png
var InstallMPVPNG embed.FS

//go:embed uninstall.bat
var UninstallBat embed.FS

//go:embed uninstall-linux.sh
var UninstallLinuxSh embed.FS

//go:embed uninstall-macos.sh
var UninstallMacOSSh embed.FS

func ReadMPVConfig() ([]byte, error) {
	return MPVConfig.ReadFile("mpv.conf")
}

func ReadLocales() ([]locale.LocaleEntry, error) {
	data, err := Locales.ReadFile("locales.json")
	if err != nil {
		return nil, err
	}
	return locale.LoadFromJSON(data)
}

func ExtractCreateShortcutVBS(destDir string) error {
	content, err := CreateShortcutVBS.ReadFile("create-shortcut.vbs")
	if err != nil {
		return err
	}

	vbsPath := filepath.Join(destDir, "create-shortcut.vbs")
	if err := os.WriteFile(vbsPath, content, 0644); err != nil {
		return err
	}

	return nil
}

func ExtractInstallMPVIco(destDir string) error {
	content, err := InstallMPVIco.ReadFile("mpv-manager.ico")
	if err != nil {
		return err
	}

	icoPath := filepath.Join(destDir, "mpv-manager.ico")
	if err := os.WriteFile(icoPath, content, 0644); err != nil {
		return err
	}

	return nil
}

func ExtractPNGIcon(destDir string) error {
	content, err := InstallMPVPNG.ReadFile("mpv-manager.png")
	if err != nil {
		return err
	}

	iconPath := filepath.Join(destDir, "mpv-manager.png")
	if err := os.WriteFile(iconPath, content, 0644); err != nil {
		return err
	}

	return nil
}

func ReadPNGIcon() ([]byte, error) {
	return InstallMPVPNG.ReadFile("mpv-manager.png")
}

// ExtractUninstallBat extracts the MPV Manager uninstall batch script
func ExtractUninstallBat(destDir string) error {
	if err := os.MkdirAll(destDir, 0755); err != nil {
		return err
	}

	src, err := UninstallBat.Open("uninstall.bat")
	if err != nil {
		return err
	}
	defer src.Close()

	dstPath := filepath.Join(destDir, "uninstall.bat")
	dst, err := os.Create(dstPath)
	if err != nil {
		return err
	}
	defer dst.Close()

	if _, err := io.Copy(dst, src); err != nil {
		return err
	}

	return nil
}

// ExtractUninstallLinuxSh extracts the Linux uninstall script to the given directory
func ExtractUninstallLinuxSh(destDir string) error {
	if err := os.MkdirAll(destDir, 0755); err != nil {
		return err
	}

	src, err := UninstallLinuxSh.Open("uninstall-linux.sh")
	if err != nil {
		return err
	}
	defer src.Close()

	dstPath := filepath.Join(destDir, "uninstall-linux.sh")
	dst, err := os.Create(dstPath)
	if err != nil {
		return err
	}
	defer dst.Close()

	if _, err := io.Copy(dst, src); err != nil {
		return err
	}

	// Set executable permissions for the script
	return os.Chmod(dstPath, 0755)
}

// ExtractUninstallMacOSSh extracts the macOS uninstall script to the given directory
func ExtractUninstallMacOSSh(destDir string) error {
	if err := os.MkdirAll(destDir, 0755); err != nil {
		return err
	}

	src, err := UninstallMacOSSh.Open("uninstall-macos.sh")
	if err != nil {
		return err
	}
	defer src.Close()

	dstPath := filepath.Join(destDir, "uninstall-macos.sh")
	dst, err := os.Create(dstPath)
	if err != nil {
		return err
	}
	defer dst.Close()

	if _, err := io.Copy(dst, src); err != nil {
		return err
	}

	// Set executable permissions for the script
	return os.Chmod(dstPath, 0755)
}
