package book

import (
	"errors"

	"github.com/flipped-aurora/gin-vue-admin/server/model/book"
)

func validateBook(item book.Book) error {
	if item.EraTag != "" && !validBookEraTags[item.EraTag] {
		return errors.New("eraTag不是有效值")
	}
	if item.CompletionStatus != "" && !validBookCompletionStatuses[item.CompletionStatus] {
		return errors.New("completionStatus不是有效值")
	}
	if item.PublishStatus != "" && !validBookPublishStatuses[item.PublishStatus] {
		return errors.New("publishStatus不是有效值")
	}
	if item.HotScore < 0 {
		return errors.New("hotScore不能小于0")
	}
	if item.Rating < 0 || item.Rating > 10 {
		return errors.New("rating必须在0到10之间")
	}
	if item.CommentCount < 0 {
		return errors.New("commentCount不能小于0")
	}
	if item.WordCount < 0 {
		return errors.New("wordCount不能小于0")
	}
	if item.SeriesSort < 0 {
		return errors.New("seriesSort不能小于0")
	}
	return nil
}

var validBookEraTags = map[string]bool{
	book.BookEraTagUnknown:      true,
	book.BookEraTagAncient:      true,
	book.BookEraTagHan:          true,
	book.BookEraTagTang:         true,
	book.BookEraTagSong:         true,
	book.BookEraTagYuan:         true,
	book.BookEraTagMing:         true,
	book.BookEraTagQing:         true,
	book.BookEraTagModern:       true,
	book.BookEraTagContemporary: true,
}

var validBookCompletionStatuses = map[string]bool{
	book.BookCompletionStatusCompleted:   true,
	book.BookCompletionStatusSerializing: true,
}

var validBookPublishStatuses = map[string]bool{
	book.BookPublishStatusDraft:    true,
	book.BookPublishStatusOffShelf: true,
	book.BookPublishStatusOnShelf:  true,
}

var validBookAuthorStatuses = map[string]bool{
	book.BookAuthorStatusEnabled:  true,
	book.BookAuthorStatusDisabled: true,
}

var validBookCommentStatuses = map[string]bool{
	book.BookCommentStatusNormal: true,
	book.BookCommentStatusHidden: true,
}

func validateBookAuthor(item book.BookAuthor) error {
	if item.AuthorStatus != "" && !validBookAuthorStatuses[item.AuthorStatus] {
		return errors.New("authorStatus不是有效值")
	}
	return nil
}

func validateBookChapter(item book.BookChapter) error {
	if item.ChapterNo <= 0 {
		return errors.New("chapterNo必须大于0")
	}
	if item.TotalLines < 0 {
		return errors.New("totalLines不能小于0")
	}
	if item.IsReadable && item.TotalLines <= 0 {
		return errors.New("totalLines必须大于0后才能开放阅读")
	}
	return nil
}

func validateBookAuthorRelation(item book.BookAuthorRelation) error {
	if item.AuthorSort <= 0 {
		return errors.New("authorSort必须大于0")
	}
	return nil
}

func applyBookAuthorRelationDefaults(item book.BookAuthorRelation) book.BookAuthorRelation {
	if item.AuthorSort == 0 {
		item.AuthorSort = 1
	}
	return item
}

func validateBookComment(item book.BookComment) error {
	if item.CommentStatus != "" && !validBookCommentStatuses[item.CommentStatus] {
		return errors.New("commentStatus不是有效值")
	}
	if item.LineIndex < 0 {
		return errors.New("lineIndex不能小于0")
	}
	if item.ChapterID == 0 && item.LineIndex != 0 {
		return errors.New("lineIndex在整书评论时必须为0")
	}
	if item.LikeCount < 0 {
		return errors.New("likeCount不能小于0")
	}
	return nil
}

func validateBookReadRecord(item book.BookReadRecord) error {
	if item.ReadProgress < 0 || item.ReadProgress > 100 {
		return errors.New("readProgress必须在0到100之间")
	}
	if item.ChapterID == 0 {
		return errors.New("chapterId必须大于0")
	}
	if item.LineIndex <= 0 {
		return errors.New("lineIndex必须大于0")
	}
	return nil
}
