import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:initial_folder/models/section_model.dart';
import 'package:initial_folder/providers/selected_title_provider.dart';
import 'package:initial_folder/services/announcement_service.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:initial_folder/widgets/announcement_user_page.dart';
import 'package:provider/provider.dart';

class Announcement extends StatefulWidget {
  const Announcement({
    Key? key,
    required this.id,
    required this.lesonOper,
    required this.sectionOper,
    required this.lessonMapIdoper,
    required this.dataLessonOper,
  }) : super(key: key);

  final id;
  final lesonOper;
  final sectionOper;
  final lessonMapIdoper;
  final List<DataLesson> dataLessonOper;

  @override
  State<Announcement> createState() => _AnnouncementState();

  String? showSummary(String? selectedTitle) {
    String? selectedSummary = dataLessonOper
        .firstWhere((data) => data.title == selectedTitle,
            orElse: () => DataLesson(
                summary: '                        Tidak ada ringkasan'))
        .summary;

    return selectedSummary;
  }
}

class _AnnouncementState extends State<Announcement> {
  double value = 0;
  final _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    AnnouncementService().getAnnouncement(widget.id);
  }

  @override
  Widget build(BuildContext context) {
    final selectedTitle =
        Provider.of<SelectedTitleProvider>(context).selectedTitle;

    return SingleChildScrollView(
      child: GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              margin: EdgeInsets.only(
                left: getProportionateScreenWidth(16),
                right: getProportionateScreenWidth(16),
                top: getProportionateScreenWidth(16),
              ),
              child: Text(
                'Ringkasan Kursus',
                style: thirdTextStyle.copyWith(
                  fontWeight: semiBold,
                  letterSpacing: 1,
                  fontSize: getProportionateScreenWidth(14),
                ),
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.only(
                    left: getProportionateScreenWidth(16),
                    right: getProportionateScreenWidth(16),
                    top: getProportionateScreenWidth(10),
                  ),
                  child: Column(
                    children: [
                      if (widget.showSummary(selectedTitle) != "")
                        Text(
                          widget.showSummary(selectedTitle)!,
                          style: thirdTextStyle,
                        )
                      else
                        Center(
                          child: Text(
                            'Tidak Ada Ringkasan',
                            style: thirdTextStyle,
                          ),
                        ),
                    ],
                  ),
                ),
              ],
            ),
            Container(
              margin: EdgeInsets.only(
                left: getProportionateScreenWidth(16),
                right: getProportionateScreenWidth(16),
                top: getProportionateScreenWidth(16),
              ),
              child: Text(
                'Pengumuman',
                style: thirdTextStyle.copyWith(
                  fontWeight: semiBold,
                  letterSpacing: 1,
                  fontSize: getProportionateScreenWidth(14),
                ),
              ),
            ),
            SizedBox(height: getProportionateScreenHeight(14)),
            AnnouncementUserPage(idCourse: widget.id),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();

    super.dispose();
  }
}
