package com.fluxup.app;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.Locale;

public class InicioFragment extends Fragment {

    private TextView tvTimer;
    private FrameLayout timerBlock;
    private LinearLayout tasksContainer;
    private CountDownTimer countDownTimer;
    private boolean isTimerRunning = false;
    private long timeLeftInMillis = 25 * 60 * 1000; // 25 minutos

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_inicio, container, false);

        tvTimer = view.findViewById(R.id.tvTimer);
        timerBlock = view.findViewById(R.id.timerBlock);
        tasksContainer = view.findViewById(R.id.tasksContainer);

        addSampleTasks();

        timerBlock.setOnClickListener(v -> {
            if (!isTimerRunning) {
                startTimer();
            } else {
                pauseTimer();
            }
        });

        view.findViewById(R.id.btnAddTasks).setOnClickListener(v -> {
            Toast.makeText(getContext(), "Adicionar tarefas: Implementação futura", Toast.LENGTH_SHORT).show();
        });

        updateCountDownText();

        return view;
    }

    private void addSampleTasks() {
        if(getContext() == null) return;
        String[] sampleTasks = {
            "Estudar para intermédio",
            "Ler artigo técnico",
            "Fazer exercícios"
        };
        tasksContainer.removeAllViews();
        for (String taskLabel : sampleTasks) {
            CheckBox cb = new CheckBox(getContext());
            cb.setText(taskLabel);
            cb.setTextColor(getResources().getColor(R.color.black));
            tasksContainer.addView(cb);
        }
    }

    private void startTimer() {
        countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftInMillis = millisUntilFinished;
                updateCountDownText();
            }

            @Override
            public void onFinish() {
                isTimerRunning = false;
                if(getContext() != null) Toast.makeText(getContext(), "Foco concluído!", Toast.LENGTH_LONG).show();
            }
        }.start();

        isTimerRunning = true;
    }

    private void pauseTimer() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        isTimerRunning = false;
    }

    private void updateCountDownText() {
        int minutes = (int) (timeLeftInMillis / 1000) / 60;
        int seconds = (int) (timeLeftInMillis / 1000) % 60;

        String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
        if(tvTimer != null) tvTimer.setText(timeLeftFormatted);
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        pauseTimer(); // Parar o timer se a view for destruída
    }
}
