// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

package org.qtproject.qt.android.qtquickview_signallistener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import java.util.ArrayList;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import org.qtproject.qt.android.QtQmlStatus;
import org.qtproject.qt.android.QtQmlStatusChangeListener;
import org.qtproject.qt.android.QtQuickView;
import org.qtproject.qt.android.QtSignalListener;
import org.qtproject.qt.android.qtquickview_signallistener_qml.tst_qtquickview_signallistener_qml.TestViewModule.TestView;

public class TestActivity extends Activity implements QtQmlStatusChangeListener
{
    native void onBasicSignal();
    native void onIntSignal(Integer value);
    native void onBoolSignal(Boolean value);
    native void onDoubleSignal(Double value);
    native void onStringSignal(String value);
    native void onManyTypeSignal(Integer i, Boolean v, Double d, String s);

    native void onEarlyRegistrationQuickViewContentSignal(Integer v);
    native void onEarlyRegistrationQuickViewSignal(Integer v);

    private final TestView m_testView = new TestView();
    private QtQuickView m_quickView;
    private static TestActivity m_instance;
    private List<Integer> m_signalListenerIds = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_instance = this;
        m_quickView = new QtQuickView(this);
        m_testView.setStatusChangeListener(this);

        m_testView.connectEarlyRegistrationQuickViewContentSignalListener(
                (String unused, Integer v) -> onEarlyRegistrationQuickViewContentSignal(v));

        m_quickView.connectSignalListener(
                "earlyRegistrationQuickViewSignal", Integer.class,
                (String unused, Integer v) -> onEarlyRegistrationQuickViewSignal(v));

        final FrameLayout qmlFrame = findViewById(R.id.qmlFrame);
        final ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        qmlFrame.addView(m_quickView, params);
        m_quickView.loadContent(m_testView);
    }

    @Override public void onStatusChanged(QtQmlStatus qtQmlStatus)
    {
        if (qtQmlStatus == QtQmlStatus.READY)
            registerSignals();
        else if (qtQmlStatus == QtQmlStatus.ERROR)
            finish(); // Error loading QML tests, just exit the app
    }

    public TestView testView() { return m_testView; }

    public static TestActivity instance() { return m_instance; }

    public void registerSignals()
    {
        m_signalListenerIds.add(m_testView.connectBasicSignalListener(
                (String unused, Void _unused) -> onBasicSignal()));
        m_signalListenerIds.add(m_testView.connectIntSignalListener(
                (String unused, Integer value) -> onIntSignal(value)));
        m_signalListenerIds.add(m_testView.connectBoolSignalListener(
                (String unused, Boolean value) -> onBoolSignal(value)));
        m_signalListenerIds.add(m_testView.connectDoubleSignalListener(
                (String unused, Double value) -> onDoubleSignal(value)));
        m_signalListenerIds.add(m_testView.connectStringSignalListener(
                (String unused, String value) -> onStringSignal(value)));
        m_signalListenerIds.add(m_testView.connectManyTypeArgSignalListener(
                (Integer i, Boolean b, Double d, String s) -> onManyTypeSignal(i, b, d, s)));
    }

    public void unregisterSignals()
    {
        for (Integer id : m_signalListenerIds)
            m_testView.disconnectSignalListener(id.intValue());
    }
}
