import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:learning/Presentation/Controller/Novel/novel_cubit.dart';
import 'package:learning/Presentation/Resources/Fonts/style_manager.dart';

import '../Resources/Colors/color_manger.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<NovelCubit, NovelState>(
      builder: (context, chapterContentState) {
        if (chapterContentState is ChapterContentLoading) {
          return const Scaffold(
            body: Center(
              child: CircularProgressIndicator(),
            ),
          );
        } else if (chapterContentState is ChapterContentSuccessfullyLoaded) {
          return Scaffold(
            appBar: AppBar(
              title: Text(
                textAlign: TextAlign.center,
                chapterContentState.chapterContent.novelTitle,
              ),
              leading: IconButton(
                onPressed: () => Navigator.pop(context),
                icon: const Icon(
                  Icons.arrow_back,
                ),
              ),
            ),
            body: SingleChildScrollView(
              child: Column(
                children: [
                  Text(
                      "Chapter${chapterContentState.chapterContent.chapterNumber}"),
                  const SizedBox(
                    height: 30,
                  ),
                  Text(
                    textAlign: TextAlign.center,
                    chapterContentState.chapterContent.chapterTitle,
                    style: getHeadlineMediumStyle(
                      color: Theme.of(context).brightness == Brightness.light
                          ? ColorManger.blackColor
                          : ColorManger.whiteColor,
                    ),
                  ),
                  const SizedBox(
                    height: 20,
                  ),
                  NotificationListener<ScrollNotification>(
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(
                        textAlign: TextAlign.center,
                        chapterContentState.chapterContent.chapterContent,
                        style: getBodyMediumStyle(
                          color:
                              Theme.of(context).brightness == Brightness.light
                                  ? ColorManger.blackColor
                                  : ColorManger.whiteColor,
                        ),
                      ),
                    ),
                    onNotification: (scrollInformation) {
                      if (scrollInformation.metrics.pixels ==
                              scrollInformation.metrics.maxScrollExtent &&
                          scrollInformation is ScrollEndNotification) {
                        return showModalBottomSheet<void>(
                            context: context,
                            builder: (BuildContext context) {
                              return Container();
                            });
                      }
                      return true;
                    },
                  )
                ],
              ),
            ),
          );
        } else if (chapterContentState is ChapterContentError) {
          return Center(
            child: Text(chapterContentState.errorMessage),
          );
        }
        return const SizedBox.shrink();
      },
    );
  }
}