package nmslib

import (
	"fmt"
	"image/png"
	"io/ioutil"
	"os"
	"strconv"

	gim "github.com/ozankasikci/go-image-merge"
)

/*CreateBanner - Output PNG of Portal Glyphs
Requires 3 vars and returns 0 var, Outputs a PNG file
var portalex string
	Portal Glyph hex string
var savename string
	Output name of PNG file
var opt int
	00 sets horizontal banner in light mode
	10 sets vertical banner in light mode
	01 sets horizontal banner in dark mode
	11 sets vertical banner in dark mode
*/
func CreateBanner(portalhex string, savename string, opt int) {
	var err error
	var GlyphHex [12]int64
	var glyphImg [12]string
	var mode string
	var vert int
	var horz int

	// Setup temp dir
	tempdir, err := ioutil.TempDir("", "nmslib-")
	if err != nil {
		panic(err)
	}
	//defer os.RemoveAll(tempdir)

	// verify len of portalhex
	if len(portalhex) == 12 {
		// get hex value from each digit in given string to an array of int64
		for i := 0; i < len(portalhex); i++ {
			GlyphHex[i], err = strconv.ParseInt(portalhex[i:int(i+1)], 16, 16)
			if err != nil {
				panic(err)
			}
		}

		// set options
		if opt == 00 { // set vertical rendering in light mode
			vert, horz = 12, 1
			mode = "light"
		} else if opt == 01 { // set vertival in dark mode
			vert, horz = 12, 1
			mode = "dark"
		} else if opt == 11 { // set horizontal rendering in dark mode
			vert, horz = 1, 12
			mode = "dark"
		} else { // set horizontal rendering in light mode (classic/default)
			vert, horz = 1, 12
			mode = "light"
		}

		// assign image location of its glyph hex value to an array of strings
		for j := 0; j < len(glyphImg); j++ {
			glyphImg[j] = fmt.Sprintf("assets/glyphs/%s/PORTALSYMBOL.%X.png", mode, GlyphHex[j])
		}
		// pull images need from glyph.go and saved them to ./glyphs/
		for k := 0; k < len(glyphImg); k++ {
			data, err := Asset(glyphImg[k])
			if err != nil {
				panic(err)
			}
			_, err2 := os.Stat(tempdir + "/assets/glyphs/" + mode + "/")
			if os.IsNotExist(err2) {
				errDir := os.MkdirAll(tempdir+"/assets/glyphs/"+mode, 0755)
				if errDir != nil {
					panic(errDir)
				}
			}
			// fmt.Println(tempdir + "/" + glyphImg[k]) // for debugging only
			err = ioutil.WriteFile(tempdir+"/"+glyphImg[k], []byte(data), 0644)

		}

		// load images for processing using github.com/ozankasikci/go-image-merge
		grids := []*gim.Grid{
			{ImageFilePath: tempdir + "/" + glyphImg[0]}, {ImageFilePath: tempdir + "/" + glyphImg[1]},
			{ImageFilePath: tempdir + "/" + glyphImg[2]}, {ImageFilePath: tempdir + "/" + glyphImg[3]},
			{ImageFilePath: tempdir + "/" + glyphImg[4]}, {ImageFilePath: tempdir + "/" + glyphImg[5]},
			{ImageFilePath: tempdir + "/" + glyphImg[6]}, {ImageFilePath: tempdir + "/" + glyphImg[7]},
			{ImageFilePath: tempdir + "/" + glyphImg[8]}, {ImageFilePath: tempdir + "/" + glyphImg[9]},
			{ImageFilePath: tempdir + "/" + glyphImg[10]}, {ImageFilePath: tempdir + "/" + glyphImg[11]},
		}

		// create new image from grids
		rgba, err := gim.New(grids, vert, horz).Merge()
		if err != nil {
			panic(err)
		}

		// save the output to png
		file, err := os.Create(savename)
		err = png.Encode(file, rgba)
		if err != nil {
			fmt.Println(err)
		}
	}
}
