package com.keyborad.theme.trendyborad.trendyutils;

import com.keyborad.theme.trendyborad.trendykeyboardhelper.TrendyKeyModel;
import com.keyborad.theme.trendyborad.trendykeyboardhelper.TrendyResponseConfig;
import com.keyborad.theme.trendyborad.trendykeyboardhelper.TrendyResponseLayout;

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

public class TrendyDealConfFile {

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

    public static TrendyResponseConfig parseConfig(String filePath)   {
//        InputStream open = App.appInstance.getAssets().open(filePath);
        TrendyResponseConfig config = new TrendyResponseConfig();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            TrendyResponseLayout 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 TrendyResponseLayout(line.split(":")[0].trim());
                    config.addLayout(currentLayout);
                } else if (currentLayout != null) {
                    if (line.equals("Key")) {
                        String[] parts = line.split(":");
                        String keyName = parts[0].trim();
                        TrendyKeyModel trendyKeyModel = new TrendyKeyModel(keyName);
                        currentLayout.addKey(trendyKeyModel);
                    } else if (line.contains(":") && currentLayout.getLastKey().getBackground() == null) {
                        // 解析按键的其他属性（如 Label）
                        String[] parts = line.split(":");
                        String keyName = parts[0].trim();
                        String keyValue = parts[1].trim();
                        TrendyKeyModel trendyKeyModel = currentLayout.getLastKey();
                        if (keyName.equals("Label")) {
                            trendyKeyModel.setLabel(keyValue);
                        }
                        if (keyName.equals("Background")) {
                            trendyKeyModel.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")) {
                            TrendyKeyModel funcationTrendyKeyModel = new TrendyKeyModel(line);
                            config.addKey(funcationTrendyKeyModel);
                        } else if (line.contains(":")) {
                            String[] parts = line.split(":");
                            String keyName = parts[0].trim();
                            String keyValue = parts[1].trim();
                            TrendyKeyModel lastTrendyKeyModel = config.getLastKeyList();
                            if (keyName.equals("Label")) {
                                lastTrendyKeyModel.setLabel(keyValue);
                            }
                            if (keyName.equals("Background")) {
                                lastTrendyKeyModel.setBackground(keyValue);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return config;
    }
}


