package com.foxx.androidcast;

import com.foxx.androidcast.remoteaccess.RemoteAccessMode;
import com.foxx.androidcast.remoteaccess.RemoteAccessStatusStore;

import java.util.ArrayList;
import java.util.List;

/** Pure text assembly for the single persistent tray notification. */
public final class CastTrayContent {
    public static final class Body {
        public final String summary;
        public final String bigText;

        public Body(String summary, String bigText) {
            this.summary = summary != null ? summary : "";
            this.bigText = bigText != null ? bigText : "";
        }
    }

    private CastTrayContent() {}

    public static Body build(
            String idleTapToOpen,
            String castActive,
            String adbWaiting,
            String adbLineFormat,
            String raPollFormat,
            String raConnected,
            String raSessionFormat,
            boolean casting,
            boolean adbEnabled,
            String adbConnectLine,
            RemoteAccessMode raMode,
            RemoteAccessStatusStore.Snapshot raSnap) {
        if (casting) {
            return new Body(castActive, castActive);
        }
        List<String> lines = new ArrayList<>();
        String raLine = remoteAccessLine(raPollFormat, raConnected, raSessionFormat, raMode, raSnap);
        if (raLine != null && !raLine.isEmpty()) {
            lines.add(raLine);
        }
        if (adbEnabled) {
            String adbLine = adbConnectLine == null || adbConnectLine.isEmpty()
                    ? adbWaiting
                    : String.format(adbLineFormat, adbConnectLine);
            lines.add(adbLine);
        }
        if (!lines.isEmpty()) {
            String joined = joinLines(lines);
            return new Body(lines.get(0), joined);
        }
        return new Body(idleTapToOpen, idleTapToOpen);
    }

    static String remoteAccessLine(
            String pollFormat,
            String connected,
            String sessionFormat,
            RemoteAccessMode mode,
            RemoteAccessStatusStore.Snapshot snap) {
        if (mode == null || !mode.isActive()) {
            return null;
        }
        if (snap != null && RemoteAccessStatusStore.STATE_SUCCESS.equals(snap.state)) {
            if (snap.sessionId != null && !snap.sessionId.isEmpty()) {
                String line = String.format(sessionFormat, snap.sessionId);
                if (mode == RemoteAccessMode.RSSH) {
                    return line + " (RSSH)";
                }
                return line;
            }
            if (mode == RemoteAccessMode.RSSH) {
                return connected + " (RSSH)";
            }
            return connected;
        }
        return String.format(pollFormat, mode.name());
    }

    private static String joinLines(List<String> lines) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lines.size(); i++) {
            if (i > 0) {
                sb.append('\n');
            }
            sb.append(lines.get(i));
        }
        return sb.toString();
    }
}
