package com.handong.smartservice.controller.api;

import com.handong.smartservice.component.CtExcelMaker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import com.handong.smartservice._AG;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.service.DeviceService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;


import java.util.List;
import java.util.Map;
import java.util.HashMap;


@RestController
@RequestMapping("/api")
class DeviceController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final DeviceService deviceService;
    

    public DeviceController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @RequestMapping(value = "/get-device.do", method = RequestMethod.POST)
    public CtResponse getDeviceList(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        CtResponse result = new CtResponse();

        Boolean is_excel = _AG.toBoolean(body.get("is_excel"));
        Long offset= _AG.toLong(body.get("offset"));
        Long limit = _AG.toLong(body.get("limit"));
        if (limit > 100)
            limit = 100L;

        Boolean is_group_access = _AG.toBoolean(body.get("is_group_access"));
        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        String name = body.get("name");
        String date_start = body.get("date_start");
        String date_end = body.get("date_end");
        
        Map<String, Object> mapResult = deviceService.getDeviceList(is_excel, offset, limit, is_group_access, biz_group_id, name, date_start, date_end);

        if (is_excel) {
            List<Map<String, String>> list = (List<Map<String, String>>)mapResult.get("list");

            for (Map<String, String> item : list) {
                item.replace("reg_time", _AG.timestampToStr(item.get("reg_time")));
                item.replace("connect_time", _AG.timestampToStr(item.get("connect_time")));
                item.replace("disconnect_time", _AG.timestampToStr(item.get("disconnect_time")));
            }

            String[] arrHeader = {"등록일", "그룹", "장치명", "단말기TID", "가동시간", "장애발생시간", "관리자"};
            String[] arrColumn = {"reg_time", "biz_group_name", "name", "uid1", "connect_time", "disconnect_time", "manager_name"};

            CtExcelMaker.makeExcelResponse(request, response, "무인기기목록", arrHeader, arrColumn, list, null);
            return null;
        }

        result.put("result", mapResult);
        return result;
    }


    public CtResponse addOrModifyDevice(boolean isModify, HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        //CtResponse result = new CtResponse();

        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        Long new_biz_group_id = _AG.toLong(body.get("new_biz_group_id"));
        Long device_id = _AG.toLong(body.get("device_id"));
        Long terminal_id = _AG.toLong(body.get("terminal_id"));
        String name = body.get("name");
        String uid1 = body.get("uid1");
        String manager_name = body.get("manager_name");

        //if (name == null || name.isEmpty() || uid1 == null || uid1.isEmpty()) {
        //    result.setErrCode(ErrorCode.INVALID_PARAMETER);
        //    return result;
        //}

        return deviceService.addOrModifyDevice(isModify, biz_group_id, new_biz_group_id, device_id, terminal_id, name, uid1, manager_name);
    }

    @RequestMapping(value = "/add-device.do", method = RequestMethod.POST)
    public CtResponse addDevice(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        return addOrModifyDevice(false, request, response, body);
    }

    @RequestMapping(value = "/modify-device.do", method = RequestMethod.POST)
    public CtResponse modifyDevice(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        return addOrModifyDevice(true, request, response, body);
    }

    @RequestMapping(value = "/remove-device.do", method = RequestMethod.POST)
    public CtResponse removeDevice(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());
        
        //String ids = body.get("ids");
        //CtResult result = deviceService.removeDevice(ids);

        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        Long device_id = _AG.toLong(body.get("device_id"));

        return deviceService.removeDevice(biz_group_id, device_id);
    }

    @RequestMapping(value = "/config-device.do", method = RequestMethod.POST)
    public CtResponse configDevice(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        CtResponse result = new CtResponse();

        Long device_id = _AG.toLong(body.get("device_id"));
        
        Map<String, Object> mapResult = deviceService.configDevice(device_id);
        result.put("result", mapResult);
        return result;
    }

    @RequestMapping(value = "/error-device.do", method = RequestMethod.POST)
    public CtResponse selectDeviceByError(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        CtResponse result = new CtResponse();

        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        
        Map<String, Object> mapResult = deviceService.selectDeviceByError(biz_group_id);
        result.put("result", mapResult);
        return result;
    }
}
