/**
 **
 ** McpDialogSession.java - Session state for an MCP-driven ImageJ dialog interaction
 **
 ** Copyright (C) 2026 Elphel, Inc.
 **
 ** -----------------------------------------------------------------------------**
 **
 **  McpDialogSession.java is free software: you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation, either version 3 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ** -----------------------------------------------------------------------------**
 **
 */
package com.elphel.imagej.mcp;

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

public class McpDialogSession {
	public static final int debugLevel = -1; // name in the same case in most other places. Compares if (debugLevel >-3)
    private final String id;
    private final String title;
    private final List<McpDialogField> fields;
    private final Map<String, String> valuesByLabel;
    private final Object submitLock = new Object();
    private Boolean submittedOk = null;

    public McpDialogSession(String title, List<McpDialogField> fields) {
        this.id = UUID.randomUUID().toString();
        this.title = title;
        this.fields = new ArrayList<McpDialogField>(fields);
        this.valuesByLabel = new HashMap<String, String>();
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public List<McpDialogField> getFields() {
        return Collections.unmodifiableList(fields);
    }

    public void setValue(String label, String value) {
        if (label == null) {
            return;
        }
        valuesByLabel.put(label, value);
        if (debugLevel > -3) {
        	System.out.println("setValue("+label+","+value+")");
        }
    }

    public void submit(boolean ok) {
        synchronized (submitLock) {
            submittedOk = Boolean.valueOf(ok);
            submitLock.notifyAll();
            if (debugLevel > -3) {
            	System.out.println("submit("+ok+")");
            }
        }
    }

    public Boolean awaitSubmit(long timeoutMs) {
        synchronized (submitLock) {
            if (submittedOk != null) {
                return submittedOk;
            }
            long deadline = System.currentTimeMillis() + timeoutMs;
            while (submittedOk == null) {
                long remaining = deadline - System.currentTimeMillis();
                if (remaining <= 0) {
                    break;
                }
                try {
                    submitLock.wait(remaining);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
            return submittedOk;
        }
    }

    public String getValue(String label) {
        if (label == null) {
            return null;
        }
        return valuesByLabel.get(label);
    }
}
