package com.foxx.androidcast;

/*********************************************************************
 * MainActivity.java
 * Created at: Fri 15 May 2026 16:49:02 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class MainActivity
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (7 commits, 97 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	c8972fea6ecc9a1dafb049d3acb1b2a3214e6d407444d6d48f216bb16a588d70
**********************************************************************/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;

import com.foxx.androidcast.dev.DevInstallWakeHelper;
import com.foxx.androidcast.ota.OtaManifest;
import com.foxx.androidcast.ota.OtaUpdateCoordinator;
import com.foxx.androidcast.ota.OtaUpdateService;
import com.foxx.androidcast.receiver.ReceiverPlaybackActivity;
import com.foxx.androidcast.sender.SenderActivity;
import com.foxx.androidcast.stats.SessionStatsStore;

public class MainActivity extends DrawerHostActivity {
    private final BroadcastReceiver otaUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (OtaUpdateService.ACTION_UPDATE_AVAILABLE.equals(intent.getAction())) {
                String json = intent.getStringExtra(OtaUpdateService.EXTRA_MANIFEST_JSON);
                String sourceUrl = intent.getStringExtra(OtaUpdateService.EXTRA_SOURCE_URL);
                boolean trustedSource = intent.getBooleanExtra(
                        OtaUpdateService.EXTRA_TRUSTED_SOURCE,
                        false);
                OtaManifest manifest = OtaManifest.fromJsonString(json);
                if (manifest != null && !isFinishing()) {
                    OtaUpdateCoordinator.presentUpdateDialog(
                            MainActivity.this,
                            manifest,
                            sourceUrl != null ? sourceUrl : "",
                            trustedSource);
                }
            }
        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (DevInstallWakeHelper.shouldShowAfterDeploy(getIntent())) {
            DevInstallWakeHelper.prepareActivityShowOnDeploy(this);
        }
        super.onCreate(savedInstanceState);
        if (AppPreferences.isGrabSessionStats(this)) {
            SessionStatsStore.pruneOnAppStart(this);
        }
        com.foxx.androidcast.network.CastTransportFactory.init(this);
        PermissionHelper.request(this);

        findViewById(R.id.btn_send).setOnClickListener(v ->
                startActivity(new Intent(this, SenderActivity.class)));
        findViewById(R.id.btn_receive).setOnClickListener(v -> {
            Intent i = new Intent(this, ReceiverPlaybackActivity.class);
            i.putExtra(ReceiverPlaybackActivity.EXTRA_AUTO_START_LISTENING, true);
            startActivity(i);
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter(OtaUpdateService.ACTION_UPDATE_AVAILABLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            registerReceiver(otaUpdateReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
        } else {
            registerReceiver(otaUpdateReceiver, filter);
        }
    }

    @Override
    protected void onStop() {
        unregisterReceiver(otaUpdateReceiver);
        super.onStop();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (DevInstallWakeHelper.shouldShowAfterDeploy(intent)) {
            DevInstallWakeHelper.prepareActivityShowOnDeploy(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (DevInstallWakeHelper.shouldShowAfterDeploy(getIntent())) {
            DevInstallWakeHelper.prepareActivityShowOnDeploy(this);
        }
        PermissionHelper.remindIfMissing(this, false);
        OtaUpdateCoordinator.requestCheckOnLaunch(this);
    }

    @Override
    protected void onDestroy() {
        if (isFinishing() && DevInstallWakeHelper.shouldShowAfterDeploy(getIntent())) {
            DevInstallWakeHelper.releaseWakeLock();
        }
        super.onDestroy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
