import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:traderscircuit/Utils/Common/CommonAppbar.dart';
import 'package:traderscircuit/Utils/Common/sized_box.dart';
import 'package:traderscircuit/Utils/text.dart';
import 'package:traderscircuit/model/Notification/notification_model.dart';
import 'package:traderscircuit/view_model/Notification/notification_api.dart';

class NotificationScreen extends StatefulWidget {
  const NotificationScreen({super.key});

  @override
  State<NotificationScreen> createState() => _NotificationScreenState();
}

class _NotificationScreenState extends State<NotificationScreen> {
  StreamController<GetNotification> NotificationController = StreamController();
  late Timer timer;

  @override
  void initState() {
    // timer = Timer.periodic(Duration(seconds: 2), (timer) {
    NotificationsAPI().notificationList(streamControl: NotificationController);
    //  });
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    NotificationController.close();
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: CommonAppbar(titleTxt: ''),
      body: StreamBuilder<GetNotification>(
        stream: NotificationController.stream,
        builder: (ctx, snapshot) {
          if (snapshot.data == null) {
            return Center(
              child: text25W600("No Notification!"),
            );
          }
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasError) {
              return Center(
                child: Text(
                  '${snapshot.error} occured',
                  style: TextStyle(fontSize: 18.spMin),
                ),
              );
            }
          }
          return notificationobj!.data!.list!.isEmpty
              ? Center(child: _buildNodataBody())
              : _buildBody(context);
        },
      ),
    );
  }

  Widget _buildNodataBody() {
    return
        //  Scaffold(
        //   body:
        Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // Lottie.asset('assets/logos/NoDataFoundLottie.json'),
        text25W600(
          "No Data Available",
          // textAlign: TextAlign.center,
        ),
      ],
    );
  }

// 'assets/images/png/Ellipse 1494.png'
  Widget notificationCard({
    required String profileimg,
    required String title,
    required String subtitle,
    required String time,
  }) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              CircleAvatar(
                backgroundImage: NetworkImage(profileimg),
                radius: 26.5.r,
              ),
              sizedBoxWidth(12.w),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  text14W400(title),
                  text12W500_B4B4B4(subtitle),
                ],
              ),
            ],
          ),
          text12W500(time),
        ],
      ),
    );
  }

  Widget _buildBody(context) {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(left: 16.w),
            child: text25W600('Notifications'),
          ),
          sizedBoxHeight(40.h),
          ListView.builder(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemCount: notificationobj!.data!.list!.length,
            itemBuilder: (context, index) {
              return Column(
                children: [
                  notificationCard(
                      profileimg: notificationobj!.data!.list!
                          .elementAt(index)
                          .notificationImage!,
                      title: notificationobj!.data!.list!
                          .elementAt(index)
                          .notificationMessage!,
                      subtitle: notificationobj!.data!.list!
                              .elementAt(index)
                              .messageName ??
                          "",
                      time: notificationobj!.data!.list!
                              .elementAt(index)
                              .deliverySchedule ??
                          ""),
                  if (index != CardList.length - 1)
                    Container(
                      height: 1,
                      margin: EdgeInsets.symmetric(vertical: 20.h),
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            color: Color.fromRGBO(176, 176, 176, 0.5),
                            width: 1,
                          ),
                        ),
                      ),
                    ),
                ],
              );
            },
          ),
          sizedBoxHeight(40.h),
        ],
      ),
    );
  }

  List<Map<String, String>> CardList = [
    {
      'profileimg': 'assets/images/png/Ellipse 588.png',
      'title': 'Mokshada Kesarkar',
      'subtitle': 'Lorem ipsum dolor sit amet cons......',
      'time': '1 day ago'
    },
    {
      'profileimg': 'assets/images/png/Ellipse 591.png',
      'title': 'Afrid Mulla',
      'subtitle': 'Lorem ipsum dolor sit amet cons......',
      'time': '1 day ago'
    },
    {
      'profileimg': 'assets/images/png/Ellipse 588.png',
      'title': 'Mokshada Kesarkar',
      'subtitle': 'Lorem ipsum dolor sit amet cons......',
      'time': '2 days ago'
    },
    {
      'profileimg': 'assets/images/png/Ellipse 591.png',
      'title': 'Afrid Mulla',
      'subtitle': 'Lorem ipsum dolor sit amet cons......',
      'time': '3 days ago'
    },
  ];
}
