package com.mobvista.apptag.controller;

import java.io.IOException;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.mobvista.apptag.config.WebSecurityConfig;
import com.mobvista.apptag.entity.Feat;
import com.mobvista.apptag.entity.Tag;
import com.mobvista.apptag.entity.TagResult;
import com.mobvista.apptag.mapper.FeatDao;
import com.mobvista.apptag.mapper.ResultDao;
import com.mobvista.apptag.mapper.TagDao;
import com.mobvista.apptag.service.ResultService;
import com.mobvista.apptag.utils.PageUtil;
import com.mobvista.apptag.utils.Query;
import com.mobvista.apptag.utils.R;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author wangjf
 */
@Controller
@RequestMapping("/result")
public class ResultController {

    @Autowired
    private ResultService resultService;
    @Autowired
    private ResultDao resultDao;
    // @Autowired
    // private UserDao userDao;
    @Autowired
    private TagDao tagDao;
    @Autowired
    private FeatDao featDao;

    public static final Logger logger = LoggerFactory.getLogger(ResultController.class);

    @GetMapping("/edit")
    String edit(@RequestParam(name = "packageName") String packageName, Model model) {
        Tag tag = tagDao.find(packageName);
        model.addAttribute("tag", tag);
        TagResult result = resultDao.find(packageName);
        model.addAttribute("resultCategory", JSONObject.fromObject(result.getFeatId()));
        List<Feat> categoryFeat = featDao.firstList("01");
        JSONArray jsonArray = JSONArray.fromObject(categoryFeat);
        model.addAttribute("firstCategory", jsonArray);
        List<Feat> secondCategoryFeat = featDao.list("01");
        JSONArray secondJsonArray = JSONArray.fromObject(secondCategoryFeat);
        model.addAttribute("secondCategory", secondJsonArray);
        List<Feat> ruleFeat = featDao.firstList("02");
        model.addAttribute("ruleCategory", JSONArray.fromObject(ruleFeat));
        List<Feat> styleFeat = featDao.firstList("03");
        model.addAttribute("styleCategory", JSONArray.fromObject(styleFeat));
        List<Feat> themeFeat = featDao.firstList("04");
        model.addAttribute("themeCategory", JSONArray.fromObject(themeFeat));
        return "result/edit";
    }

    @PostMapping("/save")
    R save(@SessionAttribute(WebSecurityConfig.SESSION_KEY) String userName,
            @RequestParam(name = "package_name") String packageName) {
        if (resultService.save(packageName, userName, "")) {
            return R.ok();
        } else {
            return R.error(1, "保存失败！");
        }
    }

    @PostMapping("/list")
    @ResponseBody
    public PageUtil list(@SessionAttribute(WebSecurityConfig.SESSION_KEY) String username, @RequestBody Query query)
            throws IOException {
        PageHelper.startPage(query.getOffset() + 1, query.getLimit());
        List<TagResult> results = resultService.list(username);
        int total = resultDao.count(username);
        PageUtil pageUtil = new PageUtil(results, total);
        return pageUtil;
    }

    @GetMapping("")
    String trueTag(Model model) {
        return "result/list";
    }
}