#include <iostream>
using namespace std;

// MAShiroUI
#include "mashiroui.h"

/*
 *  输入状态码
 *  0 - 无输入
 *  1 - 上
 *  2 - 下
 *  3 - 确认
 *  4 - 返回
 * */
int key_status = 0;

/*
 *  当前显示状态
 *  0 - 主菜单
 *  1 - 索引至 main_menu
 * */
int display_status = 0;

menu_table main_menu [] = {
    {3, "功能-01", main_menu, nullptr, function_01},
    {3, "功能-02", main_menu, fun_01_menu, nullptr},
    {3, "功能-03", main_menu, nullptr, nullptr},
};

menu_table fun_01_menu [] = {
        {4, "a", main_menu, nullptr, nullptr},
        {4, "b", main_menu, nullptr, nullptr},
        {4, "c", main_menu, nullptr, nullptr},
        {4, "d", main_menu, nullptr, nullptr},
};

// 当前表项
menu_table *current_table = main_menu;
// 当前所选
int current_table_page = 0;

// 获取用户输入的字符
char getInputToChar(){
    char temp;
    cout << "= u -> 上       e -> 确认" << endl;
    cout << "= d -> 下       r -> 返回" << endl;
    cout << "Input: ";
    cin >> temp;
    return temp;
}

void update_keyStatus(char uin){
    int tempKey = 0;
    if (uin == 'u'){
        tempKey = 1;
    }
    if (uin == 'd'){
        tempKey = 2;
    }
    if (uin == 'e'){
        tempKey = 3;
    }
    if (uin == 'r'){
        tempKey = 4;
    }

    key_status = tempKey;
}

int main() {

    while (true){
        system("cls");

        if (key_status != 0){
            int table_length = current_table[0].length;
            // up
            if (key_status == 1){
                if (display_status != 1){
                    if (display_status == 0){
                        display_status = 2;
                    } else {
                        display_status = 0;
                    }
                } else {
                    if (current_table_page - 1 < 0){
                        current_table_page = table_length - 1;
                    } else {
                        current_table_page--;
                    }
                }
            }
            if (key_status == 2){
                if (display_status != 1){
                    if (display_status == 0){
                        display_status = 2;
                    } else {
                        display_status = 0;
                    }
                } else {
                    if (current_table_page + 2 > table_length){
                        current_table_page = 0;
                    } else {
                        current_table_page++;
                    }
                }
            }
            if (key_status == 3){
                if (display_status != 1){
                    display_status = 1;
                    current_table = main_menu;
                } else {
                    if (current_table[current_table_page].sub_table == nullptr &&
                        current_table[current_table_page].function != nullptr){
                        (current_table[current_table_page].function)();
                    }
                    if (current_table[current_table_page].sub_table != nullptr &&
                        current_table[current_table_page].function == nullptr){
                        current_table = current_table[current_table_page].sub_table;
                    }
                }
            }
            if (key_status == 4){
                if (current_table == main_menu){
                    display_status = 0;
                } else {
                    current_table = current_table[current_table_page].parent_table;
                }
            }
            key_status = 0;
        }

        if (current_table_page + 1 > current_table[0].length){
            current_table_page = 0;
        }

        // 判断需要显示的菜单
        if (display_status == 0){
            cout << "this is main" << endl;
        }
        if (display_status == 1){
            int i;

            cout << "+";
            for (i = 0; i < 20; i++){
                cout << "-";
            }
            cout << "+" << endl;

            for (i = 0; i < current_table[0].length; i++){
                cout << "+ [";
                if (current_table_page == i){
                    cout << "0";
                } else {
                    cout << " ";
                }
                cout << "] - " << current_table[i].table_title;
                cout << endl;
            }

            cout << "+";
            for (i = 0; i < 20; i++){
                cout << "-";
            }
            cout << "+" << endl;
        }
        if (display_status == 2){
            cout << "this is main 02" << endl;
        }

        // 获取用户输入状态
        update_keyStatus(getInputToChar());
    }

    return 0;
}