import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:krezus/domains/budget/budget_bloc.dart';
import 'package:krezus/domains/category/category_bloc.dart';
import 'package:krezus/repositories/metadata/models/budget.dart';
import 'package:krezus/repositories/metadata/models/category.dart';

class BudgetLine extends StatelessWidget {
  final Budget budget;

  const BudgetLine({super.key, required this.budget});

  Widget _largeScreenLine(BuildContext context, List<Category> categories, double initialBudget, double remainingBudget) {
    return Row (
      children: [
        Expanded(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: DropdownButtonFormField<String>(
                  value: budget.label,
                  onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetLabel(budget, value!)),
                  items: categories.map((e) => DropdownMenuItem(value: e.label, child: Text(e.label))).toList(),
                ),
              ),
              const SizedBox(width: 30),
              IconButton(
                onPressed: () => context.read<BudgetBloc>().add(BudgetSetValue(budget, budget.value.round().toDouble() - 1)),
                icon: const Icon(Icons.remove_circle)
              ),
              Text(
                NumberFormat('####000 €', 'fr_FR').format(budget.value),
                style: const TextStyle(
                  fontFamily: 'NovaMono',
                  fontWeight: FontWeight.bold,
                  fontSize: 15,
                )
              ),
              IconButton(
                onPressed: () => context.read<BudgetBloc>().add(BudgetSetValue(budget, budget.value.round().toDouble() + 1)),
                icon: const Icon(Icons.add_circle)
              ),
              const SizedBox(width: 5),
            ],
          )
        ),
        Expanded(
          child: Slider(
            min: 0,
            max: initialBudget,
            label: budget.value.round().toString(),
            value: budget.value,
            secondaryTrackValue: remainingBudget + budget.value,
            onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetValue(budget, value.round().toDouble())),
          ),
        ),
        IconButton(
          onPressed: () => context.read<BudgetBloc>().add(BudgetRemove(budget)),
          icon: const Icon(Icons.delete),
        ),
      ]
    );
  }

  Widget _smallScreenLine(BuildContext context, List<Category> categories, double initialBudget, double remainingBudget) {
    return Column(
      children: [
        Row(
          children: [
            Expanded(
              child: DropdownButtonFormField<String>(
                value: budget.label,
                onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetLabel(budget, value!)),
                items: categories.map((e) => DropdownMenuItem(value: e.label, child: Text(e.label))).toList(),
              ),
            ),
            const SizedBox(width: 30),
            Text(
              NumberFormat('####000 €', 'fr_FR').format(budget.value),
              style: const TextStyle(
                fontFamily: 'NovaMono',
                fontWeight: FontWeight.bold,
                fontSize: 15,
              )
            ),
            IconButton(
              onPressed: () => context.read<BudgetBloc>().add(BudgetSetValue(budget, budget.value.round().toDouble() - 1)),
              icon: const Icon(Icons.remove_circle)
            ),
            IconButton(
              onPressed: () => context.read<BudgetBloc>().add(BudgetSetValue(budget, budget.value.round().toDouble() + 1)),
              icon: const Icon(Icons.add_circle)
            ),
            IconButton(
              onPressed: () => context.read<BudgetBloc>().add(BudgetRemove(budget)),
              icon: const Icon(Icons.delete),
            ),
          ],
        ),
        Slider(
          min: 0,
          max: initialBudget,
          label: budget.value.round().toString(),
          value: budget.value,
          secondaryTrackValue: remainingBudget + budget.value,
          onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetValue(budget, value.round().toDouble())),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final categoryState = context.watch<CategoryBloc>().state;
    bool smallVerticalScreen = MediaQuery.sizeOf(context).width < 800;
    return BlocBuilder<BudgetBloc, BudgetState>(
      builder: (context, state) => smallVerticalScreen 
        ? _smallScreenLine(context, categoryState.categories, state.initialBudget, state.remainingBudget) 
        : _largeScreenLine(context, categoryState.categories, state.initialBudget, state.remainingBudget),
    );
  }
}