import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krezus/domains/account/account_bloc.dart';
import 'package:krezus/domains/budget/budget_bloc.dart';
import 'package:krezus/domains/category/category_bloc.dart';
import 'package:krezus/domains/charts/chart_bloc.dart';
import 'package:krezus/domains/settings/settings_bloc.dart';
import 'package:krezus/domains/transaction/transaction_bloc.dart';
import 'package:krezus/pages/budgets/budgets_page.dart';
import 'package:krezus/pages/data/data_page.dart';
import 'package:krezus/pages/stats/stats_page.dart';
import 'package:krezus/pages/transactions/transactions_page.dart';
import 'package:krezus/repositories/metadata/metadata_repository.dart';
import 'package:krezus/repositories/transactions/transactions_repository.dart';

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

  Widget _tabBar() {
    return TabBar(
      tabAlignment: TabAlignment.center,
      splashBorderRadius: BorderRadius.circular(25),
      tabs: const [
        Tab(icon: Icon(Icons.insights)),
        Tab(icon: Icon(Icons.receipt_long)),
        Tab(icon: Icon(Icons.pie_chart)),
        Tab(icon: Icon(Icons.settings)),
      ],
    );
  }

  Widget _largeScreenTotalsCharts(BuildContext context) {
    return Align(
      alignment: Alignment.topLeft,
      child: Container(
        margin: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.primaryContainer,
          borderRadius: BorderRadius.circular(5),
        ),
        child: TabBar(
          tabAlignment: TabAlignment.center,
          splashBorderRadius: BorderRadius.circular(25),
          tabs: const [
            Tab(icon: Icon(Icons.insights)),
            Tab(icon: Icon(Icons.receipt_long)),
            Tab(icon: Icon(Icons.pie_chart)),
            Tab(icon: Icon(Icons.settings)),
          ],
        ),
      ),
    );
  }

  Widget _smallScreenTotalsCharts(BuildContext context) {
    return Align(
      alignment: Alignment.bottomCenter,
      child: Container(
        height: 40,
        width: MediaQuery.sizeOf(context).width,
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.primaryContainer,
          boxShadow: [
              BoxShadow(
                color: Theme.of(context).colorScheme.shadow,
                blurRadius: 5,
                offset: const Offset(0, -2),
                spreadRadius: 0.1,
                blurStyle: BlurStyle.normal,
              )
            ]
        ),
        child: Center(
          child:TabBar(
            tabAlignment: TabAlignment.center,
            splashBorderRadius: BorderRadius.circular(25),
            tabs: const [
              Tab(icon: Icon(Icons.insights)),
              Tab(icon: Icon(Icons.receipt_long)),
              Tab(icon: Icon(Icons.pie_chart)),
              Tab(icon: Icon(Icons.settings)),
            ],
          )
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    bool smallVerticalScreen = MediaQuery.sizeOf(context).width < 1500;
    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (context) => AccountBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
        BlocProvider(create: (context) => TransactionBloc(transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
        BlocProvider(create: (context) => CategoryBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
        BlocProvider(create: (context) => BudgetBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
        BlocProvider(create: (context) => ChartBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
        BlocProvider(create: (context) => SettingsBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context))),
      ],
      child: DefaultTabController(
        length: 4,
        child: Scaffold(
          body: Stack(
            children: [
              const TabBarView(
                children: [
                  StatsPage(),
                  TransactionsPage(),
                  BudgetsPage(),
                  DataPage()
                ],
              ),
              smallVerticalScreen ? _smallScreenTotalsCharts(context) : _largeScreenTotalsCharts(context),
            ],
          ),
        )
      )
    );
  }
}