package com.app.simplitend.faqs;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.app.simplitend.R;
import com.app.simplitend.databinding.ActivityFaqBinding;

import java.util.ArrayList;

public class FAQ_Activity extends AppCompatActivity implements FAQContracts.GETFAQsCallback {

    private ActivityFaqBinding binding;

    private FAQsPresenter presenter;

    private FAQAdapter faqAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFaqBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        presenter = FAQsPresenter.getFaqsPresenter();

        initViews();

        clickEvents();

        getFAQs();

    }

    @Override
    protected void attachBaseContext(Context newBase) {
        final Configuration configuration = new Configuration(newBase.getResources().getConfiguration());
        configuration.fontScale = Math.min(configuration.fontScale, 1.1f);
        applyOverrideConfiguration(configuration);
        super.attachBaseContext(newBase);
    }

    private void initViews() {
        binding.faqsRv.setLayoutManager(new LinearLayoutManager(this));
        faqAdapter = new FAQAdapter();
        binding.faqsRv.setAdapter(faqAdapter);
    }

    private void clickEvents() {
        binding.backBtn.setOnClickListener(v -> {
            onBackPressed();
        });
    }

    private void getFAQs() {
        binding.progressBar.setVisibility(View.VISIBLE);
        binding.noFaqs.setVisibility(View.GONE);

        presenter.getFAQs(this);
    }

    @Override
    public void onFAQsFetched(ArrayList<FAQResult> faqResults) {
        binding.progressBar.setVisibility(View.GONE);
        binding.noFaqs.setVisibility(View.GONE);

        if (faqResults.isEmpty()){
            binding.noFaqs.setVisibility(View.VISIBLE);
        }

        faqAdapter.submitList(faqResults);

    }

    @Override
    public void onFAQsFetchedFailed(Throwable t, String message) {
        binding.progressBar.setVisibility(View.GONE);
        binding.noFaqs.setVisibility(View.VISIBLE);

        Toast.makeText(this, "" + message, Toast.LENGTH_SHORT).show();
    }
}