package nmslib

import (
	"encoding/csv"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

/*
Translate words
*/
type Translate struct {
	Word string
}

// trans - set up structure of a word iteration
type trans0 struct {
	english string // english word
	word    string // no caps word
	capword string // Capitalized word
	acpword string // ALL CAPITALIZED WORD
}

/*
Kor2Eng translate korvax word to english word
*/
func (t Translate) Kor2Eng() (returnstring string) {
	csvlines := openCSV("korvax-lang.csv")
	for range csvlines {
		returnstring = (toEng(t, csvlines))
	}
	return
}

/*
Eng2Kor translate english word to korvax word
*/
func (t Translate) Eng2Kor() (returnstring string) {
	csvlines := openCSV("korvax-lang.csv")
	for range csvlines {
		returnstring = (toNMS(t, csvlines))
	}
	return
}

/*
Gek2Eng translate Gek word to english word
*/
func (t Translate) Gek2Eng() (returnstring string) {
	csvlines := openCSV("gek-lang.csv")
	for range csvlines {
		returnstring = (toEng(t, csvlines))
	}
	return
}

/*
Eng2Gek translate english word to Gek word
*/
func (t Translate) Eng2Gek() (returnstring string) {
	csvlines := openCSV("gek-lang.csv")
	for range csvlines {
		returnstring = (toNMS(t, csvlines))
	}
	return
}

/*
Vyk2Eng translate vykeen word to english word
*/
func (t Translate) Vyk2Eng() (returnstring string) {
	csvlines := openCSV("vykeen-lang.csv")
	for range csvlines {
		returnstring = (toEng(t, csvlines))
	}
	return
}

/*
Eng2Vyk translate english word to vykeen word
*/
func (t Translate) Eng2Vyk() (returnstring string) {
	csvlines := openCSV("vykeen-lang.csv")
	for range csvlines {
		returnstring = (toNMS(t, csvlines))
	}
	return
}

/*
Atl2Eng translate Atlas word to english word
*/
func (t Translate) Atl2Eng() (returnstring string) {
	csvlines := openCSV("atlas-lang.csv")
	for range csvlines {
		returnstring = (toEng(t, csvlines))
	}
	return
}

/*
Eng2Atl translate english word to Atlas word
*/
func (t Translate) Eng2Atl() (returnstring string) {
	csvlines := openCSV("atlas-lang.csv")
	for range csvlines {
		returnstring = (toNMS(t, csvlines))
	}
	return
}

func toEng(t Translate, csvlines [][]string) string {
	returnstring := "*\\Kzzzzzzt\\*" // word not found default
	for _, line := range csvlines {
		lang := trans0{
			english: strings.ToLower(line[0]),
			word:    strings.ToLower(line[1]),
			capword: strings.ToLower(line[2]),
			acpword: strings.ToLower(line[3]),
		}
		// check translate struct for value
		if strings.ToLower(t.Word) == lang.word {
			returnstring = lang.english
		} else if strings.ToLower(t.Word) == lang.capword {
			returnstring = strings.Title(strings.ToLower(lang.english))
		} else if strings.ToLower(t.Word) == lang.acpword {
			returnstring = strings.ToUpper(lang.english)
		}
	}
	return returnstring
}

func toNMS(t Translate, csvlines [][]string) string {
	returnstring := "*\\Kzzzzzzt\\*" // word not found default
	for _, line := range csvlines {
		lang := trans0{
			english: line[0],
			word:    line[1],
			capword: line[2],
			acpword: line[3],
		}
		if t.Word == lang.english {
			if lang.word != "" {
				returnstring = lang.word
			}
		}
		if t.Word == strings.Title(strings.ToLower(lang.english)) {
			if lang.capword != "" {
				returnstring = strings.Title(strings.ToLower(lang.capword))
			}
		}
		if t.Word == strings.ToUpper(lang.english) {
			if lang.acpword != "" {
				returnstring = strings.ToUpper(lang.acpword)
			}
		}
	}
	return returnstring
}

/*
func openCSV(lagcsv string) [][]string {
	csvFile, err := os.Open("./assets/lang/" + lagcsv)
	if err != nil {
		fmt.Println(err)
	}
	defer csvFile.Close() // close language file when finished

	// read csv file from memory
	csvlines, err := csv.NewReader(csvFile).ReadAll()
	if err != nil {
		fmt.Println(err)
	}
	return csvlines
*/
func openCSV(lagcsv string) [][]string {
	// create temp dir
	tempdir, err := ioutil.TempDir("", "nmslib-")
	if err != nil {
		panic(err)
	}
	defer os.RemoveAll(tempdir) // Clean up temp files

	// extract language file from resources.go
	data, err := Asset("assets/lang/" + lagcsv)
	if err != nil {
		panic(err)
	}

	// wirte extracted data to temp dir
	err = ioutil.WriteFile(tempdir+"/"+lagcsv, []byte(data), 0644)
	csvFile, err := os.Open(tempdir + "/" + lagcsv)
	if err != nil {
		fmt.Println(err)
	}
	defer csvFile.Close() // close language file when finished

	// read csv file from memory
	csvlines, err := csv.NewReader(csvFile).ReadAll()
	if err != nil {
		fmt.Println(err)
	}
	return csvlines

}
