package tr.dhee.abox.demo.data;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Class that handles socket device control via SwitchDataSource.
 * Executes SDK calls on a background thread to avoid ANR.
 */
public class SwitchRepository {

    private static volatile SwitchRepository instance;

    private final SwitchDataSource dataSource;
    private final Executor executor = Executors.newSingleThreadExecutor();

    private SwitchRepository(SwitchDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public static SwitchRepository getInstance(SwitchDataSource dataSource) {
        if (instance == null) {
            instance = new SwitchRepository(dataSource);
        }
        return instance;
    }

    /**
     * 控制插座开关, 在后台线程执行 SDK 调用
     *
     * @param soDevName 插座设备名称
     * @param status    状态: "1"(开) 或 "0"(关)
     * @param callback  结果回调 (在主线程调用)
     */
    public void sockCtrl(String soDevName, String status, SwitchCallback callback) {
        executor.execute(() -> {
            Result<String> result = dataSource.sockCtrl(soDevName, status);
            if (callback != null) {
                callback.onComplete(result);
            }
        });


        executor.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
    }

    public interface SwitchCallback {
        void onComplete(Result<String> result);
    }
}
