package com.yihu.hos.web.framework.util.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import com.yihu.hos.web.framework.model.DetailModelResult;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/8/8.
 */
public class BaseController extends AbstractController {
    static private final Logger logger = LoggerFactory.getLogger(BaseController.class);

    public BaseController() {
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        return null;
    }

    public DetailModelResult getResult(List detaiModelList, int totalCount, int currPage, int rows) {
        DetailModelResult result = new DetailModelResult();
        result.setSuccessFlg(true);
        result.setDetailModelList(detaiModelList);
        result.setTotalCount(totalCount);
        result.setCurrPage(currPage);
        result.setPageSize(rows);
        if (result.getTotalCount() % result.getPageSize() > 0) {
            result.setTotalPage(result.getTotalCount() / result.getPageSize() + 1);
        } else {
            result.setTotalPage(result.getTotalCount() / result.getPageSize());
        }

        return result;
    }

    public String encodeStr(String str) {
        try {
            return str == null ? null : new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException var3) {
            logger.error(var3.getMessage());
            return null;
        }
    }

    public List jsonArrayToList(String jsonArray, Class tClass) {
        ObjectMapper mapper = new ObjectMapper();
        ArrayList list = new ArrayList();
        ArrayNode newJsonArray = null;
        try {
            newJsonArray = mapper.readValue(jsonArray, ArrayNode.class);
            if (newJsonArray != null && newJsonArray.size() > 0) {
                Iterator it = newJsonArray.iterator();
                while (it.hasNext()) {
                    JsonNode jsonObj = (JsonNode) it.next();
                    Object model = mapper.readValue(jsonObj.toString(), tClass);
                    list.add(model);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public Object jsonToObject(String json, Class tClass) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(json, tClass);
        } catch (IOException e) {
            e.printStackTrace();

            logger.error(e.getMessage());
        }

        return null;
    }
}