package com.lemon.lifecenter.controller;

import java.util.List;

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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
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;
import com.lemon.lifecenter.dto.CenterInfoDTO;
import com.lemon.lifecenter.dto.PatientDTO;
import com.lemon.lifecenter.service.MobilePatientService;
import com.lemon.lifecenter.service.StaffService;

@Controller
@RequestMapping("/mobile")
public class MobileLoginController extends LifeCenterController {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private StaffService memberService;
    
    @Autowired
    private MobilePatientService patientService;
    
    @Autowired
    private LifeCenterConfigVO config;
    
    @RequestMapping("/login")
    public ModelAndView login() {
        List<CenterInfoDTO> centerList = memberService.selectCenterList();
        
        ModelAndView mv = setMobileMV("login/login");
        mv.addObject("centerList", centerList);
        return mv;
    }
    
    @RequestMapping( value="/check", method = RequestMethod.POST )
    @ResponseBody
    public String loginCheck(
            @ModelAttribute("dto") PatientDTO dto,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        String resultCode = "99";
        String message = "";
        JSONObject object = new JSONObject();
        
        dto.setPw(LifeCenterFunction.aesEncrypt(config.aesKey, config.IV, dto.getPw()));
        
        int cnt = patientService.selectMPatientCount(dto);
        if (cnt == 0) {
            resultCode = "01";
            message = "사용자정보가 일치하지 않습니다.";
        } else {
            dto = patientService.selectMPatientInfo(dto);
            
            LifeCenterSessionController.sessionInvalidate( request );
            
            LifeCenterSessionController.setSession(request, "sesMpIdx",String.valueOf(dto.getPatientIdx()));
            LifeCenterSessionController.setSession(request, "sesMId", dto.getId());
            LifeCenterSessionController.setSession(request, "sesMName", dto.getPatientName());
            LifeCenterSessionController.setSession(request, "sesMGender", dto.getGender());
            LifeCenterSessionController.setSession(request, "sesMWardNum", dto.getWardNumber());
            LifeCenterSessionController.setSession(request, "sesMRoomNum", dto.getRoomNumber());
            LifeCenterSessionController.setSession(request, "sesMCenterCode", String.valueOf(dto.getCenterCode()));

            logger.error("dto.getPw()dto.getPw() -- > " + dto.getPw());
            object.put("id", dto.getId());
            object.put("pw", dto.getPw());
            resultCode = "00";
        }
        
        object.put("code", resultCode);
        object.put("message", message);
        return object.toString();
    }
    
    @RequestMapping("/logout")
    public String patientLogout( HttpServletRequest request, HttpServletResponse response ) {
        String remoteIp  = LifeCenterFunction.getRemoteAddr( request );
        String sesMId     = LifeCenterSessionController.getSession( request, "sesMId" );
        
        LifeCenterSessionController.sessionInvalidate( request );
        
        logger.error( "[PATIENT LOGOUT] RemoteIP : " + remoteIp + " UserId : " + sesMId );
        
        return "redirect:/mobile/login";
    }
}