package com.handong.smartservice.controller.api;

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.CtExcelMaker;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.service.TerminalService;

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 TerminalController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final TerminalService terminalService;
    

    public TerminalController(TerminalService terminalService) {
        this.terminalService = terminalService;
    }

    @RequestMapping(value = "/get-terminal.do", method = RequestMethod.POST)
    public CtResponse getTerminalList(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"));
        Long state = _AG.toLong(body.get("state"));
        String date_start = body.get("date_start");
        String date_end = body.get("date_end");
        
        Map<String, Object> mapResult = terminalService.getTerminalList(is_excel, offset, limit, is_group_access, biz_group_id, state, 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")));
            }

            String[] arrHeader = {"등록일", "사업자", "기기명", "단말기 TID", "무인기기 장착 여부"};
            String[] arrColumn = {"reg_time", "biz_group_name", "name", "uid1", "state"};

            CtExcelMaker.makeExcelResponse(request, response, "카드단말기목록", arrHeader, arrColumn, list, null);
            return null;
        }

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


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

        Long addType = _AG.toLong(body.get("addType"));
        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        Long terminal_id = _AG.toLong(body.get("terminal_id"));
        String name = body.get("name");
        String uid1 = body.get("uid1");
        Long type = _AG.toLong(body.get("type"));

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

        return terminalService.addOrModifyTerminal(isModify, addType, biz_group_id, terminal_id, name, uid1, type);
    }

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

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

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

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

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

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

        return terminalService.removeTerminal(biz_group_id, terminal_id);
    }

    @RequestMapping(value = "/terminal-state.do", method = RequestMethod.POST)
    public CtResponse terminalState(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());
        
        String uid1 = body.get("uid1");
        Long type = _AG.toLong(body.get("type"));
        Long state = _AG.toLong(body.get("state"));

        return terminalService.terminalState(uid1, type, state);
    }
}
