import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krezus/domains/charts/chart_bloc.dart';
import 'package:krezus/pages/stats/widgets/account_counters.dart';
import 'package:krezus/pages/stats/widgets/categories_totals_chart.dart';
import 'package:krezus/pages/stats/widgets/global_counter.dart';
import 'package:krezus/pages/stats/widgets/monthly_categories_total_chart.dart';
import 'package:krezus/pages/stats/widgets/global_total_chart.dart';
import 'package:krezus/pages/stats/widgets/profit_indicator.dart';
import 'package:krezus/pages/stats/widgets/year_selector.dart';

class StatsPage extends StatelessWidget {
  const StatsPage({super.key});

  Widget _largeScreenHeader(ChartState state) {
    return Center (
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
        constraints: const BoxConstraints(
          maxWidth: 1000
        ),
        child: Column(
          children: [
            Row(
              children: [
                Expanded(
                  flex: 2,
                  child: GlobalCounter(value: state.globalTotal)
                ),
                const SizedBox(width: 10),
                Expanded(
                  flex: 1,
                  child: AccountCounter(accountsTotals: state.accountsTotals)
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const YearSelector(),
                ProfitIndicator(profit: state.scoppedProfit)
              ],
            ),
          ]
        )
      )
    );
  }

  Widget _smallScreenHeader(ChartState state) {
    return Center (
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
        constraints: const BoxConstraints(
          maxWidth: 1000
        ),
        child: Column(
          children: [
            GlobalCounter(value: state.globalTotal),
            AccountCounter(accountsTotals: state.accountsTotals),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                ProfitIndicator(profit: state.scoppedProfit),
                const YearSelector(),
              ],
            ),
          ]
        )
      )
    );
  }

  Widget _largeScreenTotalsCharts(ChartState state) {
    return Center (
      child: ConstrainedBox(
        constraints: const BoxConstraints(
          maxWidth: 1500
        ),
        child: SizedBox(
          height: 450,
          child: Row(
            children: [
              CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesPositiveTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesPositiveTotalsPercents),
              CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesNegativeTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesNegativeTotalsPercents),
            ],
          )
        )
      )
    );
  }

  Widget _smallScreenTotalsCharts(ChartState state) {
    return Center (
      child: ConstrainedBox(
        constraints: const BoxConstraints(
          maxWidth: 1500
        ),
        child: Column(
          children: [
            CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesPositiveTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesPositiveTotalsPercents),
            CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesNegativeTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesNegativeTotalsPercents),
          ],
        )
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    MediaQueryData mediaQuery = MediaQuery.of(context);
    bool smallVerticalScreen = MediaQuery.sizeOf(context).width < 800;
    return BlocBuilder<ChartBloc, ChartState>(
      builder: (context, state) => ListView(
        padding: mediaQuery.padding.copyWith(bottom: 100.0),
        children: [
          smallVerticalScreen ? _smallScreenHeader(state) : _largeScreenHeader(state),
          SizedBox(
            height: smallVerticalScreen ? 100 : 200,
            child: GlobalTotalChart(monthlyTotals: state.scopedMonthlyTotals)
          ),
          SizedBox(
            height: smallVerticalScreen ? 200 : 500,
            child: MonthlyCategoriesTotalChart(categoriesMonthlyTotals: state.scopedCategoriesMonthlyTotals)
          ),
          smallVerticalScreen ? _smallScreenTotalsCharts(state) : _largeScreenTotalsCharts(state),
        ],
      )
    );
  }
}