const router = require('koa-router')();

const funUtil = require("../biz/funUtil");
const fs = require("fs");



router.prefix('/handy/fun');

/**
 * 娱乐，花边福利
 */
router.get('/beauty',async (ctx, next) => {

    let _query = ctx.query;
    if(!_query || !_query.page || !_query.type){
        ctx.body =  [];
        console.error("banner查询参数不足");
        return;
    }
    
    let _page = _query.page;
    let _type = _query.type;
    if(_type != 35){
        ctx.body =  [];
        next();
        return;
    }
    let _num = _query.num || 20;
    let _res = await funUtil.getHbfl(_page,_num,_type);
    
    console.log("花边福利，返回数据为：",_res);

    ctx.body = _res || [];

    next();

});

/**
 * 查询疾病
 */
router.get('/ill',async (ctx, next) => {

    let _query = ctx.query;
    if(!_query || !_query.ill){
        ctx.body =  [];
        console.error("常见疾病查询参数不足");
        next();
        return;
    }
    let key = _query.ill;
    let _res = await funUtil.getCommonIll(key);
    console.log("常见疾病查询，返回数据为：",_res);

    ctx.body = _res || [];

    next();

});

/**
 * 唐诗宋词，
 * 1.随机的，用于生成图片内容题材，并把内容存入到数据库中
 * 2.关键字搜索的，用于交互
 */
router.get('/pom',async (ctx, next) => {

    let _query = ctx.query;
    if(!_query || !_query.keyword){
        ctx.body =  [];
        console.error("关键词，查找唐诗宋词，不完整");
        next();
        return;
    }
    let keyWord = _query.keyword;
    let _res = await funUtil.getPomByKeyWord(keyWord);
    console.log("关键词，查找唐诗宋词，返回数据为：",_res);
    ctx.body = _res || [];
    next();

});
router.get('/getPom',async (ctx, next) => {

    let _query = ctx.query;
    if(!_query || !_query.pomId){
        ctx.body =  [];
        console.error("id查找唐诗宋词，不完整");
        next();
        return;
    }
    let pomId = _query.pomId;
    let _res = await funUtil.getPomById(pomId);
    console.log("id查找唐诗宋词，返回数据：",_res);
    ctx.body = _res || [];
    next();

});
/**
 * 获取名人名言
 * 1.关键字获取，存入数据库中，作为faq内容，后面图片内容题材
 * 2.随机的，没有合适内容时候，随机返回用，根据
 * 3.通过参数不同，请求不同的url
 */
router.get('/famous',async (ctx, next) => {

    let _query = ctx.query;
    if(!_query || !_query.reqType){
        ctx.body =  [];
        console.error("名人名言，请求类型不明...");
        next();
        return;
    }
    let reqType = _query.reqType;
    if(reqType == 1){ //请求关键字搜索
         let word = _query.keyword;
         if(!word){
             ctx.body = [];
         }else{
            let _res = await funUtil.getKeyFamous(word);
            console.log("名人名言，根据关键词，返回数据为：",_res);
            ctx.body = _res || [];
         }
    }else if(reqType == 2){//随机返回一条
         let _res = await funUtil.getRandomFamous();
         console.log("名人名言，随机一条，返回数据为：",_res);
         ctx.body = _res || [];
    }else{
        ctx.body = [];
        console.log("名人名言，请求类型，不支持...");
    }

    next();
});

/**
 * 请求查询星座配对
 */
router.get('/starMatch',async (ctx, next) => {
    let _query = ctx.query;
    if(!_query || !_query.xingzuo1 || !_query.xingzuo2){
        ctx.body =  [];
        console.error("星座匹配，参数空...");
        next();
        return;
    }
    let xingzuo1 = _query.xingzuo1;
    let xingzuo2 = _query.xingzuo2;

    let res = await funUtil.getStarMatch(xingzuo1,xingzuo2);
    console.log("星座匹配，返回结果：",res);
    ctx.body = res || [];
    next();
});

/**
 * 查询电影票房
 * 今天截止日，前三天票房情况，还有概览标签，需要用户点击才有数据，分期上
 * reqType:1 ~ 5 
 * 4为当天，
 * 5 为概览
 * 1- 3 为前三天
 */
router.get('/film',async (ctx, next) => {
    let _query = ctx.query;
    reqType = _query.reqType || 4;
    if((reqType < 1 || reqType > 5)){ //请求类型
        ctx.body =  [];
        console.error("查询电影票房，参数空...");
        next();
        return;
    }
    let res = await funUtil.queryFilm(reqType);
    ctx.body = res || [];
    next();
});

/**
 * 查询笑话
 */
router.get('/joke',async (ctx, next) => {
    let _query = ctx.query;
    if(!_query || !_query.reqType || (_query.reqType < 1 || _query.reqType > 3) || !_query.pageIndex){ //请求类型
        ctx.body =  [];
        console.error("查询笑话，参数空...");
        next();
        return;
    }
    let res = await funUtil.queryJoke(_query.reqType,_query.pageIndex);
    ctx.body = res || [];
    next();
});

/**
 * 周公解梦
 */
router.get('/dream',async (ctx, next) => {
    let _query = ctx.query;
    if(!_query || !_query.keyword){ //请求类型
        ctx.body =  [];
        console.error("查询周公解梦，参数空...");
        next();
        return;
    }
    let res = await funUtil.queryDream(_query.keyword);
    ctx.body = res || [];
    next();
});

/**
 * 星座运势
 */
router.get('/startLuck',async (ctx, next) => {
    let _query = ctx.query;
    if(!_query || !_query.constellation || !_query.reqType || (_query.reqType < 1 || _query.reqType > 5)){ //请求类型
        ctx.body =  [];
        console.error("查询星座运势，参数空...");
        next();
        return;
    }
    let res = await funUtil.queryConstellation(_query.constellation,_query.reqType);
    ctx.body = res || [];
    next();
});


/**
 * 历史
 */
router.get('/history', async (ctx, next) => {
    let _query = ctx.query;
    if (!_query || !_query.day) { //请求类型
        ctx.body = [];
        console.error("查询历史，day参数空...");
        next();
        return;
    }
    let res = await funUtil.queryHistory(_query.day);
    ctx.body = res || [];
    next();
});
//查询历史详情
router.get('/historyDetail', async (ctx, next) => {
    let _query = ctx.query;
    if (!_query || !_query.eid) { //请求类型
        ctx.body = [];
        console.error("查询历史详情，eid参数空...");
        next();
        return;
    }
    let res = await funUtil.queryHistoryDetail(_query.eid);
    ctx.body = res || [];
    next();
});

//颜值测试
router.post('/yanzhi', async (ctx, next) => {
    let _query = ctx.query;
    let req = ctx.request;
    if (!_query) { //请求类型
        ctx.body = [];
        console.error("进行颜值测试详情，eid参数空...");
        next();
        return;
    }
    //测试颜值之前，先保存颜值图片，之后保存颜值与之对应的查询结果
    var imgData = req.body.imgData;
    if (!imgData) {
        ctx.body = '';
        next();
        console.error("颜值测评，图片内容空");
        return;
    }
    let code = hashCode(imgData);
    try{
            //过滤data:URL
            var base64Data = imgData.replace(/^data:image\/\w+;base64,/, "");
            var dataBuffer = new Buffer(base64Data, 'base64');
            let imgPath = "./uploadImg/"+code+".png";
            fs.writeFile(imgPath, dataBuffer, function (err) {
                if (err) {
                    console.error("图片保存失败",err);
                } else {
                   console.log("保存成功！");
                }
            });
    }catch(e){
        console.log("上传的图片保存失败....",e);
    }
    // let res ;
    let res = await funUtil.queryYanzhi(imgData,code);
    ctx.body = res || [];
    next();
});

 function hashCode(str) {
     var hash = 0;
     if (str.length == 0) return hash;
     for (i = 0; i < str.length; i++) {
         char = str.charCodeAt(i);
         hash = ((hash << 5) - hash) + char;
         hash = hash & hash; // Convert to 32bit integer
     }
     return hash;
 }

module.exports = router;
