package com.example.finzora;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.finzora.MainActivity;

public class OnboardingActivity extends AppCompatActivity {

    private ViewPager2 viewPager;
    private Button btnSkip, btnNext;
    private ImageButton btnPrev;
    private LinearLayout dotsLayout;
    private int currentPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_onboarding);

        // Inicializar views
        viewPager = findViewById(R.id.viewPager);
        btnSkip = findViewById(R.id.btnSkip);
        btnNext = findViewById(R.id.btnNext);
        btnPrev = findViewById(R.id.btnPrev);
        dotsLayout = findViewById(R.id.dotsLayout);

        // Configurar ViewPager
        OnboardingAdapter adapter = new OnboardingAdapter();
        viewPager.setAdapter(adapter);

        // Configurar dots indicadores
        setupDots();

        // Configurar botões
        setupButtons();

        // Ouvir mudanças de página
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                currentPage = position;
                updateDots(position);
                updateButtons(position);
            }
        });
    }

    private void setupDots() {
        dotsLayout.removeAllViews();

        for (int i = 0; i < 5; i++) {
            View dot = new View(this);
            dot.setBackgroundColor(Color.GRAY);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(16, 16);
            params.setMargins(8, 0, 8, 0);
            dot.setLayoutParams(params);

            dotsLayout.addView(dot);
        }

        updateDots(0);
    }

    private void updateDots(int position) {
        for (int i = 0; i < dotsLayout.getChildCount(); i++) {
            View dot = dotsLayout.getChildAt(i);
            if (i == position) {
                dot.setBackgroundColor(Color.parseColor("#2196F3")); // Azul Finzora
                dot.animate().scaleX(1.5f).scaleY(1.5f).setDuration(200).start();
            } else {
                dot.setBackgroundColor(Color.GRAY);
                dot.animate().scaleX(1f).scaleY(1f).setDuration(200).start();
            }
        }
    }

    private void setupButtons() {
        btnSkip.setOnClickListener(v -> skipTutorial());
        btnNext.setOnClickListener(v -> nextPage());
        btnPrev.setOnClickListener(v -> previousPage());
    }

    private void updateButtons(int position) {
        if (position == 4) { // Última página
            btnNext.setText("COMEÇAR");
            btnPrev.setVisibility(View.VISIBLE);
            btnSkip.setVisibility(View.GONE);
        } else if (position == 0) { // Primeira página
            btnNext.setText("PRÓXIMO");
            btnPrev.setVisibility(View.INVISIBLE);
            btnSkip.setVisibility(View.VISIBLE);
        } else { // Páginas do meio
            btnNext.setText("PRÓXIMO");
            btnPrev.setVisibility(View.VISIBLE);
            btnSkip.setVisibility(View.VISIBLE);
        }
    }

    private void nextPage() {
        if (currentPage < 4) {
            viewPager.setCurrentItem(currentPage + 1, true);
        } else {
            startMainApp();
        }
    }

    private void previousPage() {
        if (currentPage > 0) {
            viewPager.setCurrentItem(currentPage - 1, true);
        }
    }

    private void skipTutorial() {
        startMainApp();
    }

    private void startMainApp() {
        Intent intent = new Intent(OnboardingActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @SuppressLint("GestureBackNavigation")
    @Override
    public void onBackPressed() {
        // O super DEVE ser a primeira linha
        super.onBackPressed();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}