// Package logic -----------------------------
// @file      : artistinfo_artwork.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2023/2/24 22:26
// -------------------------------------------
package logic

import (
	"context"
	"errors"
	"github.com/fonchain/fonchain-artistinfo/cmd/internal/dao"
	"github.com/fonchain/fonchain-artistinfo/cmd/model"
	"github.com/fonchain/fonchain-artistinfo/pb/artistInfoArtwork"
	"github.com/fonchain/fonchain-artistinfo/pb/artwork_query"
	"github.com/fonchain/fonchain-artistinfo/pkg/m"
	"github.com/fonchain/fonchain-artistinfo/pkg/service"
	"google.golang.org/protobuf/types/known/emptypb"
	"strings"
)

type IArtistInfoArtwork interface {
	GetArtworkLockDetail(request *artistInfoArtwork.GetArtworkLockDetailRequest) (res *artistInfoArtwork.ArtistLockInfo, err error)
	CreateArtworkLockRecord(req *artistInfoArtwork.CreateArtworkLockRecordReq) (res *artistInfoArtwork.ArtworkCommonNoParams, err error)
	ArtworkLockAction(req *artistInfoArtwork.ArtworkLockActionRequest) (res *artistInfoArtwork.ArtworkCommonNoParams, err error)
	GetArtworkLockRecords(req *artistInfoArtwork.GetArtworkLockRecordsRequest) (res *artistInfoArtwork.ArtworkLockList, err error)
	GetArtworkLockHistoryGroup(request *artistInfoArtwork.GetArtworkLockHistoryRequest) (res *artistInfoArtwork.GetArtworkLockHistoryResponse, err error)
	DeleteArtworkRecord(req *artistInfoArtwork.DeleteArtworkRecordRequest) (res *artistInfoArtwork.ArtworkCommonNoParams, err error)
}

var _ IArtistInfoArtwork = new(ArtistInfoArtworkLogic)

type ArtistInfoArtworkLogic struct{}

func (a ArtistInfoArtworkLogic) GetArtworkLockDetail(request *artistInfoArtwork.GetArtworkLockDetailRequest) (res *artistInfoArtwork.ArtistLockInfo, err error) {
	data, err := dao.GetArtworkLockDetail(request.ArtworkUid)
	if err != nil {
		return
	}
	res = &artistInfoArtwork.ArtistLockInfo{
		ArtistUid:             data.ArtistUid,
		ArtworkUid:            data.ArtworkUid,
		Status:                data.Status,
		LockTime:              data.LockTime,
		CreatedAt:             data.CreatedAt.Unix(),
		UpdatedAt:             data.UpdatedAt.Unix(),
		DeletedAt:             int64(data.DeletedAt),
		BaseAuditStatus:       int32(data.BaseAuditStatus),
		BaseAuditMark:         data.BaseAuditMark,
		BaseAuditMark2:        data.BaseAuditMark2,
		SupplementAuditStatus: int32(data.SupplementAuditStatus),
		SupplementAuditMark:   data.SupplementAuditMark,
		SupplementAuditMark2:  data.SupplementAuditMark2,
		AuditFlowIndex:        int32(data.AuditFlowIndex),
		BaseEditable:          data.BaseEditable(),
		SupplementEditable:    data.SupplementEditable(),
	}
	return
}
func (ArtistInfoArtworkLogic) CreateArtworkLockRecord(req *artistInfoArtwork.CreateArtworkLockRecordReq) (res *artistInfoArtwork.ArtworkCommonNoParams, err error) {
	err = dao.CreateArtworkLockRecord(&model.ArtworkLockRecord{
		ArtistUid:  req.ArtistUid,
		ArtworkUid: req.ArtworkUid,
		Status:     req.Status,
		LockTime:   req.LockTime,
	})
	return
}

func (ArtistInfoArtworkLogic) ArtworkLockAction(req *artistInfoArtwork.ArtworkLockActionRequest) (res *artistInfoArtwork.ArtworkCommonNoParams, err error) {
	switch req.Lock {
	case 2:
		err = dao.BatchLockArtworks(req.ArtistUid, req.LockTime)
	case 3:
		err = dao.BatchUnlockArtworks(req.ArtistUid)
	default:
		err = errors.New("lock值错误")
	}
	return
}

func (ArtistInfoArtworkLogic) GetArtworkLockRecords(req *artistInfoArtwork.GetArtworkLockRecordsRequest) (res *artistInfoArtwork.ArtworkLockList, err error) {
	res = &artistInfoArtwork.ArtworkLockList{}
	res, err = dao.GetArtworkLockRecords(req)
	return
}

func (a ArtistInfoArtworkLogic) GetArtworkLockHistoryGroup(request *artistInfoArtwork.GetArtworkLockHistoryRequest) (res *artistInfoArtwork.GetArtworkLockHistoryResponse, err error) {
	// 查询解锁的画作
	unlockArtworkList, err := dao.GetArtworkLockRecords(&artistInfoArtwork.GetArtworkLockRecordsRequest{
		ArtistUid: request.ArtistUid,
		QueryType: artistInfoArtwork.ArtworkQueryMode_AllUnlockArtwork,
	})
	if err != nil {
		return nil, err
	}
	if len(unlockArtworkList.Data) == 0 {
		return nil, nil
	}
	res = &artistInfoArtwork.GetArtworkLockHistoryResponse{}
	var artworkUidList []string
	for _, v := range unlockArtworkList.Data {
		artworkUidList = append(artworkUidList, v.ArtworkUid)
	}
	//查询画作预览列表
	previewListRes, err := service.ArtworkQueryImpl.ArtworkPreviewList(context.Background(), &artwork_query.ArtworkPreviewListRequest{
		Page:        1,
		PageSize:    -1,
		ArtworkUids: artworkUidList,
	})
	if err != nil {
		return nil, err
	}
	var thisLockTime = ""
	var groupIndex = -1
	for _, v := range unlockArtworkList.Data {
		var newGroup bool
		if thisLockTime != v.LockTime {
			thisLockTime = v.LockTime
			newGroup = true
			groupIndex++
		}
		if newGroup {
			res.GroupList = append(res.GroupList, &artistInfoArtwork.ArtworkLockRecord{
				LockGroup: v.LockTime,
				DataList:  []*artistInfoArtwork.ArtworkPreviewInfo{},
			})
		}
		for _, artwork := range previewListRes.Data {
			res.GroupList[groupIndex].DataList = append(res.GroupList[groupIndex].DataList, &artistInfoArtwork.ArtworkPreviewInfo{
				ArtistUuid:            artwork.ArtistUuid,
				ArtworkName:           artwork.ArtworkName,
				Length:                artwork.Length,
				Width:                 artwork.Width,
				Ruler:                 artwork.Ruler,
				CreatedAddress:        strings.Split(artwork.CreatedAddress, "，"),
				ArtistPhoto:           artwork.ArtistPhoto,
				HdPic:                 artwork.HdPic,
				ArtworkUid:            artwork.ArtworkUid,
				CreatedDate:           artwork.CreateDate,
				LockStatus:            int32(v.Status),
				BaseAuditStatus:       v.BaseAuditStatus,
				BaseAuditMark:         v.BaseAuditMark,
				BaseAuditMark2:        v.BaseAuditMark2,
				SupplementAuditStatus: v.SupplementAuditStatus,
				SupplementAuditMark:   v.SupplementAuditMark,
				SupplementAuditMark2:  v.SupplementAuditMark2,
				AuditFlowIndex:        int32(v.AuditFlowIndex),
				CreatedAt:             v.CreatedAt,
				UpdatedAt:             v.UpdatedAt,
				DeletedAt:             v.DeletedAt,
			})
		}
	}
	return
}

func (ArtistInfoArtworkLogic) DeleteArtworkRecord(req *artistInfoArtwork.DeleteArtworkRecordRequest) (res *artistInfoArtwork.ArtworkCommonNoParams, err error) {
	//检查画作锁定情况
	for _, v := range req.ArtworkUids {
		if dao.HasBeenLocked(v) {
			return nil, errors.New(m.ERROR_ISLOCK)
		}
	}
	err = dao.DeletedArtworkLockRecord(req.ArtworkUids...)
	return
}

func (a ArtistInfoArtworkLogic) UpdateArtworkAuditStatus(request *artistInfoArtwork.UpdateArtworkAuditStatusRequest) (res *artistInfoArtwork.ArtworkCommonNoParams, err error) {
	if request.ArtworkUid != "" {
		err = dao.UpdateAuditStatus(request.ArtworkUid, request.AuditStatus, request.AuditMark, request.AuditMark2, request.FlowIndex)

	} else if request.ArtworkUids != nil && len(request.ArtworkUids) > 0 {
		err = dao.BatchUpdateAuditStatus(request.ArtworkUids, request.AuditStatus, request.AuditMark, request.AuditMark2, request.FlowIndex)
	} else {
		return nil, errors.New("画作uid不能为空")
	}
	return
}

// CheckArtworkEditable 检查画作的可编辑状态
// followIndex   2：基本信息审核 3补充信息审核
func (a ArtistInfoArtworkLogic) CheckArtworkEditable(request *artistInfoArtwork.ArtworkUidRequest, followIndex int) (res *artistInfoArtwork.CheckArtworkEditableResponse, err error) {
	res = &artistInfoArtwork.CheckArtworkEditableResponse{}
	lockDetail, err := dao.GetArtworkLockDetail(request.ArtworkUid)
	if err != nil {
		return nil, err
	}
	if followIndex == 2 {
		res.Editable = lockDetail.BaseEditable()
	} else if followIndex == 3 {
		res.Editable = lockDetail.SupplementEditable()
	}
	return
}
func (a ArtistInfoArtworkLogic) GenerateArtworkSupplementInfo(request *artistInfoArtwork.ArtworkUidsRequest) (*emptypb.Empty, error) {
	//更新画作流程
	err := dao.GenerateArtworkSupplementInfo(request.ArtworkUids)
	return nil, err

}
