package controller;

import dao.impl.CourseDBImpl;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import pojo.CourseInfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/courseAction/*")
public class CourseController extends BaseController {
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        int page = req.getParameter("page") == null ? 1 : Integer.parseInt(req.getParameter("page"));
        int pageSize = req.getParameter("pageSize") == null ? 10 : Integer.parseInt(req.getParameter("pageSize"));
        String keyword = req.getParameter("keyword");
        int courseType = req.getParameter("courseType").isEmpty() ? 0 : Integer.parseInt(req.getParameter("courseType"));
        ArrayList<CourseInfo> course_list = new CourseDBImpl().getCourseInfos(page, pageSize, keyword, courseType);
        StringBuilder data = new StringBuilder();
        for (CourseInfo course : course_list) {
            data.append(course.toJSON()).append(",");
        }
        if (!data.isEmpty()) {
            data.setLength(data.length() - 1);
        }
        String json = "{\"total\":" + new CourseDBImpl().getAllCourseInfoCount(keyword, courseType) +
                ",\"data\":[" +
                data +
                "],\"page\":" +
                page +
                ",\"pageSize\":" +
                pageSize +
                ",\"success\":true}";
        out.print(json);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if (req.getParameter("id") == null) {
            out.println("{\"success\":false}\"}");
            return;
        }
        int id = Integer.parseInt(req.getParameter("id"));
        CourseInfo course = new CourseDBImpl().getCourseInfoByID(id);
        if (course == null) {
            out.println("{\"success\":false}\"}");
            return;
        }
        String name = req.getParameter("name");
        int type = Integer.parseInt(req.getParameter("type"));
        float credit = Float.parseFloat(req.getParameter("credit"));
        int grade = Integer.parseInt(req.getParameter("grade"));
        int major = Integer.parseInt(req.getParameter("major"));
        String detail = req.getParameter("detail");
        course.setName(name);
        course.setType(type);
        course.setCredit(credit);
        course.setGrade(grade);
        course.setMajor(major);
        course.setDetail(detail);

        if (new CourseDBImpl().updateCourseInfo(course) >= 1) {
            out.println("{\"success\":true}");
        } else {
            out.println("{\"success\":false}");
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if (new CourseDBImpl().deleteCourseInfo(id) >= 1) {
            out.print("{\"success\": true}");
        } else {
            out.print("{\"success\": false}");
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if (req.getParameter("id") == null) {
            out.println("{\"success\":false}\"}");
            return;
        }
        int id = Integer.parseInt(req.getParameter("id"));
        CourseInfo course = new CourseInfo();
        course.setName(req.getParameter("name"));
        course.setType(Integer.parseInt(req.getParameter("type")));
        course.setCredit(Float.parseFloat(req.getParameter("credit")));
        course.setGrade(Integer.parseInt(req.getParameter("grade")));
        course.setMajor(Integer.parseInt(req.getParameter("major")));
        course.setDetail(req.getParameter("detail"));
        if (new CourseDBImpl().addCourseInfo(course) >= 1) {
            out.println("{\"success\": true}");
        }else{
            out.println("{\"success\": false}");
        }
    }
}
