package com.keyboard.themes.key.utils;

import com.keyboard.themes.key.resconfig.Key;
import com.keyboard.themes.key.resconfig.KeyboardConfig;
import com.keyboard.themes.key.resconfig.KeyboardLayout;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class KeyboardConfigParser {

    public static KeyboardConfig initConfig(String path)  {
        String filePath = "keyboard.conf"; // 文件路径
        KeyboardConfig config = parseConfig(path);


//        System.out.println("Version: " + config.getVersion());
//        System.out.println("SupportLayouts: " + config.getSupportLayouts());
//        System.out.println("HideHint: " + config.getHideHint());
//        System.out.println("LayoutStyle: " + config.getLayoutStyle());
//
//        for (KeyboardLayout layout : config.getLayouts()) {
//            System.out.println("\nLayout: " + layout.getName());
//            for (Key key : layout.getKeys()) {
//                System.out.println("Key: " + key.getName() + ", Background: " + key.getBackground() + ", Label: " + key.getLabel());
//            }
//        }
//        for (Map.Entry<String, String> stringStringEntry : config.getMaps().entrySet()) {
//            System.out.println("Map  Key= " + stringStringEntry.getKey() + ", value= " + stringStringEntry.getValue());
//        }
//        for (Key funcationKey : config.getKeyList()) {
//            System.out.println("Key: " + funcationKey.getName()+" Background= " + funcationKey.getBackground() + ", Label= " + funcationKey.getLabel());
//        }
        return config;
    }

    public static KeyboardConfig parseConfig(String filePath)   {
//        InputStream open = AppThemes.appThemesInstance.getAssets().open(filePath);
        KeyboardConfig config = new KeyboardConfig();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            KeyboardLayout currentLayout = null;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) {
                    continue; // 跳过空行
                }
                if (line.startsWith("Version:")) {
                    config.setVersion(line.split(":")[1].trim());
                } else if (line.startsWith("SupportLayouts:")) {
                    config.setSupportLayouts(line.split(":")[1].trim());
                } else if (line.startsWith("HideHint:")) {
                    config.setHideHint(Integer.parseInt(line.split(":")[1].trim()));
                } else if (line.startsWith("LayoutStyle:")) {
                    config.setLayoutStyle(line.split(":")[1].trim());
                } else if (line.equals("KeyDefault") || line.equals("KeyMarkDefault") || line.equals("KeyFuncDefault")) {
                    LinkedHashMap<String, String> maps = config.getMaps();
                    maps.put(line, "");
                } else if (line.contains(":") && currentLayout == null) {
                    String[] parts = line.split(":");
                    String keyName = parts[0].trim();
                    String keyValue = parts[1].trim();

                    String latestKey = null;
                    LinkedHashMap<String, String> maps = config.getMaps();
                    for (Map.Entry<String, String> entry : maps.entrySet()) {
                        latestKey = entry.getKey();
                    }
                    if (latestKey != null) {
                        maps.put(latestKey, keyValue);
                    }
                } else if (line.startsWith("Row")) {
                    currentLayout = new KeyboardLayout(line.split(":")[0].trim());
                    config.addLayout(currentLayout);
                } else if (currentLayout != null) {
                    if (line.equals("Key")) {
                        String[] parts = line.split(":");
                        String keyName = parts[0].trim();
                        Key key = new Key(keyName);
                        currentLayout.addKey(key);
                    } else if (line.contains(":") && currentLayout.getLastKey().getBackground() == null) {
                        // 解析按键的其他属性（如 Label）
                        String[] parts = line.split(":");
                        String keyName = parts[0].trim();
                        String keyValue = parts[1].trim();
                        Key key = currentLayout.getLastKey();
                        if (keyName.equals("Label")) {
                            key.setLabel(keyValue);
                        }
                        if (keyName.equals("Background")) {
                            key.setBackground(keyValue);
                        }
                    } else {
                        if (line.equals("KeyShift") || line.equals("KeyDelete") || line.equals("KeyAlphaSymbol") || line.equals("KeyEmoji")
                                || line.equals("KeyMark")
                                || line.equals("KeySpace")
                                || line.equals("KeyEnter")) {
                            Key funcationKey = new Key(line);
                            config.addKey(funcationKey);
                        } else if (line.contains(":")) {
                            String[] parts = line.split(":");
                            String keyName = parts[0].trim();
                            String keyValue = parts[1].trim();
                            Key lastKey = config.getLastKeyList();
                            if (keyName.equals("Label")) {
                                lastKey.setLabel(keyValue);
                            }
                            if (keyName.equals("Background")) {
                                lastKey.setBackground(keyValue);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return config;
    }
}

// 配置文件对象模型


// 键盘布局对象模型


