package cn.feast.coding.controller.index;

import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.links.Link;
import cn.feast.coding.model.stock.Stock;
import cn.feast.coding.service.links.LinkService;
import cn.feast.coding.service.stock.StockService;
import cn.feast.coding.tools.HttpRequest;
import cn.feast.coding.tools.ListUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/index")
public class IndexController extends BaseController {
    @Autowired
    private LinkService linkService;
    @Autowired
    private StockService stockService;
    private static final Integer MENU_TYPE = 1;
    private static final Integer URL_TYPE = 2;

    @RequestMapping(value = "/links")
    public String index(Link link, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request) {
        link.setTypeid(URL_TYPE);
        PageInfo<Link> pageInfo = linkService.queryPage(link, pageNum(curPage), pageSize(request, pageSize));
        List<Link> menuList = linkService.queryByType(MENU_TYPE);
        List<Link> normalList = ListUtils.getListByNum(linkService.queryByTypeTimes(URL_TYPE), 4);
        model.put("menuList", menuList);
        model.put("normalList", normalList);
        model.put("pages", pageInfo);
        model.put("link", link);
        return "front/index";
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    @ResponseBody
    public void enterSite(Link link) {
        link = linkService.selectByPrimaryKey(link.getId());
        link.setTimes(link.getTimes() + 1);
        linkService.update(link);
    }

    @RequestMapping(value = "/stock", method = RequestMethod.GET)
    @ResponseBody
    public String[] stock(ModelMap model) {
        StringBuffer baseUrl = new StringBuffer();
        baseUrl.append("https://qt.gtimg.cn/q=sh000001");
        List<Stock> stockList = stockService.queryAll();
        for (Stock s : stockList) {
            if (Pattern.matches("^6\\d{5}$", s.getStockCode())) {
                baseUrl.append(",sh" + s.getStockCode());
            } else if (Pattern.matches("^0\\d{5}$", s.getStockCode())) {
                baseUrl.append(",sz" + s.getStockCode());
            }
        }

        String html = HttpRequest.sendGet(baseUrl.toString(), null);
        String[] stocks = html.split(";");
        return stocks;
    }

    @RequestMapping(value = "/stockPage", method = RequestMethod.GET)
    public String stockPage(ModelMap model) {
        return "front/stock";
    }
}
