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.CtResponse;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.mapper.LocationMapper;
import com.handong.smartservice.service.LocationService;

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

    private final LocationService locationService;
    

    public LocationController(LocationService locationService) {
        this.locationService = locationService;
    }

    @RequestMapping(value = "/get-location.do", method = RequestMethod.POST)
    public CtResponse getLocationList(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;

        String date_start = body.get("date_start");
        String date_end = body.get("date_end");
        String name  = body.get("name");
        String description = body.get("description");
        String address = body.get("address");
        
        Map<String, Object> mapResult = locationService.getLocationList(offset, limit, date_start, date_end, name, description, address);
        result.put("result", mapResult);
        return result;
    }


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

        Long location_id = _AG.toLong(body.get("location_id"));
        Long pid = _AG.toLong(body.get("pid"));
        String name = body.get("name");
        String description = body.get("description");
        String address = body.get("address");

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

        result = locationService.addOrModifyLocation(isModify, location_id, pid, name, description, address);
        return result;
    }

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

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

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

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

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

        CtResponse result = locationService.removeLocation(ids);
        return result;
    }


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

        Boolean isRoot = _AG.toBoolean(body.get("isRoot"));
        Long pid = _AG.toLong(body.get("id"));
        String date_start = body.get("date_start");
        String date_end = body.get("date_end");
        String name  = body.get("name");
        String description = body.get("description");
        String address = body.get("address");

        return locationService.getLocation2(isRoot, pid, date_start, date_end, name, description, address);
    }
}
