package com.cyl.h5.controller;

import com.alibaba.fastjson.JSON;
import com.cyl.h5.pojo.dto.H5LoginDTO;
import com.cyl.h5.pojo.request.RegisterRequest;
import com.cyl.h5.pojo.response.RegisterResponse;
import com.cyl.h5.pojo.response.ValidatePhoneResponse;
import com.cyl.h5.pojo.response.H5LoginResponse;
import com.cyl.h5.service.H5MemberService;
import com.cyl.manager.staff.service.IStaffService;
import com.cyl.manager.ums.domain.Member;
import com.cyl.manager.ums.mapper.MemberMapper;
import com.cyl.manager.ums.pojo.vo.MemberV2VO;
import com.cyl.manager.ums.pojo.vo.MemberVO;
import com.cyl.manager.ums.service.MemberService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginMember;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/h5")
public class H5MemberController {

    @Autowired
    private H5MemberService service;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private IStaffService iStaffservice;

    @Autowired
    private MemberMapper memberMapper;


    //@ApiOperation("会员注册")
    @PostMapping("/register")
    public ResponseEntity<RegisterResponse> register(@RequestBody RegisterRequest request){
        return ResponseEntity.ok(service.register(request));
    }

    //@ApiOperation("注册登录验证码校验手机号")
    @GetMapping("/validate/{phone}")
    public ResponseEntity<ValidatePhoneResponse> validate(@PathVariable String phone){
        return ResponseEntity.ok(service.validate(phone));
    }

    //@ApiOperation("手机号密码登录")
    @PostMapping("/account/login")
    public ResponseEntity<H5LoginResponse> accountLogin(@RequestBody String data){
        return ResponseEntity.ok(service.accountLogin(data));
    }

    @PostMapping("/wechat/login")
    public ResponseEntity<H5LoginResponse> wechatLogin(String data) throws Exception {
        if (StringUtils.isEmpty(data)) {
            return ResponseEntity.ok(null);
        }
        H5LoginDTO params = JSON.parseObject(new String(Base64Utils.decodeFromString(data)), H5LoginDTO.class);
        //登录来源：1
        if (new Integer(1).equals(params.getSourceType())) {
            return ResponseEntity.ok(iStaffservice.wechatLogin(params));
        }
        return ResponseEntity.ok(service.wechatLogin(params));
    }

    //@ApiOperation("sms登录")
    @PostMapping("/sms/login")
    public ResponseEntity<H5LoginResponse> smsLogin(@RequestBody String data){
        return ResponseEntity.ok(service.smsLogin(data));
    }

    //@ApiOperation("获取会员信息")
    @GetMapping("/member/info")
    public ResponseEntity<MemberVO> getMemberInfo(){
        return ResponseEntity.ok(service.getMemberInfo());
    }

    //@ApiOperation("获取会员信息V2")
    @GetMapping("/member/infoV2")
    public ResponseEntity<MemberV2VO> getMemberInfoV2() {
        return ResponseEntity.ok(service.getMemberInfoV2());
    }

    @PostMapping("/member/updateInfoH5")
    public AjaxResult updateInfo(@RequestParam String avatar, @RequestParam String nickname) {

        Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        Member m = memberMapper.selectById(member.getId());

        m.setAvatar(avatar);
        m.setNickname(nickname);

        memberMapper.updateById(m);

        return AjaxResult.success();
    }

    //@ApiOperation("设置会员微信信息")
    @PostMapping("/member/setWechatInfo")
    public void setWechatInfo(@RequestBody String data){
        service.setWechatInfo(data);
    }

    //@ApiOperation("新增会员登录记录")
    @GetMapping("/record/login")
    public void add(HttpServletRequest request) {
        LoginMember loginMember = tokenService.getLoginMember(request);
        if (loginMember != null){
            service.insert(loginMember.getMemberId());
        }
    }
}
