package drawio

import (
	"io"
	"os"
	"reflect"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestOptions(t *testing.T) {
	testData := []struct {
		name   string   // Наименование теста
		opts   []Option // Параметры
		app    string   // Путь к приложению drawio
		outdir string   // Путь к папке с экспортированными файлами
		outext string   // Расширение экспортированных файлов
		args   []string // Аргументы командной строки drawio
	}{
		{
			name:   "default",
			opts:   []Option{},
			app:    "drawio",
			outdir: "export",
			outext: ".pdf",
			args:   []string{},
		},
		{
			name: "svg with all options",
			opts: []Option{
				WithAppPath("/usr/local/bin/drawio"),
				WithOutput("/tmp/images/"),
				WithFormat(SVG),
				WithRecursive(),
				WithRemovePageSuffix(),
				WithQuality(100),
				WithTransparent(),
				WithEmbedDiagram(),
				WithEmbedSvgImages(),
				WithBorder(1),
				WithScale(120),
				WithWidth(800),
				WithHeight(600),
				WithCrop(),
				WithUncompressed(),
				WithEnablePlugins(),
			},
			app:    "/usr/local/bin/drawio",
			outdir: "/tmp/images/",
			outext: ".svg",
			args: []string{
				"--format", "svg",
				"--quality", "100",
				"--transparent",
				"--embed-diagram",
				"--embed-svg-images",
				"--border", "1",
				"--scale", "120",
				"--width", "800",
				"--height", "600",
				"--crop",
				"--uncompressed",
				"--enable-plugins",
			},
		},
		{
			name: "png with xvfb",
			opts: []Option{
				WithAppPath("/usr/local/bin/drawio"),
				WithXvfb(),
				WithOutput("/tmp/images/"),
				WithFormat(PNG),
				WithRecursive(),
				WithRemovePageSuffix(),
				WithQuality(100),
				WithTransparent(),
				WithEmbedDiagram(),
				WithEmbedSvgImages(),
				WithBorder(1),
				WithScale(120),
				WithWidth(800),
				WithHeight(600),
				WithCrop(),
				WithUncompressed(),
				WithEnablePlugins(),
			},
			app:    "xvfb-run",
			outdir: "/tmp/images/",
			outext: ".png",
			args: []string{
				"-a", "-l", "/usr/local/bin/drawio",
				"--format", "png",
				"--quality", "100",
				"--transparent",
				"--embed-diagram",
				"--embed-svg-images",
				"--border", "1",
				"--scale", "120",
				"--width", "800",
				"--height", "600",
				"--crop",
				"--uncompressed",
				"--enable-plugins",
			},
		},
	}
	for _, test := range testData {
		t.Run(test.name, func(t *testing.T) {
			options := Options{}
			for _, opt := range test.opts {
				opt.apply(&options)
			}
			assert.Equal(t, test.app, options.App())
			assert.Equal(t, test.outdir, options.OutDir())
			assert.Equal(t, test.outext, options.OutExt())
			assert.ElementsMatch(t, test.args, options.Args())
		})
	}
}

// Тестовая функция открытия файла
func openFile(name string) (io.ReadCloser, error) {
	return nil, nil
}

func TestOptionsOpenFileFunc(t *testing.T) {
	opts := New(WithOpenFile(openFile))
	assert.Equal(t,
		reflect.ValueOf(openFile).Pointer(),
		reflect.ValueOf(opts.openFile).Pointer(),
	)
}

func readDir(name string) ([]os.DirEntry, error) {
	return []os.DirEntry{}, nil
}

func TestOptionsReadDirFunc(t *testing.T) {
	opts := New(WithReadDir(readDir))
	assert.Equal(t,
		reflect.ValueOf(readDir).Pointer(),
		reflect.ValueOf(opts.readDir).Pointer(),
	)
}
