package com.foxx.androidcast;

/*********************************************************************
 * CastSettingsActivity.java
 * Created at: Sun 17 May 2026 18:17:14 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class CastSettingsActivity
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 73 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	1d58e0ba44d2692f0f925163c929f836de05cd995fe631bf9f9777a46238ebff
**********************************************************************/
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.foxx.androidcast.receiver.ReceiverPrefs;

/** Global, sender, and receiver default cast settings. */
public class CastSettingsActivity extends AppCompatActivity {
    private final CastSettings senderSettings = new CastSettings();
    private final CastSettings receiverSettings = new CastSettings();

    private EditText usernameInput;
    private EditText pinInput;
    private CheckBox trayIconCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cast_settings);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        usernameInput = findViewById(R.id.edit_username);
        pinInput = findViewById(R.id.edit_settings_pin);
        trayIconCheck = findViewById(R.id.check_tray_icon);

        senderSettings.copyFrom(AppPreferences.loadSenderDefaults(this));
        receiverSettings.copyFrom(AppPreferences.loadReceiverDefaults(this));

        CastSettingsBinder.bindGlobal(this, usernameInput, pinInput, trayIconCheck);
        CastSettingsBinder.bindSender(this, senderSettings);
        CastSettingsBinder.bindReceiver(this, receiverSettings);
    }

    @Override
    protected void onResume() {
        super.onResume();
        CastActivityTracker.onResume(this);
    }

    @Override
    protected void onPause() {
        saveAll();
        super.onPause();
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }

    private void saveAll() {
        CastSettingsBinder.saveGlobal(this, usernameInput, pinInput, trayIconCheck);
        CastSettingsBinder.saveSender(this, senderSettings);
        CastSettingsBinder.saveReceiver(this, receiverSettings);
        ReceiverPrefs.setAllowAudio(this, AppPreferences.isPlayIncomingAudio(this));
        Toast.makeText(this, R.string.settings_saved, Toast.LENGTH_SHORT).show();
    }
}
