import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:krezus/domains/category/category_bloc.dart';
import 'package:krezus/domains/charts/models/chart_item.dart';

class CategoriesTotalsChart extends StatelessWidget {
  final List<ChartItem> categoriesTotals;
  final List<ChartItem> categoriesTotalsPercents;

  const CategoriesTotalsChart({super.key, required this.categoriesTotals, required this.categoriesTotalsPercents});

  List<PieChartSectionData> _convertDataForChart(Map<String, Color> categoriesColors, bool smallVerticalScreen) {
    return categoriesTotalsPercents
      .map((item) => 
        PieChartSectionData(
          value: item.value,
          title: NumberFormat("#0 %").format(item.value / 100),
          titleStyle: const TextStyle(
            fontSize: 15,
            fontWeight: FontWeight.w300
          ),
          showTitle: !smallVerticalScreen,
          titlePositionPercentageOffset: 0.5,
          borderSide: const BorderSide(width: 0),
          radius: smallVerticalScreen ? 30 : 40,
          color: categoriesColors[item.label]
        ))
      .toList();
  }

  List<Row> _computeLegend(Map<String, Color> categoriesColors) {
    return categoriesTotals
      .map((item) => Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Container(
                height: 10,
                width: 10,
                decoration: BoxDecoration(
                  color: categoriesColors[item.label],
                  borderRadius: BorderRadius.circular(15)
                ),
              ),
              Container(width: 5),
              Text(item.label),
            ],
          ),
          Text(
            NumberFormat("#00 €").format(item.value.abs()),
            style: const TextStyle(
              fontFamily: 'NovaMono',
            )
          )
        ],
      )).toList();
  }

  @override
  Widget build(BuildContext context) {
    bool smallVerticalScreen = MediaQuery.sizeOf(context).width < 800;
    return BlocBuilder<CategoryBloc, CategoryState>(
      builder: (context, state) => Container(
        height: 320,
        width: smallVerticalScreen ? null : 500,
        padding: const EdgeInsets.all(10),
        margin: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          color: Theme.of(context).colorScheme.primaryContainer,
        ),
        child: Row(
          children: [
            Expanded(
              child: PieChart(
                PieChartData(
                  sections: _convertDataForChart(state.categoriesColors, smallVerticalScreen),
                  borderData: FlBorderData(
                    show: false
                  ),
                  centerSpaceRadius: smallVerticalScreen ? 30 :50,
                  sectionsSpace: 4
                )
              ),
            ),
            Container(
              height: 300,
              width: 250,
              padding: const EdgeInsets.all(10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: Theme.of(context).colorScheme.secondaryContainer
              ),
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: _computeLegend(state.categoriesColors),
                ),
              ) 
            ),
          ],
        ),
      )
    );
  }
}