package com.slodon.b2b2c.controller.sso.seller;

import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.JWTParser;
import com.slodon.b2b2c.core.constant.*;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.user.UserAuthority;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.Md5;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.dto.GpUserInfoDto;
import com.slodon.b2b2c.model.member.advich.OneClickLoginModel;
import com.slodon.b2b2c.model.seller.StoreApplyModel;
import com.slodon.b2b2c.model.seller.StoreModel;
import com.slodon.b2b2c.model.seller.VendorModel;
import com.slodon.b2b2c.model.seller.VendorResourcesModel;
import com.slodon.b2b2c.seller.example.StoreApplyExample;
import com.slodon.b2b2c.seller.example.VendorExample;
import com.slodon.b2b2c.seller.example.VendorResourcesExample;
import com.slodon.b2b2c.seller.pojo.Store;
import com.slodon.b2b2c.seller.pojo.StoreApply;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.seller.pojo.VendorResources;
import com.slodon.b2b2c.util.JWTRSA256Util;
import com.slodon.b2b2c.vo.seller.VendorLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author Slodon
 */
@Api(tags = "商户登录")
@RestController
@RequestMapping("v3/sellerLogin/oauth")
public class SellerAuthController {

    @Resource
    private VendorResourcesModel vendorResourcesModel;
    @Resource
    private VendorModel vendorModel;
    @Resource
    private StoreModel storeModel;
    @Resource
    private StoreApplyModel storeApplyModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate<String, Object> objectRedisTemplate;
    @Resource
    private OneClickLoginModel oneClickLoginModel;

    @ApiOperation("登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "verifyCode", value = "图形验证码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "verifyKey", value = "图形验证码key", required = true, paramType = "query"),
            @ApiImplicitParam(name = "refresh_token", paramType = "query")
    })
    @PostMapping("token")
    public JsonResult<VendorLoginVO> doLogin(HttpServletRequest request, String username, String password, String verifyCode, String verifyKey, String refresh_token, Integer ssoType) {
        Vendor vendor;
        if (!StringUtil.isEmpty(refresh_token)) {
            //刷新token
            String vendorId = JWTRSA256Util.validToken(refresh_token);
            vendor = vendorModel.getVendorByVendorId(Long.parseLong(vendorId));
        } else {
            //开关开启需要验证
            if (ssoType == null) {
                String verifyCodeCheck = stringRedisTemplate.opsForValue().get("verification_code_check_is_enable");
                if ("1".equals(verifyCodeCheck)) {
                    String verifyNumber = stringRedisTemplate.opsForValue().get(verifyKey);
                    AssertUtil.isTrue(verifyNumber == null || !verifyNumber.equalsIgnoreCase(verifyCode), "图形验证码不正确");
                }
            }

            // 登录验证
            VendorExample vendorExample = new VendorExample();
            vendorExample.setVendorName(username);
            vendorExample.setIsAllowLoginNotEquals(VendorConst.LOGOUT_ALLOW_LOGIN);
            List<Vendor> vendorList = vendorModel.getVendorList(vendorExample, null);
            if (CollectionUtils.isEmpty(vendorList)) {
                // 检测gp系统用户是否存在
                GpUserInfoDto gpUserInfoDto = oneClickLoginModel.checkGpSystemUserIsExist(request, username, password, "SELLER", null);
                if (gpUserInfoDto == null) {
                    AssertUtil.notEmpty(vendorList, "用户名或密码错误");
                } else {
                    vendorList = vendorModel.getVendorList(vendorExample, null);
                }
            } else {
                GpUserInfoDto gpUserInfoDto = oneClickLoginModel.checkGpSystemUserIsExist(request, username, password, "SELLER", null);
                if (gpUserInfoDto == null) {
                    AssertUtil.notEmpty(vendorList, "用户名或密码错误");
                } else {
                    vendorList = vendorModel.getVendorList(vendorExample, null);
                }
            }
//            AssertUtil.notEmpty(vendorList, "用户名或密码错误");
            vendor = vendorList.get(0);
            if(ssoType != null && ssoType == 1){
                AssertUtil.isTrue(!vendor.getVendorPassword().equals(password), "用户名或密码错误！");
            }else{
                AssertUtil.isTrue(!vendor.getVendorPassword().equals(Md5.getMd5String(password)), "用户名或密码错误！");
            }
        }
        AssertUtil.isTrue(vendor.getIsAllowLogin() != VendorConst.IS_ALLOW_LOGIN, "账号不可用");
        if (vendor.getStoreId() != 0) {
            Store store = storeModel.getStoreByStoreId(vendor.getStoreId());
            vendor.setStore(store);
        }

        //定义一个uuid，用做存储redis的key，并且设置到token中
        String uuid = UUID.randomUUID().toString();
        //生成access_token和refresh_token
        VendorLoginVO vo = new VendorLoginVO();
        vo.setAccess_token(JWTRSA256Util.buildToken(vendor.getVendorId().toString(), uuid, WebConst.WEB_IDENTIFY_SELLER));
        vo.setRefresh_token(JWTRSA256Util.buildRefreshToken(vendor.getVendorId().toString(), uuid));
        vo.setVendorId(vendor.getVendorId());
        vo.setStoreId(vendor.getStoreId());
        vo.setIsStoreAdmin(vendor.getIsStoreAdmin());
        vo.setVendorName(vendor.getVendorName());
        vo.setVendorMobile(vendor.getVendorMobile());
        vo.setStoreUrlBy9710(vendor.getStoreUrlBy9710());
        vo.setStoreUrlBy9810(vendor.getStoreUrlBy9810());

        vendor.setVendorPassword("");
        //构造存储对象
        UserAuthority<Vendor> userAuthority = new UserAuthority<>();
        userAuthority.setT(vendor);

        //修改最后登录时间
        Vendor vendorNew = new Vendor();
        vendorNew.setVendorId(vendor.getVendorId());
        vendorNew.setLatestLoginTime(new Date());
        vendorModel.updateVendor(vendorNew);

        //查询角色权限
        if (vendor.getStoreId() == 0L) {
            //未入驻的角色权限
            VendorResourcesExample vendorResourcesExample = new VendorResourcesExample();
            //4级资源
            vendorResourcesExample.setGrade(VendorConst.RESOURCE_GRADE_4);
            vendorResourcesExample.setPidIn("-1,-2");
            List<VendorResources> vendorResourcesList = vendorResourcesModel.getVendorResourcesList(vendorResourcesExample, null);
            vendorResourcesList.forEach(vendorResources -> {
                userAuthority.addAuthority(vendorResources.getUrl());
            });
        } else {
            //已入驻的角色权限
            VendorResourcesExample vendorResourcesExample = new VendorResourcesExample();
            //4级资源
            vendorResourcesExample.setGrade(VendorConst.RESOURCE_GRADE_4);
            vendorResourcesExample.setRoleId(vendor.getRolesId());
            vendorResourcesExample.setPidNotEquals(-2);
            List<VendorResources> vendorResourcesList = vendorResourcesModel.getVendorResourcesList(vendorResourcesExample, null);
            vendorResourcesList.forEach(vendorResources -> {
                userAuthority.addAuthority(vendorResources.getUrl());
            });
        }
        objectRedisTemplate.opsForHash().put("seller-" + vendor.getVendorId().toString(), uuid, userAuthority);
        //用户信息比access_token多5秒，防止token未过期时提示未授权
        objectRedisTemplate.expire("seller-" + vendor.getVendorId().toString(), ExpireTimeConst.TOKEN_EXPIRE_TIME + 5, TimeUnit.MILLISECONDS);

        //登录调用，查询权限信息，构造二级三级权限资源
        if (vendor.getStoreId() == 0L) {
            //未入驻成功
            StoreApplyExample storeApplyExample = new StoreApplyExample();
            storeApplyExample.setVendorId(vendor.getVendorId());
            List<StoreApply> storeApplyList = storeApplyModel.getStoreApplyList(storeApplyExample, null);
            Integer state;
            if (CollectionUtils.isEmpty(storeApplyList)) {
                state = 0;
            } else {
                state = storeApplyList.get(0).getState();
            }
            vo.setApplyState(state);
            if (state == 1) {
                vo.setMessage("您的入驻申请已提交等待审核");
            } else if (state == 3) {
                vo.setMessage("不符合入驻资格");
                vo.setReason(storeApplyList.get(0).getRefuseReason());
                vo.setRemark(storeApplyList.get(0).getAuditInfo());
            } else if (state == 0) {
                vo.setMessage("未入驻");
            }

            //查询角色权限资源
            VendorResourcesExample example = new VendorResourcesExample();
            example.setFrontPath("/apply");
            List<VendorResources> vendorResourcesList = vendorResourcesModel.getVendorResourceByFrontPath(example);
            vo.setResourceList(vendorResourcesList);
            return SldResponse.failSpecial(vo);
        } else {
            //已有店铺信息
            Store storeDb = storeModel.getStoreByStoreId(vendor.getStoreId());
            if (!storeDb.getState().equals(StoreConst.STORE_STATE_OPEN)) {
                JsonResult<VendorLoginVO> jsonResult = SldResponse.fail("店铺已关闭");
                jsonResult.setState(ResponseConst.STATE_STORE_CLOSE);
                return jsonResult;
            }
            //登录调用，查询权限信息，构造二级三级权限资源
            VendorResourcesExample example = new VendorResourcesExample();
            example.setGrade(VendorConst.RESOURCE_GRADE_2);
            example.setFrontPathNotEquals("/apply");
            example.setRoleId(vendor.getRolesId());
            example.setOrderBy("resources_id asc");
            List<VendorResources> vendorResourcesList = vendorResourcesModel.getVendorResourcesList(example, null);
            vendorResourcesList.forEach(vendorResources -> {
                example.setGrade(VendorConst.RESOURCE_GRADE_3);
                example.setPid(vendorResources.getResourcesId());
                if (storeDb.getIsOwnStore() == StoreConst.IS_OWN_STORE) {
                    example.setFrontPathNotEquals("/store/info");
                }
                List<VendorResources> vendorResourcesList3 = vendorResourcesModel.getVendorResourcesList(example, null);//三级资源
                if (storeDb.getIsOwnStore() == StoreConst.NO_OWN_STORE) {
                    //非自营，全部资源
                    vendorResources.setChildren(vendorResourcesList3);
                } else {
                    //自营，过滤中金账户
                    vendorResourcesList3.forEach(vendorResources3 -> {
                        if (vendorResources.getChildren() == null) {
                            vendorResources.setChildren(new ArrayList<>());
                        }
                        if (!"/bill/CPCN_account".equalsIgnoreCase(vendorResources3.getFrontPath())) {
                            vendorResources.getChildren().add(vendorResources3);
                        }
                    });
                }
            });
            vo.setResourceList(vendorResourcesList);
            return SldResponse.success(vo);
        }
    }

    /**
     * 检测refreshToken是否有效
     *
     * @param refreshToken refreshToken
     * @return refreshToken中存放用户信息的uuid
     */
    private String checkRefreshToken(String refreshToken) {
        AssertUtil.notEmpty(refreshToken, "未登录");
        try {
            //解析 refreshToken
            JWTClaimsSet jwtClaimsSet = JWTParser.parse(refreshToken).getJWTClaimsSet();
            //获取其中的uuid
            String uuid = jwtClaimsSet.getStringClaim("uuid");
            //获取其中的user_id
            String userId = jwtClaimsSet.getStringClaim("user_id");
            //查询redis中是否有用户信息
            Object user = objectRedisTemplate.opsForHash().get(jwtClaimsSet.getStringClaim("webIdentify") + "-" + userId, uuid);
            return user != null ? uuid : null;
        } catch (ParseException e) {
            return null;
        }
    }

    @ApiOperation("用户登出接口")
    @PostMapping("logout")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "refresh_token", value = "刷新token", required = true, paramType = "query")
    })
    public JsonResult<Object> logout(HttpServletRequest request, String refresh_token) throws ParseException {


        //检测refresh_token,获取token中存放用户信息的uuid
        String uuid = this.checkRefreshToken(refresh_token);
        if (uuid == null) {
            //refresh_token已失效，不做处理
            return SldResponse.success("退出成功");
        }
        //解析 refreshToken
        JWTClaimsSet jwtClaimsSet = JWTParser.parse(refresh_token).getJWTClaimsSet();
        //获取其中的user_id
        String userId = jwtClaimsSet.getStringClaim("user_id");
        //清除redis中的用户信息
        objectRedisTemplate.opsForHash().delete(jwtClaimsSet.getStringClaim("webIdentify") + "-" + userId, uuid);

        return SldResponse.success("退出成功");
    }

}
