import 'package:hmg_patient_app/core/model/notifications/get_notifications_response_model.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/progress_indicator/app_circular_progress_Indeicator.dart';
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class NotificationsDetailsPage extends StatefulWidget {
  final GetNotificationsResponseModel notification;

  NotificationsDetailsPage({required this.notification});

  @override
  State<NotificationsDetailsPage> createState() => _NotificationsDetailsPageState();
}

class _NotificationsDetailsPageState extends State<NotificationsDetailsPage> {
  late YoutubePlayerController _controller;

  @override
  void initState() {
    _controller = YoutubePlayerController(
      initialVideoId: getVideoURL(),
      flags: YoutubePlayerFlags(
        autoPlay: true,
        mute: false,
      ),
    );
    super.initState();
  }

  getDateForm(String date) {
    DateTime d = DateUtil.convertStringToDate(date);
    String monthName = DateUtil.getMonth(d.month).toString();
    TimeOfDay timeOfDay = TimeOfDay(hour: d.hour, minute: d.minute);
    String minute = timeOfDay.minute < 10 ? timeOfDay.minute.toString().padLeft(2, '0') : timeOfDay.minute.toString();

    String hour = '${timeOfDay.hourOfPeriod}:$minute';
    if (timeOfDay.period == DayPeriod.am) {
      hour = hour + "AM";
    } else {
      {
        hour = hour + "PM";
      }
    }
    return monthName + ',${d.day},${d.year}, $hour';
  }

  String getVideoURL() {
    if (widget.notification.videoURL != null && widget.notification.notificationType == "2") {
      String videoId;
      videoId = YoutubePlayer.convertUrlToId(widget.notification.videoURL)!;
      print(videoId); // BBAyRBTfsOU
      return videoId;
    }else{
      return "";
    }
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      isShowDecPage: false,
      appBarTitle: TranslationBase.of(context).notificationDetails,
      body: ListView(
        physics: BouncingScrollPhysics(),
        padding: EdgeInsets.all(21),
        children: [
          Text(
            DateUtil.getDayMonthYearDateFormatted(DateUtil.convertStringToDate(widget.notification.createdOn!)) +
                " " +
                DateUtil.formatDateToTimeLang(DateUtil.convertStringToDate(widget.notification.createdOn!), false),
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w600,
              color: Color(0xff2E303A),
              letterSpacing: -0.64,
            ),
          ),
          SizedBox(height: 18),
          Text(
            widget.notification.message ?? "",
            style: TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.w600,
              color: Color(0xff575757),
              letterSpacing: -0.48,
            ),
          ),
          if (widget.notification.notificationType == "2")
            Padding(
              padding: const EdgeInsets.only(top: 18),
              child: YoutubePlayer(
                controller: _controller,
                showVideoProgressIndicator: true,
              ),
            ),
          if (widget.notification.messageTypeData != null)
            if (widget.notification.messageTypeData!.length != 0 && widget.notification.notificationType != "2")
              Padding(
                padding: const EdgeInsets.only(top: 18),
                child: Image.network(widget.notification.messageTypeData!, loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) {
                  if (loadingProgress == null) return child;
                  return Center(
                    child: SizedBox(
                      width: 40.0,
                      height: 40.0,
                      child: AppCircularProgressIndicator(),
                    ),
                  );
                }, fit: BoxFit.fill),
              ),
          SizedBox(height: 18),
          // Text(
          //   widget.notification.message!.trim(),
          //   style: TextStyle(
          //     fontSize: 12,
          //     fontWeight: FontWeight.w600,
          //     color: Color(0xff575757),
          //     letterSpacing: -0.48,
          //   ),
          // ),
          // Text(
          //   widget.notification.message.trim(),
          //   style: TextStyle(
          //     fontSize: 12,
          //     fontWeight: FontWeight.w600,
          //     color: Color(0xff575757),
          //     letterSpacing: -0.48,
          //   ),
          // ),
        ],
      ),
    );
  }
}
