package de.silveryard.car.kernel.gui;

import de.silveryard.basesystem.sdk.kernel.*;
import de.silveryard.car.kernel.CarReturnCode;
import de.silveryard.transport.Message;
import de.silveryard.transport.Parameter;
import de.silveryard.transport.highlevelprotocols.qa.QAMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Sebif on 10.04.2017.
 */
public abstract class Button {
    private static boolean initialized = false;
    private static Map<Integer, ButtonClickHandler> handlers;

    private synchronized static void initialize(){
        if(initialized){
            return;
        }
        initialized = true;

        handlers = new HashMap<>();

        Kernel.registerForCommand("de.silveryard.car.systemcall.gui.button.onclick", Button::handleOnClick);
    }

    private static void handleOnClick(Message message){
        int id = message.getParams().get(0).getInt();
        ButtonClickHandler handler = handlers.get(id);
        AppSDK.runOnMainThread(new Action() {
            @Override
            public void invoke() {
                handler.invoke(id);
            }
        });
    }

    public static void systemCallCarGuiButtonCreate(
            int positionX, int positionY,
            String text,
            ButtonClickHandler clickHandler,
            Wrapper<ReturnCode> outReturnCode,
            Wrapper<CarReturnCode> outCarReturnCode,
            Wrapper<Integer> outButtonId
    ){
        initialize();

        List<Parameter> params = new ArrayList<>();
        params.add(Parameter.createInt(positionX));
        params.add(Parameter.createInt(positionY));
        params.add(Parameter.createString(text));
        QAMessage result = Kernel.systemCall("de.silveryard.car.systemcall.gui.button.create", params);

        outReturnCode.value = ReturnCode.getEnumValue(result.getParameters().get(0).getInt());
        outCarReturnCode.value = CarReturnCode.getEnumValue(result.getParameters().get(1).getInt());
        outButtonId.value = result.getParameters().get(2).getInt();

        if(outReturnCode.value == ReturnCode.OK && outCarReturnCode.value == CarReturnCode.OK){
            handlers.put(outButtonId.value, clickHandler);
        }
    }
    public static void systemCallCarGuiButtonGetText(
            int buttonID,
            Wrapper<ReturnCode> outReturnCode,
            Wrapper<CarReturnCode> outCarReturnCode,
            Wrapper<String> outText
    ){

        List<Parameter> params = new ArrayList<>();
        params.add(Parameter.createInt(buttonID));
        QAMessage result = Kernel.systemCall("de.silveryard.car.systemcall.gui.button.gettext", params);

        outReturnCode.value = ReturnCode.getEnumValue(result.getParameters().get(0).getInt());
        outCarReturnCode.value = CarReturnCode.getEnumValue(result.getParameters().get(1).getInt());
        outText.value = result.getParameters().get(2).getString();
    }
    public static void systemCallCarGuiButtonSetText(
            int buttonID, String text,
            Wrapper<ReturnCode> outReturnCode,
            Wrapper<CarReturnCode> outCarReturnCode
    ){
        List<Parameter> params = new ArrayList<>();
        params.add(Parameter.createInt(buttonID));
        params.add(Parameter.createString(text));
        QAMessage result = Kernel.systemCall("de.silveryard.car.systemcall.gui.button.settext", params);

        outReturnCode.value = ReturnCode.getEnumValue(result.getParameters().get(0).getInt());
        outCarReturnCode.value = CarReturnCode.getEnumValue(result.getParameters().get(1).getInt());
    }
}
