package auth

import (
	"fmt"
	"github.com/tiptok/gocomm/common"
	"github.com/tiptok/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
	protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/auth"
)

type AuthService struct {
}

func (svr *AuthService) Login(header *protocol.RequestHeader, request *protocolx.LoginRequest) (rsp *protocolx.LoginResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		UserRepository, _     = factory.CreateUserRepository(transactionContext)
	)
	rsp = &protocolx.LoginResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var user *domain.User
	if user, err = UserRepository.FindOne(map[string]interface{}{"phone": request.UserName}); err != nil || user == nil {
		err = protocol.NewCustomMessage(1, "用户不存在!")
		return
	}
	if user.Passwd != request.Password {
		err = protocol.NewCustomMessage(1, "密码有误!")
		return
	}
	token, _ := common.GenerateToken(fmt.Sprintf("%v", user.Id), user.Passwd)
	rsp.Access = map[string]interface{}{
		"accessToken": "Bearer " + token,
		"expiresIn":   domain.TokenExpire,
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AuthService) Logout(header *protocol.RequestHeader, request *protocolx.LogoutRequest) (rsp *protocolx.LogoutResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.LogoutResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	err = transactionContext.CommitTransaction()
	return
}

func (svr *AuthService) Profile(header *protocol.RequestHeader, request *protocolx.ProfileRequest) (rsp *protocolx.ProfileResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.ProfileResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	err = transactionContext.CommitTransaction()
	return
}

func (svr *AuthService) CaptchaInit(header *protocol.RequestHeader, request *protocolx.CaptchaInitRequest) (rsp *protocolx.CaptchaInitResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.CaptchaInitResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	err = transactionContext.CommitTransaction()
	return
}

func (svr *AuthService) ChangePassword(header *protocol.RequestHeader, request *protocolx.ChangePasswordRequest) (rsp *protocolx.ChangePasswordResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.ChangePasswordResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	err = transactionContext.CommitTransaction()
	return
}

func NewAuthService(options map[string]interface{}) *AuthService {
	svr := &AuthService{}
	return svr
}