package com.lemon.lifecenter.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.lemon.lifecenter.common.LifeCenterConfigVO;
import com.lemon.lifecenter.common.LifeCenterController;
import com.lemon.lifecenter.common.LifeCenterFunction;
import com.lemon.lifecenter.common.LifeCenterSessionController;
@Controller
@RequestMapping("/mobile")
public class MobileNonFaceController extends LifeCenterController {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private LifeCenterConfigVO config;
    
    @RequestMapping("/nonface")
    public ModelAndView nonFace(
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        String salt = LifeCenterFunction.getSalt();
        String timeStamp = LifeCenterFunction.getTimestamp();
        String signature = LifeCenterFunction.getSignature(config.nonFaceApiSecret, salt, timeStamp);
        
        String patientId = LifeCenterSessionController.getSession( request, "sesMId" );
        String roomId = LifeCenterSessionController.getSession(request, "sesMRoomNum");
        String patientName = LifeCenterSessionController.getSession(request, "sesMName");
        
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("api_key", config.nonFaceApiKey);
        data.put("salt", salt);
        data.put("timestamp", timeStamp);
        data.put("signature", signature);
        data.put("client_id", config.nonFaceClientId);
        data.put("member_id", patientId);
        
        String result = LifeCenterFunction.httpUrlConnection(config.nonFaceApiTokenUrl, data);
        JSONObject object = new JSONObject(result);
        String message = "";
        String token = "";
        for(String key : object.keySet()) {
            logger.error("key -- > " + key);
            if (key.equals("errorCode")) {
                message = object.getString("message");
            } else if (key.equals("token")){
                token = object.getString("token");
            }
        }
        
        if (!message.equals("")) {
            //error
        }
        
        String hashData = LifeCenterFunction.getSignature(config.nonFaceApiSecret, config.nonFaceApiKey, roomId + patientId); //"1011503101"
        
        ModelAndView mv = setMobileMV("nonface/nonface");
        mv.addObject("api_key", config.nonFaceApiKey);
        mv.addObject("member_id", patientId);
        mv.addObject("token", token);
        mv.addObject("room_id", roomId);
        mv.addObject("member_name", patientName);
        mv.addObject("classify", "p"); // p이면 환자 d이면 의사
        mv.addObject("hashData", hashData);
        
        return mv;
    }
    
}
