package com.binho.cosmicsdk.examples.i3c.daa;

import com.binho.cosmicsdk.CosmicSdk;

import static com.binho.cosmicsdk.examples.ExampleHelpers.assertStatus;
import static com.binho.cosmicsdk.examples.ExampleHelpers.printDeviceInfo;
import static com.binho.cosmicsdk.examples.ExampleHelpers.printTargetDeviceTable;

public final class I3cDaaExample {
    private I3cDaaExample() {
    }

    private static void printTable(CosmicSdk sdk, String stage) {
        var table = sdk.i3cGetDeviceTable();
        assertStatus(table.status(), "i3cGetDeviceTable(" + stage + ")");
        printTargetDeviceTable(table.value());
    }

    public static void main(String[] args) {
        try (CosmicSdk sdk = new CosmicSdk(2000)) {
            if (!sdk.connect()) {
                throw new IllegalStateException("Failed to connect to a Binho device.");
            }

            var infoResult = sdk.getDeviceInfo();
            assertStatus(infoResult.status(), "getDeviceInfo");
            printDeviceInfo(infoResult.value());

            assertStatus(sdk.i3cInitBus(), "i3cInitBus");

            assertStatus(sdk.i3cResetBus(), "i3cResetBus before SETDASA");
            assertStatus(sdk.i3cCccSetdasa(0x69, 0x0B), "i3cCccSetdasa(0x69 -> 0x0B)");
            assertStatus(sdk.i3cCccSetdasa(0x6B, 0x0C), "i3cCccSetdasa(0x6B -> 0x0C)");
            printTable(sdk, "after SETDASA");

            assertStatus(sdk.i3cResetBus(), "i3cResetBus before SETAASA");
            assertStatus(sdk.i3cCccSetaasa(new byte[]{0x69, 0x6B}), "i3cCccSetaasa");
            printTable(sdk, "after SETAASA");

            assertStatus(sdk.i3cResetBus(), "i3cResetBus before ENTDAA");
            assertStatus(sdk.i3cCccEntdaa(), "i3cCccEntdaa");
            printTable(sdk, "after ENTDAA");

            assertStatus(sdk.i3cResetBus(), "i3cResetBus before SETNEWDA");
            assertStatus(sdk.i3cInitBus(), "i3cInitBus before SETNEWDA");
            assertStatus(sdk.i3cCccSetnewda(0x08, 0x0B), "i3cCccSetnewda(0x08 -> 0x0B)");
            assertStatus(sdk.i3cCccSetnewda(0x09, 0x0C), "i3cCccSetnewda(0x09 -> 0x0C)");
            printTable(sdk, "after SETNEWDA");
        }
    }
}
