package kr.co.jinwoosi.clfd.clfd.dgns.web;

import kr.co.jinwoosi.clfd.clfd.dgns.model.Dgns;
import kr.co.jinwoosi.clfd.clfd.dgns.model.DgnsSearch;
import kr.co.jinwoosi.clfd.clfd.dgns.service.DgnsAskService;
import kr.co.jinwoosi.clfd.clfd.dgns.service.DgnsListService;
import kr.co.jinwoosi.clfd.clfd.dgns.service.DgnsService;
import kr.co.jinwoosi.clfd.clfd.dgns.vo.DgnsSelection;
import kr.co.jinwoosi.clfd.clfd.site.service.ClfdSiteService;
import kr.co.jinwoosi.clfd.framework.cd.model.CdDtl;
import kr.co.jinwoosi.clfd.framework.cd.service.CdDtlService;
import kr.co.jinwoosi.clfd.framework.security.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DgnsController {
    @Autowired
    private ClfdSiteService clfdSiteService;
    @Autowired
    private DgnsAskService dgnsAskService;
    @Autowired
    private DgnsService dgnsService;
    @Autowired
    private DgnsListService dgnsListService;
    @Autowired
    private CdDtlService cdDtlService;

    @RequestMapping("/clfd/main.do")
    public String diagnosisMainPage(HttpServletRequest request, HttpServletResponse response, Model model) {
        if (clfdSiteService.visitClfdSite(request, response, model) == null) {
            return "";
        }
        return "clfd/main";
    }

    @RequestMapping(value = "/clfd/dgns/survey.do", method = RequestMethod.GET)
    public String dgnsPageGet(HttpServletRequest request, HttpServletResponse response, Model model) {
        if (clfdSiteService.visitClfdSite(request, response, model) == null) {
            return "";
        }
        return "clfd/dgns/survey";
    }

    @RequestMapping("/clfd/dgns/survey.json")
    @ResponseBody
    public HashMap<String, Object> dgnsSurvey() {
        HashMap<String, Object> result = new HashMap<>();
        try {
            result.put("result", "success");
            result.put("dgnsAskList", dgnsAskService.getCashedDgnsAskList(false));
            result.put("dgnsTyCdList", cdDtlService.selectList(new CdDtl("dgnsTyCd")));
        } catch (Exception e) {
            result.put("result", "fail");
            result.put("message", "진단항목을 가져오는 중 에러가 발생하였습니다.");
        }
        return result;
    }

    @RequestMapping(value = "/clfd/dgns/surveyExam.do",method = RequestMethod.GET)
    public String dgnsSurveyExamDoGet() {
        return "/";
    }

    @RequestMapping(value = "/clfd/dgns/surveyExam.do", method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String, Object> dgnsSurveyExamDo(HttpServletRequest request, @RequestBody List<DgnsSelection> dgnsSelectionList) {
        HashMap<String, Object> result = new HashMap<>();
        String userRole= (String) request.getSession().getAttribute("userRole");
        User user = (User) request.getSession().getAttribute("user");

        Dgns dgns = dgnsService.examDgns(dgnsSelectionList);

        if (user != null && userRole.equals("ROLE_USER")) {
            dgnsService.saveDgns(user.getUserId(), dgns);
            dgnsListService.saveDgnsList(user.getUserId(), dgns.getDgnsId(), dgnsSelectionList);
        }

        result.put("result", "success");
        result.put("odScore", dgns.getOdTotalScore());
        result.put("srScore", dgns.getSrTotalScore());
        result.put("pnScore", dgns.getPnTotalScore());
        result.put("wtScore", dgns.getWtTotalScore());
        return result;
    }

    @RequestMapping(value = "/clfd/dgns/list.do")
    public String dgnsList (HttpServletRequest request, HttpServletResponse response, Model model) {
        if (clfdSiteService.visitClfdSite(request, response, model) == null) {
            return "";
        }

        return "clfd/dgns/list";
    }

    @RequestMapping(value = "/clfd/dgns/list.json")
    @ResponseBody
    public HashMap<String, Object> dgnsListJson(HttpServletRequest request, HttpServletResponse response, DgnsSearch dgnsSearch) {
        HashMap<String, Object> result = new HashMap<>();

        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            result.put("result", "fail");
            result.put("message", "사용자를 찾을 수 없습니다.");
            return result;
        }
        dgnsSearch.setUserId(user.getUserId());
        dgnsSearch.setSortOrd("DESC");
        dgnsSearch.setRecSt("Y");
        dgnsSearch.setSort("DGNS_CNT");
        List<Dgns> dgnsList = dgnsService.selectList(dgnsSearch);
        result.put("result", "success");
        result.put("dgnsList", dgnsList);

        return result;
    }

    @RequestMapping(value = "/clfd/dgns/dtl.do")
    public String dgnsDtl(@RequestParam("dgns") String dgnsId, HttpServletRequest request, HttpServletResponse response, Model model) {
        User user = (User)request.getSession().getAttribute("user");

        if (clfdSiteService.visitClfdSite(request, response, model) == null) {
            return "";
        }

        DgnsSearch dgnsSearch = new DgnsSearch();
        dgnsSearch.setUserId(user.getUserId());
        dgnsSearch.setDgnsId(Long.parseLong(dgnsId));
        Dgns dgns = dgnsService.select(dgnsSearch);

        model.addAttribute("dgnsTitle", dgnsService.getDgnsResultTitleText(dgns));
        model.addAttribute("dgnsTime", dgnsService.getDgnsResultTime(dgns));
        model.addAttribute("user", user);
        model.addAttribute("skTy", dgns.getSkTy());
        model.addAttribute("odScore", dgns.getOdTotalScore());
        model.addAttribute("srScore", dgns.getSrTotalScore());
        model.addAttribute("pnScore", dgns.getPnTotalScore());
        model.addAttribute("wtScore", dgns.getWtTotalScore());
        if(dgns.getOdTotalScore() < 11) {
            model.addAttribute("odScorePercent", String.format("%.2f", (dgns.getOdTotalScore()-4)*2/13*50));
        } else {
            model.addAttribute("odScorePercent", String.format("%.2f", 50 + (dgns.getOdTotalScore()-10.5)*2/11*50));
        }
        if(dgns.getSrTotalScore() < 11) {
            model.addAttribute("srScorePercent", String.format("%.2f", (dgns.getSrTotalScore()-4)*2/9*50));
        } else {
            model.addAttribute("srScorePercent", String.format("%.2f", 50 + (dgns.getSrTotalScore()-10.5)*2/27*50));
        }
        if(dgns.getPnTotalScore() < 11) {
            model.addAttribute("pnScorePercent", String.format("%.2f", (dgns.getPnTotalScore()-4)*2/13*50));
        } else {
            model.addAttribute("pnScorePercent", String.format("%.2f", 50 + (dgns.getPnTotalScore()-10.5)*2/11*50));
        }
        if(dgns.getWtTotalScore() < 23) {
            model.addAttribute("wtScorePercent", String.format("%.2f", (dgns.getWtTotalScore()-9)*2/27*50));
        } else {
            model.addAttribute("wtScorePercent", String.format("%.2f", 50 + (dgns.getWtTotalScore()-22.5)*2/27*50));
        }
        model.addAttribute("resultText", dgnsService.getDgnsResultText(user, dgns));
        return "clfd/dgns/dtl";
    }

    @RequestMapping(value = "/clfd/dgns/result.do")
    public String dgnsResult(@RequestParam Map<String, String> params, HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute("user");

        String odScore = params.get("od");
        String srScore = params.get("sr");
        String pnScore = params.get("pn");
        String wtScore = params.get("wt");

        if (odScore == null || srScore == null || pnScore == null || wtScore == null) {
            return "";
        }

        Dgns dgns = dgnsService.examDgnsByScore(odScore, srScore, pnScore, wtScore);

        if (dgns == null || dgns.getSkTy() == null) {
            return "";
        }

        model.addAttribute("dgnsTitle", dgnsService.getDgnsResultTitleText(dgns));
        model.addAttribute("dgnsTime", dgnsService.getDgnsResultTime(dgns));
        model.addAttribute("user", user);
        model.addAttribute("skTy", dgns.getSkTy());
        model.addAttribute("odScore", dgns.getOdTotalScore());
        model.addAttribute("srScore", dgns.getSrTotalScore());
        model.addAttribute("pnScore", dgns.getPnTotalScore());
        model.addAttribute("wtScore", dgns.getWtTotalScore());
        if(dgns.getOdTotalScore() < 11) {
            model.addAttribute("odScorePercent", String.format("%.2f", (dgns.getOdTotalScore()-4)*2/13*50));
        } else {
            model.addAttribute("odScorePercent", String.format("%.2f", 50 + (dgns.getOdTotalScore()-10.5)*2/11*50));
        }
        if(dgns.getSrTotalScore() < 11) {
            model.addAttribute("srScorePercent", String.format("%.2f", (dgns.getSrTotalScore()-4)*2/9*50));
        } else {
            model.addAttribute("srScorePercent", String.format("%.2f", 50 + (dgns.getSrTotalScore()-10.5)*2/27*50));
        }
        if(dgns.getPnTotalScore() < 11) {
            model.addAttribute("pnScorePercent", String.format("%.2f", (dgns.getPnTotalScore()-4)*2/13*50));
        } else {
            model.addAttribute("pnScorePercent", String.format("%.2f", 50 + (dgns.getPnTotalScore()-10.5)*2/11*50));
        }
        if(dgns.getWtTotalScore() < 23) {
            model.addAttribute("wtScorePercent", String.format("%.2f", (dgns.getWtTotalScore()-9)*2/27*50));
        } else {
            model.addAttribute("wtScorePercent", String.format("%.2f", 50 + (dgns.getWtTotalScore()-22.5)*2/27*50));
        }
        model.addAttribute("resultText", dgnsService.getDgnsResultText(user, dgns));

        return "clfd/dgns/dtl";
    }

    @RequestMapping (value="/system/stats/skinType.mng", method=RequestMethod.GET)
    public String skinTypeList(){
        return "system/satis/skinTypeList";
    }

    @RequestMapping (value="/system/stats/skinType.json", method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> skinTypeList(DgnsSearch dgnsSearch){
        Map<String , Object> result = new HashMap<String, Object>();
        try {
            result.put("data", dgnsService.skinTypeStats(dgnsSearch));
            result.put("result", "success");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("result", "fail");
            result.put("message", e.getMessage());
        }
        return result;
    }

}
