package controllers

import (
	"encoding/json"
	"sws_xcx/enums"
	"sws_xcx/models"
	"sws_xcx/service"

	"github.com/medivhzhan/weapp/v3/phonenumber"
)

type UserApiController struct {
	BaseApiAuthController
}

// @Title GetUserInfo
// @Description 获取个人中心信息
// @Success 200 {object} models.UserInfoResp success
// @Failure 500 error
// @router /getuserinfo [get]
func (c *UserApiController) GetUserInfo() {

	resp := models.UserInfoResp{
		Id:                      c.CurrentUser.Id,
		Avatar:                  c.CurrentUser.Avatar,
		Email:                   c.CurrentUser.Email,
		NickName:                c.CurrentUser.NickName,
		Phone:                   c.CurrentUser.Phone,
		PrivacyProtocolVersions: c.CurrentUser.PrivacyProtocolVersions,
		UnionId:                 c.CurrentUser.UnionId,
		OpenId:                  c.CurrentUser.OpenId,
		Status:                  c.CurrentUser.Status,
		Source:                  c.CurrentUser.Source,
		Ctime:                   c.CurrentUser.Ctime,
		Mtime:                   c.CurrentUser.Mtime,
	}

	c.ServeSuccessJSON(resp)
}

// @Title UpdatePhoneByCode
// @Description 获取小程序绑定的手机号码并更新到用户信息
// @Param	body	body 	models.WxXcxLoginReq	true  "小程序登录请求参数"
// @Success 200 {object} models.XcxUser success
// @Failure 500 error
// @router /updatephonebycode [post]
func (c *UserApiController) UpdatePhoneByCode() {
	dataBody := models.WxXcxLoginReq{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody); err != nil || dataBody.Code == "" {

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
		return
	}

	wxcli := service.GetWxSdk().NewPhonenumber()
	resp, err := wxcli.GetPhoneNumber(&phonenumber.GetPhoneNumberRequest{
		Code: dataBody.Code,
	})
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return
	}
	if resp.ErrCode != 0 {
		c.ServeDynamicFailJsonSend(resp.ErrMSG)
		return
	}

	err = c.updateCurrentUser(func(u *models.XcxUser) {
		u.Phone = resp.Data.PhoneNumber
	})

	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return
	}
	c.ServeSuccessJSON(c.CurrentUser)

}

// @Title SaveUserInfo
// @Description 个人中心保存用户信息和透析病友信息
// @Param	body	body 	models.SaveUserInfoReq	true  "小程序登录请求参数"
// @Success 200  success
// @Failure 500 error
// @router /saveuserinfo [post]
func (c *UserApiController) SaveUserInfo() {
	req := &models.SaveUserInfoReq{}

	if err := json.Unmarshal(c.Ctx.Input.RequestBody, req); err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
		return
	}

	err := c.updateCurrentUser(func(u *models.XcxUser) {
		u.Avatar = req.Avatar
		u.Email = req.Email
		u.NickName = req.NickName
		u.Phone = req.Phone
	})

	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return
	}

	err = service.NewUserHealthProfileService().SavePatientInfo(
		c.CurrentUser.Id,
		req.RealName,
		req.IdCard,
		req.InpatientRegPhone)

	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return
	}

	c.ServeSuccessJSON(new(interface{}))

}

// @Title SaveHealthProfile
// @Description 保存健康档案
// @Param	body	body 	models.SaveHealthProfileReq	true  "小程序登录请求参数"
// @Success 200  success
// @Failure 500 error
// @router /savehealthprofile [post]
func (c *UserApiController) SaveHealthProfile() {

	req := &models.SaveHealthProfileReq{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, req); err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
		return
	}
	err := service.NewUserHealthProfileService().SaveHealthProfile(c.CurrentUser.Id, *req)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return
	}
	c.ServeSuccessJSON(new(interface{}))

}

// @Title GetHealthProfile
// @Description 获取健康档案
// @Success 200 {object} models.HealthProfileResp success
// @Failure 500 error
// @router /gethealthprofile [get]
func (c *UserApiController) GetHealthProfile() {
	p, err := service.NewUserHealthProfileService().GetUserHealthProfileByUserId(c.CurrentUser.Id)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return
	}

	resp := models.HealthProfileResp{}
	if p != nil {

		resp.Birthday = p.Birthday
		resp.BloodType = p.BloodType
		resp.CreatineTime = p.CreatineTime
		resp.Creatinine = p.Creatinine
		resp.CreatinineUnit = p.CreatinineUnit
		resp.Ctime = p.Ctime
		resp.Gender = p.Gender
		resp.Height = p.Height
		resp.Id = p.Id
		resp.IllnessState = p.IllnessState
		resp.Mtime = p.Mtime
		resp.RenalFunctionStatus = p.RenalFunctionStatus
		resp.Status = p.Status
		resp.UrineProtein = p.UrineProtein
		resp.UrineProteinTime = p.UrineProteinTime
		resp.UrineProteinUnit = p.UrineProteinUnit
		resp.UrineProtein24h = p.UrineProtein24h
		resp.UrineProtein24hTime = p.UrineProtein24hTime
		resp.UrineProtein24hUnit = p.UrineProtein24hUnit

		resp.Weight = p.Weight

	}

	c.ServeSuccessJSON(resp)
}
