package controller

import (
	"context"
	"errors"
	"fonchain-artshow/cmd/model"
	"fonchain-artshow/cmd/service"
	"fonchain-artshow/pb/artShow"
	"fonchain-artshow/pkg/m"
)

type ArtShowProvider struct {
	artShow.UnimplementedArtShowServer
}

func (p *ArtShowProvider) CreateShow(_ context.Context, req *artShow.SaveShowReq) (res *artShow.SaveShowRes, err error) {
	//fmt.Println("artShow create info ======== ", req.ShowSeq)
	if req.ShowName == "" {
		err = errors.New(m.ERROR_SHOW_NAME)
		return nil, err
	}
	if req.CreateTime == "" {
		err = errors.New(m.ERROR_TIME)
		return nil, err
	}
	if req.ShowSeq == "" {
		err = errors.New(m.ERROR_NOT_ARTIST_SEQ)
		return nil, err
	}

	res = new(artShow.SaveShowRes)
	err, showUID := service.CreateArtShowWithArtworkPrice(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(err.Error())
		return res, err
	}
	res.Msg = m.CREATE_SUCCESS
	res.ShowUID = showUID
	return res, nil
}

func (p *ArtShowProvider) UpdateShow(_ context.Context, req *artShow.SaveShowReq) (res *artShow.SaveShowRes, err error) {
	if req.ShowUID == "" {
		err = errors.New(m.ERROR_INVALID_ID)
		return
	}
	res = new(artShow.SaveShowRes)
	if req.IsShow == m.ARTSHOW_INSIDE {
		showRel := new(model.ShowRel)
		err, showRel = service.QueryShowRel_showUID(req.ShowUID)
		if err != nil {
			return res, err
		}
		if showRel.ID > 0 {
			err = errors.New(m.ERROR_NOT_UPDATE_ISSHOW)
			return
		}
	}

	err, showUID := service.UpdateArtShowWithArtworkPrice(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.UPDATE_FAILED)
		return
	}
	res.Msg = m.UPDATE_SUCCESS
	res.ShowUID = showUID
	return
}

func (p *ArtShowProvider) DelShow(_ context.Context, req *artShow.DelShowReq) (res *artShow.CommonRes, err error) {
	res = new(artShow.CommonRes)
	if len(req.ShowUID) == 0 {
		err = errors.New(m.ERROR_INVALID_ID)
		return
	}
	err = service.DelArtShow(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(err.Error())
		return
	}
	res.Msg = m.DELETE_SUCCESS
	return
}

func (p *ArtShowProvider) ShowList(_ context.Context, req *artShow.ShowListReq) (res *artShow.ShowListRes, err error) {
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.Page = 10
	}
	res = new(artShow.ShowListRes)
	err, res = service.ArtShowList(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ShowListWithRel(_ context.Context, req *artShow.ShowListReq) (res *artShow.ShowListRes, err error) {
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.Page = 10
	}
	res = new(artShow.ShowListRes)
	err, res = service.ArtShowListWithRel(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ShowArtworkInfo(_ context.Context, req *artShow.ArtworkDetailReq) (res *artShow.ShowArtworkDetailRes, err error) {
	err, res = service.ShowArtworkInfo(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ShowDetail(_ context.Context, req *artShow.ShowDetailReq) (res *artShow.ShowDetailRes, err error) {
	res = new(artShow.ShowDetailRes)
	err, res = service.ShowDetail(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ShowStatisticalInfo(_ context.Context, req *artShow.ShowStatisticalInfoReq) (res *artShow.ShowStatisticalInfoRes, err error) {
	res = new(artShow.ShowStatisticalInfoRes)
	err, num := service.ShowStatisticalInfo(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_Statistical_NUM)
		return
	}
	res.Data = num
	return
}

func (p *ArtShowProvider) ArtworkPrice(_ context.Context, req *artShow.ArtworkPriceReq) (res *artShow.ArtworkPriceRes, err error) {
	if req.ArtworkUID == "" {
		err = errors.New(m.ERROR_INVALID_ID)
		return
	}
	res = new(artShow.ArtworkPriceRes)
	err, res = service.ArtworkPriceInfo(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ShowListWithApply(_ context.Context, req *artShow.ShowListReq) (res *artShow.ShowListRes, err error) {
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.Page = 10
	}
	if len(req.IsShow) == 0 {
		req.IsShow = append(req.IsShow, 2)
	}
	res = new(artShow.ShowListRes)
	err, res = service.ArtShowListWithApply(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ShowListForArtwork(_ context.Context, req *artShow.ShowListForArtworkReq) (res *artShow.ShowListForArtworkRes, err error) {
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.Page = 10
	}
	res = new(artShow.ShowListForArtworkRes)
	err, res = service.QueryArtShowForArtwork(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ArtworkPriceList(_ context.Context, req *artShow.ShowDetailReq) (res *artShow.ArtworkPriceListRes, err error) {
	if len(req.ShowUID) != 1 && req.ShowUID[len(req.ShowUID)-1] == "" {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	res = new(artShow.ArtworkPriceListRes)
	err, res.Data = service.ArtworkPriceList(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) CancelShow(_ context.Context, req *artShow.CancelShowReq) (res *artShow.CommonRes, err error) {
	res = new(artShow.CommonRes)
	err = service.CancelShow(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_DELETE)
		return
	}
	res.Msg = m.DELETE_SUCCESS
	return
}

func (p *ArtShowProvider) UpdateArtworkReward(_ context.Context, req *artShow.UpdateArtworkRewardReq) (res *artShow.CommonRes, err error) {
	if len(req.Data) < 1 {
		res.Msg = m.ERROR_INVALID_PARAM
		err = errors.New(m.ERROR_INVALID_PARAM)
		return
	}
	res = new(artShow.CommonRes)
	err = service.UpdateArtworkPriceReward(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.UPDATE_FAILED)
		return
	}
	res.Msg = m.UPDATE_SUCCESS
	return
}

func (p *ArtShowProvider) UpdateArtworkSaleAddress(_ context.Context, req *artShow.UpdateArtworkSaleAddressReq) (res *artShow.CommonRes, err error) {
	if len(req.Data) < 1 {
		res.Msg = m.ERROR_INVALID_PARAM
		err = errors.New(m.ERROR_INVALID_PARAM)
		return
	}
	res = new(artShow.CommonRes)
	err = service.UpdateArtworkPriceSaleAddress(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.UPDATE_FAILED)
		return
	}
	res.Msg = m.UPDATE_SUCCESS
	return
}

func (p *ArtShowProvider) QueryShowStatus(_ context.Context, req *artShow.ShowStatusReq) (res *artShow.ShowStatusRes, err error) {
	if len(req.ShowUID) < 1 {
		err = errors.New(m.ERROR_INVALID_PARAM)
		return
	}
	res = new(artShow.ShowStatusRes)
	err, res = service.QueryShowStatus(req)
	if err != nil {
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}
