import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:krow_domain/krow_domain.dart';
// ignore: depend_on_referenced_packages
import 'package:core_localization/core_localization.dart';

import '../blocs/documents/documents_cubit.dart';
import '../blocs/documents/documents_state.dart';
import '../navigation/documents_navigator.dart';
import '../widgets/document_card.dart';
import '../widgets/documents_progress_card.dart';

class DocumentsPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    final DocumentsCubit cubit = Modular.get<DocumentsCubit>();

    if (cubit.state.status == DocumentsStatus.initial) {
      cubit.loadDocuments();
    }

    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        leading: IconButton(
          icon: const Icon(UiIcons.arrowLeft, color: UiColors.iconSecondary),
          onPressed: () => Modular.to.pop(),
        ),
        title: Text(
          t.staff_documents.title,
          style: UiTypography.headline3m.copyWith(
            color: UiColors.textPrimary,
          ),
        ),
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(1.0),
          child: Container(color: UiColors.border, height: 1.0),
        ),
      ),
      body: BlocBuilder<DocumentsCubit, DocumentsState>(
        bloc: cubit,
        builder: (BuildContext context, DocumentsState state) {
          if (state.status == DocumentsStatus.loading) {
            return const Center(
              child: CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(UiColors.primary),
              ),
            );
          }
          if (state.status == DocumentsStatus.failure) {
            return Center(
              child: Text(
                t.staff_documents.list.error(
                  message: state.errorMessage ?? 'Unknown',
                ),
                style: UiTypography.body1m.copyWith(color: UiColors.textError),
              ),
            );
          }
          if (state.documents.isEmpty) {
            return Center(
              child: Text(
                t.staff_documents.list.empty,
                style: UiTypography.body1m.copyWith(color: UiColors.textSecondary),
              ),
            );
          }

          return ListView(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 24),
            children: <Widget>[
              DocumentsProgressCard(
                completedCount: state.completedCount,
                totalCount: state.totalCount,
                progress: state.progress,
              ),
              const SizedBox(height: 16),
              ...state.documents.map(
                (StaffDocument doc) => DocumentCard(
                  document: doc,
                  onTap: () => Modular.to.pushDocumentDetails(doc.id),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
