package ml.docilealligator.infinityforreddit.activities;

import static ml.docilealligator.infinityforreddit.utils.UtilsKt.getChromeCustomTabPackageName;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.browser.customtabs.CustomTabColorSchemeParams;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.snackbar.Snackbar;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

import javax.inject.Inject;
import javax.inject.Named;

import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.account.FetchMyInfo;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.asynctasks.ParseAndInsertNewAccount;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.databinding.ActivityLoginChromeCustomTabBinding;
import ml.docilealligator.infinityforreddit.events.NewUserLoggedInEvent;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class LoginChromeCustomTabActivity extends BaseActivity {

    @Inject
    @Named("no_oauth")
    Retrofit mRetrofit;
    @Inject
    @Named("oauth")
    Retrofit mOauthRetrofit;
    @Inject
    RedditDataRoomDatabase mRedditDataRoomDatabase;
    @Inject
    @Named("default")
    SharedPreferences mSharedPreferences;
    @Inject
    @Named("current_account")
    SharedPreferences mCurrentAccountSharedPreferences;
    @Inject
    CustomThemeWrapper mCustomThemeWrapper;
    @Inject
    Executor mExecutor;
    private ActivityLoginChromeCustomTabBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ((Infinity) getApplication()).getAppComponent().inject(this);

        setImmersiveModeNotApplicableBelowAndroid16();

        super.onCreate(savedInstanceState);
        binding = ActivityLoginChromeCustomTabBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        applyCustomTheme();

        if (isImmersiveInterfaceRespectForcedEdgeToEdge()) {
            ViewCompat.setOnApplyWindowInsetsListener(binding.getRoot(), new OnApplyWindowInsetsListener() {
                @NonNull
                @Override
                public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
                    Insets allInsets = Utils.getInsets(insets, false, isForcedImmersiveInterface());

                    setMargins(binding.toolbarLoginChromeCustomTabActivity,
                            allInsets.left,
                            allInsets.top,
                            allInsets.right,
                            BaseActivity.IGNORE_MARGIN);

                    return WindowInsetsCompat.CONSUMED;
                }
            });
        }

        setSupportActionBar(binding.toolbarLoginChromeCustomTabActivity);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
        if (intent != null && intent.getData() != null) {
            handleRedirectUri(intent.getData());
        } else {
            checkAndOpenLoginPage();
        }

        binding.openWebpageButtonLoginChromeCustomTabActivity.setOnClickListener(view -> {
            checkAndOpenLoginPage();
        });
    }

    @Override
    protected void onNewIntent(@NonNull Intent intent) {
        super.onNewIntent(intent);

        Uri uri = intent.getData();
        if (uri == null) {
            binding.openWebpageButtonLoginChromeCustomTabActivity.setVisibility(View.VISIBLE);
            return;
        }

        handleRedirectUri(uri);
    }

    @Override
    public SharedPreferences getDefaultSharedPreferences() {
        return mSharedPreferences;
    }

    @Override
    public SharedPreferences getCurrentAccountSharedPreferences() {
        return mCurrentAccountSharedPreferences;
    }

    @Override
    public CustomThemeWrapper getCustomThemeWrapper() {
        return mCustomThemeWrapper;
    }

    @Override
    protected void applyCustomTheme() {
        binding.getRoot().setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
        applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(binding.appbarLayoutLoginChromeCustomTabActivity, null, binding.toolbarLoginChromeCustomTabActivity);
        binding.openWebpageButtonLoginChromeCustomTabActivity.setTextColor(mCustomThemeWrapper.getButtonTextColor());
        binding.openWebpageButtonLoginChromeCustomTabActivity.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme());
        if (typeface != null) {
            binding.openWebpageButtonLoginChromeCustomTabActivity.setTypeface(typeface);
        }
    }

    private void checkAndOpenLoginPage() {
        String packageName = getChromeCustomTabPackageName(this);
        if (packageName == null) {
            Toast.makeText(this, R.string.login_chrome_required, Toast.LENGTH_SHORT).show();
        } else {
            openLoginPage(packageName);
        }
    }

    private void openLoginPage(@NonNull String packageName) {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        // add share action to menu list
        builder.setShareState(CustomTabsIntent.SHARE_STATE_ON);
        builder.setDefaultColorSchemeParams(
                new CustomTabColorSchemeParams.Builder()
                        .setToolbarColor(mCustomThemeWrapper.getColorPrimary())
                        .build());
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.intent.setPackage(packageName);
        customTabsIntent.intent.putExtra("com.google.android.apps.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB", true);

        try {
            Uri.Builder uriBuilder = Uri.parse(APIUtils.OAUTH_URL).buildUpon();
            uriBuilder.appendQueryParameter(APIUtils.CLIENT_ID_KEY, APIUtils.CLIENT_ID);
            uriBuilder.appendQueryParameter(APIUtils.RESPONSE_TYPE_KEY, APIUtils.RESPONSE_TYPE);
            uriBuilder.appendQueryParameter(APIUtils.STATE_KEY, APIUtils.STATE);
            uriBuilder.appendQueryParameter(APIUtils.REDIRECT_URI_KEY, APIUtils.REDIRECT_URI);
            uriBuilder.appendQueryParameter(APIUtils.DURATION_KEY, APIUtils.DURATION);
            uriBuilder.appendQueryParameter(APIUtils.SCOPE_KEY, APIUtils.SCOPE);

            customTabsIntent.launchUrl(this, uriBuilder.build());
        } catch (ActivityNotFoundException e) {
            Snackbar.make(binding.getRoot(), R.string.custom_tab_not_available, Snackbar.LENGTH_LONG).show();
        }
    }

    private void handleRedirectUri(@NonNull Uri uri) {
        binding.openWebpageButtonLoginChromeCustomTabActivity.setVisibility(View.GONE);

        String authCode = uri.getQueryParameter("code");
        if (authCode != null) {
            String state = uri.getQueryParameter("state");
            if (APIUtils.STATE.equals(state)) {
                Map<String, String> params = new HashMap<>();
                params.put(APIUtils.GRANT_TYPE_KEY, "authorization_code");
                params.put("code", authCode);
                params.put(APIUtils.REDIRECT_URI_KEY, APIUtils.REDIRECT_URI);

                RedditAPI api = mRetrofit.create(RedditAPI.class);
                Call<String> accessTokenCall = api.getAccessToken(APIUtils.getHttpBasicAuthHeader(), params);
                accessTokenCall.enqueue(new Callback<>() {
                    @Override
                    public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                        if (response.isSuccessful()) {
                            try {
                                String accountResponse = response.body();
                                if (accountResponse == null) {
                                    //Handle error
                                    return;
                                }

                                JSONObject responseJSON = new JSONObject(accountResponse);
                                String accessToken = responseJSON.getString(APIUtils.ACCESS_TOKEN_KEY);
                                String refreshToken = responseJSON.getString(APIUtils.REFRESH_TOKEN_KEY);

                                FetchMyInfo.fetchAccountInfo(mExecutor, mHandler, mOauthRetrofit,
                                        mRedditDataRoomDatabase, accessToken,
                                        new FetchMyInfo.FetchMyInfoListener() {
                                            @Override
                                            public void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma, boolean isMod) {
                                                mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken)
                                                        .putString(SharedPreferencesUtils.ACCOUNT_NAME, name)
                                                        .putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, profileImageUrl).apply();
                                                mCurrentAccountSharedPreferences.edit().remove(SharedPreferencesUtils.SUBSCRIBED_THINGS_SYNC_TIME).apply();
                                                ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name, accessToken, refreshToken, profileImageUrl, bannerImageUrl,
                                                        karma, isMod, authCode, mRedditDataRoomDatabase.accountDao(),
                                                        () -> {
                                                            EventBus.getDefault().post(new NewUserLoggedInEvent());
                                                            finish();
                                                        });
                                            }

                                            @Override
                                            public void onFetchMyInfoFailed(boolean parseFailed) {
                                                if (parseFailed) {
                                                    Toast.makeText(LoginChromeCustomTabActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show();
                                                } else {
                                                    Toast.makeText(LoginChromeCustomTabActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
                                                }

                                                finish();
                                            }
                                        });
                            } catch (JSONException e) {
                                e.printStackTrace();
                                Toast.makeText(LoginChromeCustomTabActivity.this, R.string.parse_json_response_error, Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        } else {
                            Toast.makeText(LoginChromeCustomTabActivity.this, R.string.retrieve_token_error, Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
                        Toast.makeText(LoginChromeCustomTabActivity.this, R.string.retrieve_token_error, Toast.LENGTH_SHORT).show();
                        t.printStackTrace();
                        finish();
                    }
                });
            } else {
                Toast.makeText(this, R.string.something_went_wrong, Toast.LENGTH_SHORT).show();
                finish();
            }

        } else if ("access_denied".equals(uri.getQueryParameter("error"))) {
            Toast.makeText(this, R.string.access_denied, Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}