package com.yeejoin.amos.boot.module.tzs.biz.controller;

import com.yeejoin.amos.component.robot.AmosRequestContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

/**
 * @author zjw
 * @date 2020-03-30
 */

@RestController
@RequestMapping(value = "/wechat/api")
@Api(tags = "小程序登录api")
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

//    @Autowired
//    private RedisTemplate<String, String> redisTemplate;
//
//    @Value("${outSystem.user.password}")
//    private String password;
//
//    @Value("${amos.system.user.product}")
//    private String product;
//
//    @Value("${amos.system.user.app-key}")
//    private String appKey;

    @Autowired
    AmosRequestContext amosRequestContext;

//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "通过userId登录",  notes = "查询设备指标")
//    @GetMapping(value = "/{userId}")
//    synchronized  public ReginParams getBindEquipment(@PathVariable("userId") String userId) throws Exception {
//
//        if (ObjectUtils.isEmpty(userId)){
//            throw new Exception("用户信息不存在");
//        }
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
//                .getRequestAttributes()).getRequest();
//        String token = "wxxcxdl";
//        ReginParams reginParams;
//        reginParams = JSON.parseObject(redisTemplate.opsForValue().get(buildKey(userId, token)), ReginParams.class);
//        if(reginParams == null){
//            reginParams = getLogin(userId, token);
//        }else{
//            RequestContext.setProduct(product);
//            if (!TokenOperation.refresh(reginParams.getToken())) {
//                reginParams = getLogin(userId, token);
//            }
//        }
//        return reginParams;
//    }
//
//    private ReginParams getLogin(String userId, String token) throws Exception{
//        ReginParams reginParams = new ReginParams();
//        IdPasswordAuthModel  dPasswordAuthModel = new IdPasswordAuthModel();
//        dPasswordAuthModel.setLoginId(userId);
//        dPasswordAuthModel.setPassword(DesUtil.encode(password, "qaz"));
//        RequestContext.setProduct(product);
//        FeignClientResult feignClientResult = Privilege.authClient.idpassword(dPasswordAuthModel);
//        if (ObjectUtils.isEmpty(feignClientResult.getResult())){
//            throw new Exception("缺失登录信息");
//        }
//        // 调用平台授权接口
//        Map<String ,String > re = (Map<String, String>) feignClientResult.getResult();
//        String amosToken = re.get("token");
//
//        logger.info("应用授权开始======================================"+amosToken);
//        RequestContext.setToken(amosToken);
//        RequestContext.setProduct(product);
//        RequestContext.setAppKey(appKey);
//
//        List<ApplicationModel>  fd = Privilege.applicationClient.queryUserService().getResult();
//
//        logger.info("应用授权结束======================================"+JSON.toJSONString(fd)+amosToken);
//
//        AgencyUserModel userModel  =  (AgencyUserModel) Privilege.agencyUserClient.getme().getResult();
//        CompanyModel companyModel = userModel.getCompanys().get(0);
//
//        reginParams.setToken(amosToken);
//        reginParams.setUserModel(userModel);
//        redisTemplate.opsForValue().set(buildKey(userId, token), JSONObject.toJSONString(reginParams),28, TimeUnit.DAYS);
//        return reginParams;
//    }
//
//
//    private String buildKey(String userId, String token) {
//        return "region_" + userId + "_" + token;
//    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "获取机器人登录token",  notes = "获取机器人登录token")
    @GetMapping(value = "/acquireToken")
    synchronized  public ResponseModel<String> getRobotLoginToken() {
        return ResponseHelper.buildResponse(amosRequestContext.getToken());
    }
}
