package web.ParkingRecordRelated;

import pojo.ParkingRecord;
import pojo.Vehicle;
import service.ParkingRecordService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/main/admin/Records")
public class SelectParkingRecordServlet extends HttpServlet {
    ParkingRecordService service = new ParkingRecordService();
    public void addAttribute(HttpServletRequest req) throws Exception {
        Map<String, Double> topCost = service.getTopCost();
        Map<Date, Double> monthIncome = service.getMonthIncome();
        int peopleBusy = service.getBusyNum();
        int peopleSum = service.getUserSum();
        Map<Integer, Double> hourMins = service.getHourMins();
        req.setAttribute("monthIncome", monthIncome);
        req.setAttribute("topCost", topCost);
        req.setAttribute("peopleBusy", peopleBusy);
        req.setAttribute("peopleSum", peopleSum);
        req.setAttribute("hourMins", hourMins);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<ParkingRecord> parkingRecords = null;
        try {
            parkingRecords = service.getAllRecords();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            addAttribute(req);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("parkingRecords", parkingRecords);
        req.getRequestDispatcher("/main/admin/adminRecord.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String car_license = req.getParameter("car_license");
        String location = req.getParameter("location");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date start_date = null;
        try {
            start_date = parseDateParameter(req.getParameter("start_time"), dateFormat);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Date end_date = null;
        try {
            end_date = parseDateParameter(req.getParameter("end_time"), dateFormat);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        Timestamp start_time = convertToTimestamp(start_date);
        Timestamp end_time = convertToTimestamp(end_date);

        List<ParkingRecord> parkingRecords = null;
        try {
            parkingRecords = service.getRecordsByCase(car_license, location, start_time, end_time);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            addAttribute(req);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("parkingRecords", parkingRecords);
        req.getRequestDispatcher("/main/admin/adminRecord.jsp").forward(req, resp);
    }

    private Date parseDateParameter(String parameter, SimpleDateFormat dateFormat) throws ParseException {
        if (parameter != null && !parameter.isEmpty()) {
            return dateFormat.parse(parameter);
        }
        return null;
    }

    private Timestamp convertToTimestamp(Date date) {
        if (date != null) {
            return new Timestamp(date.getTime());
        }
        return null;
    }

}


