"use client";

import {useEffect, useState} from "react";
import {fetchConfig} from "@/app/utils/fetchConfig";
import InputControl from "@/app/components/InputControl";

const BusControl = ({busControlConfig, mainConfig}) => {

    const [appConfig, setAppConfig] = useState(null);
    const busId = busControlConfig.id;
    const busChannel = busControlConfig.bus

    useEffect(() => {
        if (mainConfig) {
            setAppConfig(mainConfig);
        } else {
            fetchConfig().then(config => {
                    setAppConfig(config);
                }
            );
        }
    }, [mainConfig]);

    return (
        <div>
            {!appConfig ? <p>Loading...</p> : (
                <div className={"p-2 rounded-md border border-gray-250"}>
                    <div className={"mb-2 pb-3 border-b border-b-gray-250"}>
                        <InputControl
                            id={`bus-${busId}-main`}
                            name={`Main Level`}
                            basePath={`/bus/${busChannel.toString().padStart(2, "0")}/mix`}
                            appConfig={appConfig} faderControlSuffix={undefined} muteControlSuffix={undefined}                        />
                    </div>
                    <div className={"flex flex-col gap-3"}>
                        <h3>Inputs</h3>
                        {appConfig.inputs.map((input) => (
                            <InputControl
                                key={input.id}
                                id={`bus-${busId}-input-${input.id}`}
                                name={input.name}
                                faderControlSuffix={"level"}
                                basePath={`/ch/${input.channels[0].toString().padStart(2, "0")}/mix/${busChannel.toString().padStart(2, "0")}`}
                                appConfig={appConfig} muteControlSuffix={undefined}                            />
                        ))}
                    </div>
                </div>
            )}
        </div>
    );
};

export default BusControl;
