package jp.co.y_net.amm.api;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
import org.apache.wicket.request.http.WebResponse;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.time.Time;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author k_inaba
 */
public abstract class ApiPage extends WebPage{
    
    @Override
    protected void configureResponse(WebResponse response){
//        response.setContentType("application/json; charset=utf-8");
        response.setContentType("application/json; utf-8");
       
       /* ブラウザキャッシュ無効化 */
       java.util.Calendar objCal1=java.util.Calendar.getInstance();
       java.util.Calendar objCal2=java.util.Calendar.getInstance();
       objCal2.set(1970,0,1,0,0,0);

       response.setDateHeader("Last-Modified",Time.valueOf(objCal1.getTime()));
       response.setDateHeader("Expires",Time.valueOf(objCal2.getTime()));
       response.setHeader("progma","no-cache");
       response.setHeader("Cache-Control","no-cache");

       
       super.configureResponse(response);
    }
    
    public void setResponse(Object result) {
        ObjectMapper objectMapper = new ObjectMapper();
        String json;
        try {
            json = objectMapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        
        getRequestCycle().scheduleRequestHandlerAfterCurrent(
           new ResourceStreamRequestHandler(new StringResourceStream(json))
        );
    }
//    /* ----------------------------------------------
//     * パラメータ取得ユーティリティ
//     */
//    protected String getParam(String key) {
//        if (getPageParameters() == null) {
//            return "";
//        } else {
//            return getPageParameters().get(key).toString("");
//        }
//    }
    
    /* ----------------------------------------------
     * 返却値の共通実装
     */
    public static class ApiResult{
        
        public boolean r = true;
        public String rmsg = null;

        public ApiResult() {
            /* デフォルトコンストラクタ */
        }
        public ApiResult(String rmsg) {
            this.r = false;
            this.rmsg = rmsg;
      }
        public ApiResult(Exception e) {
            this.rmsg = e.getMessage();
            if (rmsg == null) {
                this.rmsg = e.getClass().getSimpleName();
            }
        }
        public ApiResult err(String rmsg) {
            this.r = false;
            this.rmsg = rmsg;
            return this;
        }
        public ApiResult err(Exception e) {
            this.rmsg = e.getMessage();
            if (rmsg == null) {
                this.rmsg = e.getClass().getSimpleName();
            }
            return this;
        }
    }
    
}
//// String text = "{\"a\":123,\"b\":true,\"c\":\"あいう\"}";
//String text = readTextAsString(file.getAbsolutePath());
//ObjectMapper mapper = new ObjectMapper();
//T bean;
//try {
//   // JsonNode root = mapper.readTree(text);
//   // String c = root.get("c").asText();
//   bean = mapper.readValue(text, clz);
//} catch (JsonProcessingException e) {
//   throw new RuntimeException(e);
//} catch (IOException e) {
//   throw new RuntimeException(e);
//}


///**
//* 保存する
//* @param bean 保存するデータ
//* @return id 保存したときに付与されたid
//*/
//public int insert(Object bean) {
//   String tablename = bean.getClass().getName();
//   int maxId = -1;
//   File[] existfiles = getDbFiles(tablename);
//   for (File existfile : existfiles) {
//        int id = toId(existfile.getName(), tablename);
//        maxId = Math.max(maxId, id);
//   }
//   int insertId = maxId + 1;
//   ObjectMapper objectMapper = new ObjectMapper();
//   try {
//        String json = objectMapper.writeValueAsString(bean);
//        File f = toFile(bean.getClass(), (insertId));
//        writeText(f.getAbsolutePath(), json);
//   } catch (JsonProcessingException e) {
//        throw new RuntimeException(e);
//   }
//   return insertId;
//}

