package eu.toldi.infinityforlemmy.adapters.navigationdrawer;

import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG;
import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;

import java.util.concurrent.Executor;

import butterknife.BindView;
import butterknife.ButterKnife;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.activities.BaseActivity;
import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import pl.droidsonroids.gif.GifImageView;

public class HeaderSectionRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private BaseActivity baseActivity;
    private CustomThemeWrapper customThemeWrapper;
    private Resources resources;
    private RequestManager glide;
    private String accountName;
    private String accountQualifiedName;
    private String profileImageUrl;
    private String bannerImageUrl;
    private int karma;
    private boolean requireAuthToAccountSection;
    private boolean showAvatarOnTheRightInTheNavigationDrawer;
    private boolean isLoggedIn;
    private boolean isInMainPage = true;
    private PageToggle pageToggle;
    private boolean hideKarma;

    public HeaderSectionRecyclerViewAdapter(BaseActivity baseActivity, CustomThemeWrapper customThemeWrapper,
                                            RequestManager glide, String accountName, String accountQualifiedName,
                                            SharedPreferences sharedPreferences,
                                            SharedPreferences navigationDrawerSharedPreferences,
                                            SharedPreferences securitySharedPreferences,
                                            PageToggle pageToggle) {
        this.baseActivity = baseActivity;
        this.customThemeWrapper = customThemeWrapper;
        resources = baseActivity.getResources();
        this.glide = glide;
        this.accountName = accountName;
        isLoggedIn = accountName != null;
        this.pageToggle = pageToggle;
        this.accountQualifiedName = accountQualifiedName;
        requireAuthToAccountSection = securitySharedPreferences.getBoolean(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER, false);
        showAvatarOnTheRightInTheNavigationDrawer = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_AVATAR_ON_THE_RIGHT, false);
        showAvatarOnTheRightInTheNavigationDrawer = navigationDrawerSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_AVATAR_ON_THE_RIGHT, false);
        this.hideKarma = true;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new NavHeaderViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.nav_header_main, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof NavHeaderViewHolder) {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ((NavHeaderViewHolder) holder).profileImageView.getLayoutParams();
            if (showAvatarOnTheRightInTheNavigationDrawer) {
                params.addRule(RelativeLayout.ALIGN_PARENT_END);
            } else {
                params.removeRule(RelativeLayout.ALIGN_PARENT_END);
            }
            ((NavHeaderViewHolder) holder).profileImageView.setLayoutParams(params);
            if (isLoggedIn) {

                ((NavHeaderViewHolder) holder).karmaTextView.setVisibility(View.VISIBLE);
                ((NavHeaderViewHolder) holder).karmaTextView.setText(accountQualifiedName);
                ((NavHeaderViewHolder) holder).accountNameTextView.setTranslationY(0);

                ((NavHeaderViewHolder) holder).accountNameTextView.setText(accountName);
                if (profileImageUrl != null && !profileImageUrl.equals("")) {
                    glide.load(profileImageUrl)
                            .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0)))
                            .error(glide.load(R.drawable.subreddit_default_icon)
                                    .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0))))
                            .into(((NavHeaderViewHolder) holder).profileImageView);
                } else {
                    glide.load(R.drawable.subreddit_default_icon)
                            .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0)))
                            .into(((NavHeaderViewHolder) holder).profileImageView);
                }

                if (bannerImageUrl != null && !bannerImageUrl.equals("")) {
                    glide.load(bannerImageUrl).into(((NavHeaderViewHolder) holder).bannerImageView);
                }
            } else {
                ((NavHeaderViewHolder) holder).karmaTextView.setText(R.string.press_here_to_login);
                ((NavHeaderViewHolder) holder).accountNameTextView.setText(R.string.anonymous_account);
                glide.load(R.drawable.subreddit_default_icon)
                        .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0)))
                        .into(((NavHeaderViewHolder) holder).profileImageView);
            }

            if (isInMainPage) {
                ((NavHeaderViewHolder) holder).dropIconImageView.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_arrow_drop_down_24dp));
            } else {
                ((NavHeaderViewHolder) holder).dropIconImageView.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_arrow_drop_up_24dp));
            }

            holder.itemView.setOnClickListener(view -> {
                if (isInMainPage) {
                    if (requireAuthToAccountSection) {
                        BiometricManager biometricManager = BiometricManager.from(baseActivity);
                        if (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS) {
                            Executor executor = ContextCompat.getMainExecutor(baseActivity);
                            BiometricPrompt biometricPrompt = new BiometricPrompt(baseActivity,
                                    executor, new BiometricPrompt.AuthenticationCallback() {
                                @Override
                                public void onAuthenticationSucceeded(
                                        @NonNull BiometricPrompt.AuthenticationResult result) {
                                    super.onAuthenticationSucceeded(result);
                                    pageToggle.openAccountSection();
                                    openAccountSection(((NavHeaderViewHolder) holder).dropIconImageView);
                                }
                            });

                            BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                                    .setTitle(baseActivity.getString(R.string.unlock_account_section))
                                    .setAllowedAuthenticators(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)
                                    .build();

                            biometricPrompt.authenticate(promptInfo);
                        } else {
                            pageToggle.openAccountSection();
                            openAccountSection(((NavHeaderViewHolder) holder).dropIconImageView);
                        }
                    } else {
                        pageToggle.openAccountSection();
                        openAccountSection(((NavHeaderViewHolder) holder).dropIconImageView);
                    }
                } else {
                    ((NavHeaderViewHolder) holder).dropIconImageView.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_baseline_arrow_drop_down_24dp, null));
                    pageToggle.closeAccountSectionWithoutChangeIconResource();
                    closeAccountSectionWithoutChangeIconResource(false);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    private void openAccountSection(ImageView dropIconImageView) {
        dropIconImageView.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_baseline_arrow_drop_up_24dp, null));
        isInMainPage = false;
    }

    public boolean closeAccountSectionWithoutChangeIconResource(boolean checkIsInMainPage) {
        if (!(checkIsInMainPage && isInMainPage)) {
            isInMainPage = true;
            return true;
        }

        notifyItemChanged(0);
        return false;
    }

    public void updateAccountInfo(String profileImageUrl, String bannerImageUrl) {
        this.profileImageUrl = profileImageUrl;
        this.bannerImageUrl = bannerImageUrl;
        notifyItemChanged(0);
    }

    public void setRequireAuthToAccountSection(boolean requireAuthToAccountSection) {
        this.requireAuthToAccountSection = requireAuthToAccountSection;
    }

    public void setShowAvatarOnTheRightInTheNavigationDrawer(boolean showAvatarOnTheRightInTheNavigationDrawer) {
        this.showAvatarOnTheRightInTheNavigationDrawer = showAvatarOnTheRightInTheNavigationDrawer;
    }

    public void setHideKarma(boolean hideKarma) {
        this.hideKarma = hideKarma;
        notifyItemChanged(0);
    }

    class NavHeaderViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.name_text_view_nav_header_main)
        TextView accountNameTextView;
        @BindView(R.id.karma_text_view_nav_header_main)
        TextView karmaTextView;
        @BindView(R.id.profile_image_view_nav_header_main)
        GifImageView profileImageView;
        @BindView(R.id.banner_image_view_nav_header_main)
        ImageView bannerImageView;
        @BindView(R.id.account_switcher_image_view_nav_header_main)
        ImageView dropIconImageView;

        NavHeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            if (baseActivity.typeface != null) {
                accountNameTextView.setTypeface(baseActivity.typeface);
                karmaTextView.setTypeface(baseActivity.typeface);
            }

            itemView.setBackgroundColor(customThemeWrapper.getColorPrimary());
            accountNameTextView.setTextColor(customThemeWrapper.getToolbarPrimaryTextAndIconColor());
            karmaTextView.setTextColor(customThemeWrapper.getToolbarSecondaryTextColor());
            dropIconImageView.setColorFilter(customThemeWrapper.getToolbarPrimaryTextAndIconColor(), android.graphics.PorterDuff.Mode.SRC_IN);
        }
    }

    public interface PageToggle {
        void openAccountSection();
        void closeAccountSectionWithoutChangeIconResource();
    }
}
