package service

import (
	"fmt"
	"path/filepath"
	"time"

	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/command"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/dto"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/query"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
)

// 文件服务
type FileService struct {
}

// 创建文件服务
func (fileService *FileService) CreateFile(ctx *domain.Context, createFileCommand *command.CreateFileCommand) (interface{}, error) {
	if err := createFileCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	fileInfo := &domain.FileInfo{
		Name:     domain.FileName(createFileCommand.Name),
		Url:      createFileCommand.Url,
		FileSize: createFileCommand.FileSize,
		Ext:      filepath.Ext(createFileCommand.Name),
	}
	newFile := &domain.File{
		FileType:     domain.SourceFile.ToString(),
		FileInfo:     fileInfo,
		SourceFileId: 0,
		//Operator:     "",
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		Context:   ctx,
	}
	fileRepository, _, _ := factory.FastPgFile(transactionContext, 0)

	// 文件名相同进行替换
	if oldFile, findOldFileErr := fileRepository.FindOne(map[string]interface{}{
		"context":  ctx,
		"fileName": fileInfo.Name,
		"fileType": domain.SourceFile.ToString(),
	}); oldFile != nil && findOldFileErr == nil {
		oldFile.FileInfo = fileInfo
		oldFile.UpdatedAt = time.Now()
		newFile = oldFile
	}

	file, err := fileRepository.Save(newFile)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err = factory.FastLog(transactionContext, domain.CommonLog, file.FileId, &domainService.FileUploadSuccessLog{
		LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.SourceFile.ToString(), domain.FileUpload, ctx),
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// 返回文件服务
func (fileService *FileService) GetFile(ctx *domain.Context, getFileQuery *query.GetFileQuery) (interface{}, error) {
	if err := getFileQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var fileRepository domain.FileRepository
	if value, err := factory.CreateFileRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		fileRepository = value
	}
	response := map[string]interface{}{
		"file": nil,
	}
	options := map[string]interface{}{"context": ctx}
	if getFileQuery.FileId > 0 {
		options["fileId"] = getFileQuery.FileId
	}
	if len(getFileQuery.FileName) > 0 {
		options["fileName"] = domain.FileName(getFileQuery.FileName)
	}
	if len(getFileQuery.FileType) > 0 {
		options["fileType"] = getFileQuery.FileType
	}
	// 未传递参数
	if len(options) == 1 {
		return response, nil
	}
	file, _ := fileRepository.FindOne(options)
	//if err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	response["file"] = (&dto.FileDto{}).Load(file)
	return response, nil
}

// 返回文件服务列表
func (fileService *FileService) ListFile(listFileQuery *query.ListFileQuery) (interface{}, error) {
	if err := listFileQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var fileRepository domain.FileRepository
	if value, err := factory.CreateFileRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		fileRepository = value
	}
	if count, files, err := fileRepository.Find(tool_funs.SimpleStructToMap(listFileQuery)); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return map[string]interface{}{
			"count": count,
			"files": files,
		}, nil
	}
}

// 返回文件服务列表
func (fileService *FileService) SearchFile(listFileQuery *query.SearchFileQuery) (interface{}, error) {
	if err := listFileQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var fileRepository domain.FileRepository
	if value, err := factory.CreateFileRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		fileRepository = value
	}
	count, files, err := fileRepository.Find(utils.ObjectToMap(listFileQuery))
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var fileDtos = make([]*dto.FileDto, 0)
	for _, file := range files {
		var item = &dto.FileDto{}
		item.Load(file)
		fileDtos = append(fileDtos, item)
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{
		"count": count,
		"files": fileDtos,
	}, nil
}

// 移除文件服务
func (fileService *FileService) RemoveFile(ctx *domain.Context, removeFileCommand *command.RemoveFileCommand) (interface{}, error) {
	if err := removeFileCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var fileRepository domain.FileRepository
	if value, err := factory.CreateFileRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		fileRepository = value
	}
	file, err := fileRepository.FindOne(map[string]interface{}{"fileId": removeFileCommand.FileId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if file == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%d", removeFileCommand.FileId))
	}
	deleteFileService, _ := factory.CreateDeleteFileService(transactionContext)
	err = deleteFileService.DeleteFiles(ctx, file)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// 更新文件服务
func (fileService *FileService) UpdateFile(updateFileCommand *command.UpdateFileCommand) (interface{}, error) {
	if err := updateFileCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var fileRepository domain.FileRepository
	if value, err := factory.CreateFileRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		fileRepository = value
	}
	file, err := fileRepository.FindOne(map[string]interface{}{"fileId": updateFileCommand.FileId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if file == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%d", updateFileCommand.FileId))
	}
	if err := file.Update(tool_funs.SimpleStructToMap(updateFileCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if file, err := fileRepository.Save(file); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return file, nil
	}
}

// 取消校验中的文件
func (fileService *FileService) CancelVerifyingFile(ctx *domain.Context, cmd *command.CancelVerifyingFileCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	_, file, err := factory.FastPgFile(transactionContext, cmd.FileId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if file.FileType != domain.TemporaryFile.ToString() {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "校验中的文件才允许取消")
	}

	deleteFileService, _ := factory.CreateDeleteFileService(transactionContext)
	err = deleteFileService.DeleteFiles(nil, file)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	byteCore, _ := factory.CreateByteCoreService(transactionContext)
	_, err = byteCore.CancelFile(domain.ReqCancelFile{})
	if err != nil {
		return nil, factory.FastError(err)
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// CheckFileVerifyStatus 检查文件校验状态
func (fileService *FileService) CheckFileVerifyStatus(ctx *domain.Context, cmd *command.CheckFileVerifyStatusCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	fileRepository, file, err := factory.FastPgFile(transactionContext, cmd.FileId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var res = struct {
		ExistVerifyFile bool `json:"existVerifyFile"`
	}{}
	_, verifyFiles, findErr := fileRepository.Find(map[string]interface{}{"context": ctx, "fileType": domain.VerifiedFile.ToString(), "sourceFileId": file.SourceFileId})
	if findErr == nil && len(verifyFiles) > 0 {
		res.ExistVerifyFile = true
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return res, nil
}

func NewFileService(options map[string]interface{}) *FileService {
	newFileService := &FileService{}
	return newFileService
}