package de.danoeh.antennapod.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.DownloadRequest;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import org.apache.commons.lang3.Validate;

/**
 * Shows a username and a password text field.
 * The activity MUST be started with the ARG_DOWNlOAD_REQUEST argument set to a non-null value.
 */
public class DownloadAuthenticationActivity extends AppCompatActivity {

    /**
     * The download request object that contains information about the resource that requires a username and a password.
     */
    public static final String ARG_DOWNLOAD_REQUEST = "request";

    private EditText etxtUsername;
    private EditText etxtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getNoTitleTheme());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_authentication_activity);

        etxtUsername = findViewById(R.id.etxtUsername);
        etxtPassword = findViewById(R.id.etxtPassword);

        Validate.isTrue(getIntent().hasExtra(ARG_DOWNLOAD_REQUEST), "Download request missing");
        DownloadRequest request = getIntent().getParcelableExtra(ARG_DOWNLOAD_REQUEST);

        TextView txtvDescription = findViewById(R.id.txtvDescription);
        String newDescription = txtvDescription.getText() + ":\n\n" + request.getTitle();
        txtvDescription.setText(newDescription);

        if (savedInstanceState != null) {
            etxtUsername.setText(savedInstanceState.getString("username"));
            etxtPassword.setText(savedInstanceState.getString("password"));
        }

        findViewById(R.id.butConfirm).setOnClickListener(v -> {
            String username = etxtUsername.getText().toString();
            String password = etxtPassword.getText().toString();
            request.setUsername(username);
            request.setPassword(password);
            DownloadRequester.getInstance().download(DownloadAuthenticationActivity.this, request);
            finish();
        });

        findViewById(R.id.butCancel).setOnClickListener(v -> {
            setResult(Activity.RESULT_CANCELED);
            finish();
        });

    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("username", etxtUsername.getText().toString());
        outState.putString("password", etxtPassword.getText().toString());
    }
}

@@ -88,16 +88,47 @@
 Bundle;%0A
+import android.text.TextUtils;%0A
 import a
@@ -299,16 +299,120 @@
 apod.R;%0A
+import de.danoeh.antennapod.core.feed.FeedMedia;%0Aimport de.danoeh.antennapod.core.feed.FeedPreferences;%0A
 import d
@@ -574,32 +574,268 @@
 torage.D
-ownloadRequester
+BReader;%0Aimport de.danoeh.antennapod.core.storage.DBWriter;%0Aimport de.danoeh.antennapod.core.storage.DownloadRequester;%0Aimport io.reactivex.Completable;%0Aimport io.reactivex.android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.schedulers.Schedulers
 ;%0Aimport
@@ -871,16 +871,17 @@
 idate;%0A%0A
+%0A
 /**%0A * S
@@ -1368,16 +1368,53 @@
 assword;
+%0A    private DownloadRequest request;
 %0A%0A    @O
@@ -1861,32 +1861,16 @@
         
-DownloadRequest 
 request 
@@ -2395,34 +2395,32 @@
 ickListener(v -%3E
- %7B
 %0A            Str
@@ -2420,125 +2420,102 @@
     
-String username = etxtUsername.getText().toString();%0A            String password = etxtPassword.getText().toString();
+    Completable.fromAction(this::updatePassword)%0A                .subscribeOn(Schedulers.io())
 %0A   
@@ -2527,82 +2527,98 @@
     
-request.setUsername(username);%0A            request.setPassword(password);%0A
+    .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(() -%3E %7B%0A        
     
@@ -2717,32 +2717,40 @@
 t);%0A            
+        
 finish();%0A      
@@ -2743,33 +2743,42 @@
 nish();%0A        
-%7D
+        %7D)
 );%0A%0A        find
@@ -2915,24 +2915,912 @@
 %7D);%0A%0A    %7D%0A%0A
+    private void updatePassword() %7B%0A        String username = etxtUsername.getText().toString();%0A        String password = etxtPassword.getText().toString();%0A        request.setUsername(username);%0A        request.setPassword(password);%0A%0A        if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) %7B%0A            long mediaId = request.getFeedfileId();%0A            FeedMedia media = DBReader.getFeedMedia(mediaId);%0A            if (media != null) %7B%0A                FeedPreferences preferences = media.getItem().getFeed().getPreferences();%0A                if (TextUtils.isEmpty(preferences.getPassword()) %7C%7C TextUtils.isEmpty(preferences.getUsername())) %7B%0A                    preferences.setUsername(username);%0A                    preferences.setPassword(password);%0A                    DBWriter.setFeedPreferences(preferences);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A
     @Overrid

Store password when entering after failed download