package com.zhangmeng.online.exam.ui.api;

import com.zhangmeng.online.exam.ui.admin.IndexPage;
import com.zhangmeng.online.exam.ui.api.form.PaperForm;
import com.zhangmeng.online.exam.ui.api.form.QuestionForm;
import com.zhangmeng.online.exam.ui.api.form.RoleForm;
import com.zhangmeng.online.exam.ui.api.form.UserForm;
import com.zhangmeng.online.exam.ui.api.model.*;
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBack;
import com.zhangmeng.online.exam.ui.layouts.SideMenu;
import com.zhangmeng.online.exam.ui.utils.FxUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.beans.property.SimpleMapProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableView;
import com.alibaba.fastjson2.*;
import javafx.scene.layout.BorderPane;

import java.util.*;

/**
 * @author zm
 * @date 2025/3/6 15:17
 * @version: 1.0
 */
public class ApiUtils {

    public static final String API_URL = "http://localhost:8083";

    public static final int PAGE_SIZE = 25;

    public static final int PAGE_NUM = 1;

    public static List<SideMenu.SideMenuItem> getMenuItems() {

        List<SideMenu.SideMenuItem> menuItems = new ArrayList<>();

        String sideData = HttpUtils.GET(API_URL + "/user/menu");
        JSONObject jsonObject = JSON.parseObject(sideData);
        JSONArray data = jsonObject.getJSONArray("data");

        for (int i = 0; i < data.size(); i++) {
            JSONObject menu = data.getJSONObject(i);
            String name = menu.getString("name");
            String url = menu.getString("url");
            JSONArray children = (JSONArray) menu.get("children");
            List<SideMenu.SideMenuData> sideMenuDataList = new ArrayList<>();
            children.forEach(child -> {
                JSONObject childMenu = (JSONObject) child;
                String childName = childMenu.getString("name");
                String childUrl = childMenu.getString("url");
                SideMenu.SideMenuData sideMenuData = new SideMenu.SideMenuData(childName, childUrl);
                sideMenuDataList.add(sideMenuData);
            });
            SideMenu.SideMenuItem item = new SideMenu.SideMenuItem(name, sideMenuDataList);
            menuItems.add(item);
        }

        return menuItems;
    }

    public static DataView getUserList() {
        DataLoad dataLoad = new UserDataLoad();
        dataLoad.setForm(new UserForm());
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getRoleList() {
        DataLoad dataLoad = new RoleDataLoad();
        dataLoad.setForm(new RoleForm());
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getPermissionList() {
        DataLoad dataLoad = new PermissionDataLoad();
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getQuestionList() {
        DataLoad dataLoad = new QuestionDataLoad();
        dataLoad.setForm(new QuestionForm());
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getSubjectList() {
        DataLoad dataLoad = new SubjectDataLoad();
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getQuestionOptionList() {
        DataLoad dataLoad = new QuestionOptionDataLoad();
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getAnswerList() {
        DataLoad dataLoad = new AnswerDataLoad();
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getExamPaper() {
        DataLoad dataLoad = new PaperDataLoad();
        dataLoad.setForm(new PaperForm());
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static DataView getProfessionList() {
        DataLoad dataLoad = new ProfessionDataLoad();
        return dataLoad.loadData(PAGE_NUM,PAGE_SIZE);
    }

    public static Map<String, SimpleStringProperty> selectedItem(MenuItem menuItem){
        ContextMenu parentPopup = menuItem.getParentPopup();
        BorderPane indexPage = (BorderPane)parentPopup.getStyleableParent();
        TabPane tabPane = (TabPane)indexPage.getCenter();
        DynamicTableComponent center = (DynamicTableComponent)tabPane.getSelectionModel().getSelectedItem().getContent();
        TableView<Map<String, SimpleStringProperty>> tableView = center.getTableView();
        return tableView.getSelectionModel().getSelectedItem();
    }

    public static void submitAnswer(Map<String, Object> context) {
        System.out.println(context);
    }



    public static class DataView {

        private List<String> keys;

        private List<Map<String, SimpleStringProperty>> dataList;

        private Integer pageNum;
        private Integer pageSize;
        private Integer total;

        private DynamicTableComponentCallBack callBack;

        private DataLoad dataLoad;

        public DataLoad getDataLoad() {
            return dataLoad;
        }

        public DataView setDataLoad(DataLoad dataLoad) {
            this.dataLoad = dataLoad;
            return this;
        }

        public DynamicTableComponentCallBack getCallBack() {
            return callBack;
        }

        public void setCallBack(DynamicTableComponentCallBack callBack) {
            this.callBack = callBack;
        }

        public DataView(List<String> keys, List<Map<String, SimpleStringProperty>> dataList, Integer pageNum, Integer pageSize, Integer total) {
            this.keys = keys;
            this.dataList = dataList;
            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.total = total;
        }

        public Integer getPageNum() {
            return pageNum;
        }

        public DataView setPageNum(Integer pageNum) {
            this.pageNum = pageNum;
            return this;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public DataView setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Integer getTotal() {
            return total;
        }

        public DataView setTotal(Integer total) {
            this.total = total;
            return this;
        }

        public DataView(List<String> keys, List<Map<String, SimpleStringProperty>> dataList) {
            this.keys = keys;
            this.dataList = dataList;
        }

        public DataView() {
        }

        public List<String> getKeys() {
            return keys;
        }

        public void setKeys(List<String> keys) {
            this.keys = keys;
        }

        public List<Map<String, SimpleStringProperty>> getDataList() {
            return dataList;
        }

        public void setDataList(List<Map<String, SimpleStringProperty>> dataList) {
            this.dataList = dataList;
        }
    }
}
