package cn.com.capcloud.api.action.myself.system.nosession;

import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.api.action.service.MongoService;
import cn.com.capcloud.api.action.service.NoSessionService;
import cn.com.capcloud.ccui.core.common.json.JackJSON;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.system.user.UserService;
import cn.com.capcloud.core.util.IPUtil;
import cn.com.capcloud.core.util.ReadProperty;
import com.google.code.kaptcha.Producer;
import net.sf.json.JSONObject;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//测试
@Component
public class NoSessionSystemConfigAction {
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private NoSessionService noSessionService;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private MongoService mongoService;
    @Resource
    private ReadProperty readProperty;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private Producer captchaProducer = null;
    private static NoSessionSystemConfigAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.mongoTemplate=this.mongoTemplate;
        action.mongoService=this.mongoService;
        action.jdbcTemplate=this.jdbcTemplate;
        action.noSessionService=this.noSessionService;
        action.khcRedisCache=this.khcRedisCache;
        action.captchaProducer=this.captchaProducer;
    }
    public Map<String,Object> isWhiteListIP(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        String ip= IPUtil.getIpAddress(request);
        map.put("ip",ip);
        map.put("isWhiteListIP","Y");//todo 暂时硬编码写死全部是白名单 未来可以加一个IP黑名单表，在黑名单里的则返回N即可。
        map.put("message","白名单");
        return map;
    }

    public Map<String,Object> getCaptchaImage(String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        String capText = action.captchaProducer.createText();
        System.out.println("capText: " + capText);
        Map map = new HashMap();
        BufferedImage bi = action.captchaProducer.createImage(capText);
        String s = Instant.now().toEpochMilli() + ".jpg";
        String imageFilePath = action.readProperty.getCaptchaFilePath()+s;
        try {
            File imageFile = new File(imageFilePath);
            ImageIO.write(bi, "jpg", imageFile);
            map.put("status","ok");
            String url = s;
            map.put("url",url);
            action.khcRedisCache.getYZMByURL(url,capText);
            map.put("message","成功!");
            return map;
        } catch (IOException e) {
            retJson.put("status","no_code");
            retJson.put("message","请求失败,接口异常,请联系管理员!");
            return new HashMap<>();
        }
    }

    public Map<String,Object> getNoSessionInfo(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        String code=action.apiService.getJsonValueByKey(jsonStr,retJson,"code");
        //因为不登陆就能请求访问的接口部分字段敏感，所以用到什么字段响应对应字段即可
        String sql="select js_file,js_method_name from tbl_data_nosession where status='0' and nosession_code='"+code+"'";
        List<Map<String, Object>> nosessionList=action.jdbcTemplate.queryForList(sql);
        Map<String,Object> map=new HashMap<>();
        if(nosessionList.size()==0){
            retJson.put("status","no_code");
            retJson.put("message","无效的链接，或链接已下线");
            return new HashMap<>();
        }else{
            map.put("data",nosessionList.get(0));
            map.put("message","成功");
            return map;
        }
    }

}
