package reaper

import (
	"encoding/xml"
	"io"

	"golang.org/x/net/html/charset"
	"gorm.io/gorm"
)

func Parse(filereader io.Reader) *FB2Read {
	// legacy, but normal algo
	bookXML := new(FB2Read)
	decoder := xml.NewDecoder(filereader)
	decoder.CharsetReader = charset.NewReaderLabel

	for t, _ := decoder.Token(); t != nil; t, _ = decoder.Token() {
		if se, ok := t.(xml.StartElement); ok {
			if se.Name.Local == "description" {
				decoder.DecodeElement(&bookXML, &se)
				break
			}
		}
	}
	if bookXML.Title == "" {
		return nil
	}

	return bookXML
}

func RawToFB2(
	reaped FB2Read,
	filename string,
	bookcase *string,
	size uint64,
	hash *string,
) FB2 {
	return FB2{
		SrcFile:      filename,
		Bookcase:     bookcase,
		Title:        reaped.Title,
		Genres:       reaped.Genres,
		Authors:      reaped.Authors,
		HasCover:     reaped.Cover.Id != nil,
		Lang:         reaped.Lang,
		SrcLang:      reaped.SrcLang,
		Translators:  reaped.Translators,
		Sequence:     reaped.Sequence,
		Year:         reaped.Year,
		ISBN:         reaped.ISBN,
		Publisher:    reaped.Publisher,
		Annotation:   &reaped.Annotation.Html,
		SymbolsCount: 0,
		Size:         size,
		Hash:         hash,
	}
}

// omg legacy here ->
func nilCheck(nilString *string) string {
	if nilString == nil {
		return ""
	}
	return *nilString
}

func FB2toDB(tx *gorm.DB, book FB2) uint64 {
	var genres *[]Genre
	//book.Genres = nil
	if book.Genres != nil {
		var genresNN []Genre
		for _, genre := range book.Genres {
			genresNN = append(genresNN, Genre{
				RawTag: genre,
			})
		}
		genres = &genresNN
	}
	var authors []Author
	for _, author := range book.Authors {
		var dbAuthor Author
		tx.FirstOrCreate(&dbAuthor, Author{
			Key:        author.FirstName + nilCheck(author.MiddleName) + nilCheck(author.LastName),
			FirstName:  author.FirstName,
			MiddleName: author.MiddleName,
			LastName:   author.LastName,
		})
		authors = append(authors, dbAuthor)
	}
	var translators []Translator
	if book.Translators != nil {
		for _, translator := range *book.Translators {
			var dbTranslator Translator
			tx.FirstOrCreate(&dbTranslator, Translator{
				Key:        translator.FirstName + nilCheck(translator.MiddleName) + nilCheck(translator.LastName),
				FirstName:  translator.FirstName,
				MiddleName: translator.MiddleName,
				LastName:   translator.LastName,
			})
			translators = append(translators, dbTranslator)
		}
	}
	var sequence *Sequence
	if book.Sequence.Name != nil {
		sequence = &Sequence{
			Name: *book.Sequence.Name,
		}
	}
	var publisher *Publisher
	if book.Publisher != nil {
		publisher = &Publisher{
			Name: *book.Publisher,
		}
	}
	// var filetype schemas.Filetype
	// tx.FirstOrCreate(&filetype, schemas.Filetype{
	// 	Filetype: "fb2",
	// 	Name:     "FB2",
	// })
	//fmt.Println(book.Lang)
	dbBook := Book{
		Title:   book.Title,
		Authors: authors,
		Language: Language{
			Code: book.Lang,
		},
		Genre:        genres,
		Description:  book.Annotation,
		HasCover:     book.HasCover,
		SequenceID:   sequence,
		SequenceBook: book.Sequence.Number,
		IsTranslated: book.Translators != nil,
		Translators:  &translators,
		SrcLanguage: Language{
			Code: book.Lang,
		},
		Year:         book.Year,
		Isbn:         book.ISBN,
		PublisherID:  publisher,
		SymbolsCount: &book.SymbolsCount,
		Size:         int(book.Size),
		Hash:         book.Hash,
		Bookcase:     book.Bookcase,
		Filename:     book.SrcFile,
		Filetype:     "fb2",
	}
	tx.Create(&dbBook)
	return dbBook.ID
}
