package com.cirle.scientific.controller.miniapp;

import com.cirle.scientific.dao.QkjMemberAppDao;
import com.cirle.scientific.po.QkjMemberApp;
import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.WeixinService;
import com.cirle.scientific.utils.DESUtil;
import com.cirle.scientific.utils.IpUtil;
import com.cirle.scientific.utils.UUID;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/miniapp/login")
@Api("登录相关接口")
@Slf4j
public class LoginController {

    @Autowired
    QkjMemberAppDao qkjMemberAppDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    WeixinService weixinService;

    private static final String phones = "13621173040,13390906112,13390906112,15011407616,18515368686,13911906973,18618427271,13501103018,13611127040,13716327458,15901176537,13520544504,13683131351,18600651891,13693161027,13466380946,18510351280,18518272962,13581972813,13811592600,13699283983,18801077506,18810482487,18101318590,13718237252,18810766386,15652790935,18911615130,13810921141,13581866981,13381003887,18911615067,18310131996,15910690230,13520927906,13520556057,13651115505,15001082965,13810503364,15210016357,13021026498,15810022271,13717987881,18511857639,13241553809,13701331252,13611152105,13810678250," +
            "18501363756,15738268948,13811408269,18518156226,18434795701,18835388433,15210182287,13466380946";


    @RequestMapping(value = "/mobileLogin", method = {RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "m登录")
    public Result mobileLogin(
            @ApiParam(name = "code", value = "code") @RequestParam(name = "code", required = true) String code,
            HttpServletRequest request, HttpServletResponse response
    ) {
        try {
            String phone = weixinService.getPhone(code);
            if (StringUtils.isBlank(phone)) {
                return Result.failed("授权失败手机号为空");
            }
            List<String> phoneList = getPhones(phones);
            if(!phoneList.contains(phone)){
                return Result.success(BizCodeEnum.NO_WHITELIST);
            }
            QkjMemberApp qkjMemberApp = qkjMemberAppDao.findByPhone(phone);
            if(Objects.isNull(qkjMemberApp)){
                QkjMemberApp memberApp = new QkjMemberApp();
                memberApp.setMemMobile(phone);
                memberApp.setMemCard(UUID.getUUID());
                memberApp.setMemStatus(0);
                String memToken = getMemToken(memberApp.getMemCard(), request);
                memberApp.setMemToken(memToken);
                qkjMemberAppDao.save(memberApp);
                qkjMemberApp = memberApp;
            }


            String key = String.format("miniapp-token:%s", qkjMemberApp.getMemToken());
            stringRedisTemplate.opsForValue().set(key, qkjMemberApp.getMemCard(), 2, TimeUnit.DAYS);
            return Result.success(qkjMemberApp.getMemToken());
        } catch (Exception e) {
            log.info("mobileLogin e:{}", e);
        }
        return Result.failed("服务异常");
    }

    private List<String> getPhones(String phones){
        List<String> result = Lists.newArrayList();
        try {
            result = Arrays.stream(phones.split(","))
                    .map(StringUtils::trim)
                    .collect(Collectors.toList());

        }catch (Exception e){
            log.error("getPhones e:{}", e);
        }
        return result;
    }

    public static void main(String[] args) {
        String key = String.format("miniapp-token:%s", "sdaf");
        System.out.println("sdada" + key);
    }

    private String getMemToken(String memCard, HttpServletRequest request) {
        String requestIp = IpUtil.getIpAddr(request);
        DESUtil des = new DESUtil();
        String memToken = "miniapp" + "_" + des.encryptStrToken(requestIp + "," + System.currentTimeMillis() + "," + memCard);
        return memToken;
    }

}
