package com.rf.psychological.scale.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.dao.model.*;
import com.rf.psychological.scale.dao.model.ScaleEntity;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.scale.service.ScaleService;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.ExcelUtil;
import com.rf.psychological.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author zzf
 * @description:接收测试结果计算结果--量表测试
 * @date 2021/1/21 16:07
 */
@Slf4j
@RestController
@RequestMapping("/appResult")
@Api(tags = "量表测试结果计算")
public class ScaleResultController extends BaseController {

    @Autowired
    private ScaleService scaleService;

    //@SafetyProcess
    @PostMapping("/{type}")
    @ApiOperation(value = "type:量表标志，jsonObject：测试结果")
    public Result completeTest(@RequestBody JSONObject jsonObject, @PathVariable String type) throws Exception {
        System.out.println("----------------result-------------------");System.out.println(jsonObject);
        String userName = jsonObject.getString("userName");
        String userBirthday=jsonObject.getString("userBirthday");
        String userSex=jsonObject.getString("userSex");
        String testTime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
        if(jsonObject.containsKey("startTime")){
            testTime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(jsonObject.getDate("startTime"));
        }
        String fileName = userName+ Constant.SPLIT_CHAR+userBirthday+Constant.SPLIT_CHAR+userSex+Constant.SPLIT_CHAR+testTime;
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        List<Map<String,String>> resultMapList = null;
        return success("success","完成");
    }

    private static int checkQuality(JSONArray jsonArray){
        //睡眠质量,第六题，很好0分 较好1分  较差2分 很差3分
        int scoreQuality = 0 ;
        JSONObject jsonObject1 = jsonArray.getJSONObject(14);
        String checked = jsonObject1.getString("checked");
        if(checked.equals("(2) 较好")){
            scoreQuality +=1;
        }else if(checked.equals("(3) 较差")){
            scoreQuality +=2;
        }else if(checked.equals("(4) 很差")){
            scoreQuality +=3;
        }
        return scoreQuality;
    }
    private static int checkTime(JSONArray jsonArray){
        //入睡时间 ，
        // 1、第二题 计分为≤ 15 分计 0 分，16～30 分计 1 分，31~60计 2 分，≥60 分计 3 分
        // 2、 5a⽆计 0 分，<1 周/ 次计 1 分，1~2 周/ 次计 2 分，≥3周/ 次计 3 分3.
        // 累加条⽬ 2 和 5a 的计分，若累加分为 0 计 0 分，1~2计 1 分，3~4分 2 ，5~6 计 3 分
        int scoreTime = 0;
        int scoreTime5a=0;
        int scoreTime2=0;
        JSONObject jsonObject1 = jsonArray.getJSONObject(1);
        String minute = jsonObject1.getString("checked");
        if(minute.equals("(1) ≤15min")){
            scoreTime2 =1;
        }else if(minute.equals("(2) 16~30")){
            scoreTime2 =2;
        }else {
            scoreTime2 =3;
        }
        jsonObject1=jsonArray.getJSONObject(4);
        String checked = jsonObject1.getString("checked").trim();
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreTime5a = 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreTime5a = 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreTime5a=3;
        }
        int scoreTemp = scoreTime2+scoreTime5a;
        if(scoreTemp==0){
            scoreTime = 0;
        }else if(scoreTemp>=1 && scoreTemp<= 2 ){
            scoreTime = 1;
        }else if(scoreTemp>=3 && scoreTemp<= 4 ){
            scoreTime = 2;
        }else if(scoreTemp>=5 && scoreTemp<= 6 ){
            scoreTime = 3;
        }
        return scoreTime;
    }

    private int checkHour(JSONArray jsonArray) {

        //睡眠时间 根据条⽬ 4 的应答计分， >7 ⼩时计 0 分，6~7 计 1 分，5~6计 2 分，<5 ⼩时计 3 分。
        int scoreHour = 0;
        JSONObject jsonObject1 = jsonArray.getJSONObject(3);
        int hour =jsonObject1.getIntValue("checkItems");
        if(hour>6&&hour<=7){
            scoreHour = 1;
        }else if (hour>5&&hour<=6){
            scoreHour = 2 ;
        }else if (hour<5){
            scoreHour = 3 ;
        }
        return scoreHour;
    }

    private static int checkEfficiency(JSONArray jsonArray){
//        床上时间 = 条⽬ 3（起床时间） - 条⽬ 1（上床时间）
        String goBad = "2021-01-26 "+jsonArray.getJSONObject(2).getString("checked")+"";
        LocalDateTime beginDate = LocalDateTime.parse(goBad, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        String leaveBad = "2021-01-27 "+jsonArray.getJSONObject(0).getString("checked");
        LocalDateTime engDate = LocalDateTime.parse(leaveBad,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        Duration duration = Duration.between(beginDate,engDate);
        //相差的分钟数
        long hourOnBed = duration.toMinutes();
        String hourSleepString = jsonArray.getJSONObject(3).getString("checked");
        int hourSleep = Integer.parseInt(hourSleepString.substring(0,hourSleepString.indexOf(":")))*60 + Integer.parseInt(hourSleepString.substring(hourSleepString.indexOf(":")+1));
        double efficiencyTemp = hourSleep/hourOnBed;
        if(efficiencyTemp>0.85){
            return 0;
        }else if(efficiencyTemp>=0.75&&efficiencyTemp<=0.84){
            return 1;
        }else if(efficiencyTemp>=0.65&&efficiencyTemp<0.74){
            return 2;
        }else{
            return 3;
        }

    }
    private static  int checkObstruct(JSONArray jsonArray){
        int scoreTemp = 0;
        int score = 0;
        for(int i=5;i<14;i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String checked = jsonObject.getString("checked").trim();
            if(checked.equals("(2) 〈1 次/ 周")){
                scoreTemp += 1;
            }else if(checked.equals("(3)1-2 次/ 周")){
                scoreTemp += 2;
            }else if(checked.equals("(4) ≥ 3 次/ 周")){
                scoreTemp += 3;
            }
            if(scoreTemp==0){
                score = 0;
            }else if(scoreTemp>=1 && scoreTemp<10){
                score = 1;
            }else if(scoreTemp>=10 && scoreTemp<=18){
                score = 2;
            }else if(scoreTemp >=19 && scoreTemp<=27){
                score = 3;
            }
        }
        return score;
    }

    private int checkMedicine(JSONArray jsonArray){
        int scoreMedicine = 0 ;
        JSONObject jsonObject = jsonArray.getJSONObject(16);
        String checked = jsonObject.getString("checked").trim();
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreMedicine = 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreMedicine = 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreMedicine = 3;
        }
        return scoreMedicine;
    }

    private static int checkDay(JSONArray jsonArray){
        JSONObject jsonObject = jsonArray.getJSONObject(15);
        String checked = jsonObject.getString("checked").trim();
        int scoreDay=0;
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreDay += 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreDay += 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreDay += 3;
        }

        jsonObject = jsonArray.getJSONObject(16);
        checked = jsonObject.getString("checked").trim();
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreDay += 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreDay += 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreDay += 3;
        }

        jsonObject = jsonArray.getJSONObject(17);
        checked = jsonObject.getString("checked").trim();
        if(checked.equals("(1) 没有")){
            scoreDay += 0;
        }else if(checked.equals("(2) 偶尔有")){
            scoreDay += 1;
        }else if(checked.equals("(3) 有时有")){
            scoreDay += 2;
        }else if(checked.equals("(4) 经常有")){
            scoreDay += 3;
        }
        if(scoreDay == 0){
            scoreDay = 0;
        }else if(scoreDay == 1 || scoreDay == 2){
            scoreDay = 1;
        }else if(scoreDay == 3 || scoreDay == 4){
            scoreDay = 2;
        }else if(scoreDay == 5 || scoreDay == 6){
            scoreDay = 3;
        }
        return scoreDay;
    }

    /**
     * 根据flag查询检测列表
     * @param flag
     * @return
     */
    @SafetyProcess
    @GetMapping("/{flag}")
    @ApiOperation("根据flag查询检测列表")
    public Result getScaleByFlag(@PathVariable String flag){

        List<ScaleEntity> scaleEntityList = this.scaleService.getScaleByFlag(flag);
        return success(scaleEntityList);
    }
}
