package com.szxgl.nft.controller;

import com.szxgl.nft.entity.RedisKeyName;
import com.szxgl.nft.entity.UserDO;
import com.szxgl.nft.exception.ParameterException;
import com.szxgl.nft.service.NftPhotoService;
import com.szxgl.nft.service.UserService;
import com.szxgl.nft.utils.CookieUtil;
import com.szxgl.nft.utils.R;
import com.szxgl.nft.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private NftPhotoService nftPhotoService;

    @Autowired
    private RedisUtil redisUtil;

    private static final String openidCode = RedisKeyName.getOpenidCode();

    /**
     * 用户登录
     * @param code 验证码
     * @return
     */
    @PostMapping("/login")
    public R login(String seed,String code, HttpServletRequest request,Integer flag){
        String openid = CookieUtil.getCookie(request.getCookies());
        if(StringUtils.isBlank(openid)){
            log.error("openid为空："+openid);
            return R.error("openid为空");
       }

        if(StringUtils.isBlank(seed)){
            log.error("手机号不能为空！");
            return R.error("手机号不能为空！");
        }

        if(StringUtils.isBlank(code)){
            log.error("验证码不能为空！");
            return R.error("验证码不能为空！");
        }

        if(!redisUtil.hasKey(openidCode+openid)){
            log.error("redis缓存中的key不存在："+openidCode+openid);
            return R.error("验证码错误或者已过期！");
        }
        String s = redisUtil.get(openidCode + openid).toString();
        if(s.equals(code)){
            if(seed.contains(" ")|| seed.contains("-")){
                seed = seed.replaceAll(" ", "");
                seed = seed.replaceAll("-", "");
                seed = seed.substring(4);
            }
            //用户绑定手机号
            R r = userService.addPhone(seed, openid);
            if("手机号已被用户绑定!".equals(r.get("msg"))){
                return r;
            }
            //用户绑定NFT图片Hash值
            nftPhotoService.addPhonePaintingHash(openid, seed, flag);
            //获取图片类型
            String status = nftPhotoService.getStatus(openid);
            return R.ok(status);
        }else {
            return R.error("验证码错误或者已过期！");
        }
//        Long parseInt = Long.parseLong(seed);
//        for (int i = 0; i < 500; i++) {
//            ++ parseInt;
//            nftPhotoService.addPhonePaintingHash(openid,parseInt+"",flag);
//        }
//        return R.ok();
    }


    /**
     * 是否绑定手机号
     * @param request
     * @return  有值表示已绑定手机号  walletAddr如果有值表示
     */
    @PostMapping("/getPhone")
    public R getPhone(HttpServletRequest request){
        String openid = CookieUtil.getCookie(request.getCookies());
        if(StringUtils.isBlank(openid)){
            log.error("openid为空："+openid);
            return R.error("openid为空");
       }
        Map<String, Object> map = nftPhotoService.getNftPhotoUrl(openid);
        if(!map.get("phone").equals("") ){
            //手机号已绑定
            map.put("flag",true);
            if(map.get("status") != null || map.get("url") !=null ){
                int status = (int)map.get("status");
                String url = (String) map.get("url");
                if(status == 1 ){
                    url = String.format("https://cdn.xglpa.com/pars-nft/directional200/%s.png",url);
                }else if(status == 2){
                    url = String.format("https://cdn.xglpa.com/pars-nft/scene200/%s.png",url);
                }else if(status == 3 ){
                    url = String.format("https://cdn.xglpa.com/pars-nft/nonDirectional500/%s.png",url);
                }
                map.put("url",url);
            }
            return R.ok(map);
        }else {
            //手机号未绑定
            Map<String, Object> map2 = new HashMap<>();
            map2.put("flag",false);
            return R.ok(map2);
        }

    }

}
