package com.example.todojava;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.todojava.data.Todo;
import com.example.todojava.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private TodoViewModel viewModel;
    private TodoAdapter adapter;

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

        setSupportActionBar(binding.toolbar);

        viewModel = new ViewModelProvider(this).get(TodoViewModel.class);

        setupRecyclerView();
        setupFilterChips();
        setupFab();
        observeData();
    }

    private void setupRecyclerView() {
        adapter = new TodoAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);

        adapter.setOnTodoClickListener(todo -> {
            TodoFormDialogFragment dialog = TodoFormDialogFragment.newInstance(todo);
            dialog.show(getSupportFragmentManager(), "edit_todo");
        });

        adapter.setOnTodoCheckedListener(todo -> viewModel.toggleCompleted(todo));

        // Swipe to delete
        SwipeToDeleteCallback swipeCallback = new SwipeToDeleteCallback(position -> {
            Todo todo = adapter.getCurrentList().get(position);
            viewModel.delete(todo);
            Snackbar.make(binding.getRoot(), R.string.todo_deleted, Snackbar.LENGTH_LONG)
                    .setAction(R.string.undo, v -> viewModel.insert(todo))
                    .show();
        });
        new ItemTouchHelper(swipeCallback).attachToRecyclerView(binding.recyclerView);
    }

    private void setupFilterChips() {
        binding.chipAll.setOnClickListener(v -> viewModel.setFilter(TodoViewModel.Filter.ALL));
        binding.chipActive.setOnClickListener(v -> viewModel.setFilter(TodoViewModel.Filter.ACTIVE));
        binding.chipDone.setOnClickListener(v -> viewModel.setFilter(TodoViewModel.Filter.DONE));
    }

    private void setupFab() {
        binding.fab.setOnClickListener(v -> {
            TodoFormDialogFragment dialog = TodoFormDialogFragment.newInstance();
            dialog.show(getSupportFragmentManager(), "add_todo");
        });
    }

    private void observeData() {
        viewModel.getFilteredTodos().observe(this, todos -> {
            adapter.submitList(todos);
            updateEmptyState(todos);
        });

        // Update chip text with counts
        viewModel.getAllCount().observe(this, count -> {
            String text = getString(R.string.filter_all) + " (" + count + ")";
            binding.chipAll.setText(text);
        });

        viewModel.getActiveCount().observe(this, count -> {
            String text = getString(R.string.filter_active) + " (" + count + ")";
            binding.chipActive.setText(text);
        });

        viewModel.getCompletedCount().observe(this, count -> {
            String text = getString(R.string.filter_done) + " (" + count + ")";
            binding.chipDone.setText(text);
        });
    }

    private void updateEmptyState(List<Todo> todos) {
        boolean isEmpty = todos == null || todos.isEmpty();
        binding.emptyState.getRoot().setVisibility(isEmpty ? View.VISIBLE : View.GONE);
        binding.recyclerView.setVisibility(isEmpty ? View.GONE : View.VISIBLE);

        if (isEmpty) {
            TodoViewModel.Filter filter = viewModel.getCurrentFilter().getValue();
            if (filter == null) filter = TodoViewModel.Filter.ALL;

            View emptyRoot = binding.emptyState.getRoot();
            android.widget.TextView titleView = emptyRoot.findViewById(R.id.emptyTitle);
            android.widget.TextView subtitleView = emptyRoot.findViewById(R.id.emptySubtitle);

            switch (filter) {
                case ACTIVE:
                    titleView.setText(R.string.empty_active_title);
                    subtitleView.setText(R.string.empty_active_subtitle);
                    break;
                case DONE:
                    titleView.setText(R.string.empty_done_title);
                    subtitleView.setText(R.string.empty_done_subtitle);
                    break;
                default:
                    titleView.setText(R.string.empty_state_title);
                    subtitleView.setText(R.string.empty_state_subtitle);
                    break;
            }
        }
    }
}
