import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/response/notification_response.dart';
import 'package:e_commerce_training/src/page/main/main.dart';
import 'package:e_commerce_training/src/page/notification/notification_cubit.dart';
import 'package:e_commerce_training/src/util/const.dart';
import 'package:e_commerce_training/src/util/date_util.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:e_commerce_training/src/widget/custom_appbar.dart';
import 'package:e_commerce_training/src/widget/pending_action.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class NotificationPage extends StatefulWidget {
  const NotificationPage({Key? key}) : super(key: key);

  @override
  _NotificationPageState createState() => _NotificationPageState();
}

class _NotificationPageState extends State<NotificationPage> {
  late NotificationCubit _cubit;
  late MainCubit _mainCubit;
  RefreshController _refreshController = RefreshController();

  @override
  void initState() {
    // TODO: implement initState
    _mainCubit = BlocProvider.of<MainCubit>(context);
    _cubit = BlocProvider.of<NotificationCubit>(context);
    _cubit.getListNotification();
    super.initState();
    _cubit.pushFCMtoken();
    _cubit.initMessaging();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: R.color.white,
      appBar: appBar(context, R.string.notification.tr(), rightIcons: [MdiIcons.cartOutline]),
      body: BlocProvider(
        create: (BuildContext context) => _cubit,
        child: BlocConsumer<NotificationCubit, NotificationState>(
          listener: (context, state) {
            if (state is NotificationFailure) Util.showErrorSnackBar(context, state.error);
            if (!(state is NotificationLoading)) {
              _refreshController.refreshCompleted();
              _refreshController.loadComplete();
            }
            if (state is ReadNotificationSuccess) {
              // _mainCubit.getCountNotification();
              _mainCubit.countUnRead--;
              _mainCubit.refreshMainEvent();
            }
          },
          builder: (BuildContext context, NotificationState state) {
            return Container(
              height: double.infinity,
              color: R.color.white,
              child: Stack(
                children: [
                  SmartRefresher(
                    enablePullUp: _cubit.hasMorePage,
                    controller: _refreshController,
                    onRefresh: () => _cubit.getListNotification(isRefresh: true),
                    onLoading: () => _cubit.getListNotification(isLoadMore: true),
                    child: ListView.separated(
                      shrinkWrap: true,
                      itemCount: _cubit.listNotification.length,
                      separatorBuilder: (context, index) => Container(color: R.color.gray, height: 0.5),
                      itemBuilder: (BuildContext context, int index) {
                        NotificationData data = _cubit.listNotification[index];
                        bool isRead = data.isRead == true;
                        return InkWell(
                          onTap: () {
                            _cubit.readNotification(data);
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Image.asset(
                                  R.drawable.ic_notification,
                                  height: 40.h,
                                  width: 40.h,
                                  fit: BoxFit.cover,
                                  color: R.color.accentColor,
                                ),
                                SizedBox(
                                  width: 15,
                                ),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        data.title ?? "",
                                        maxLines: 1,
                                        style: TextStyle(
                                          fontSize: 16.sp,
                                          fontWeight: isRead ? FontWeight.w400 : FontWeight.bold,
                                          color: R.color.black,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 5,
                                      ),
                                      Text(
                                        data.message ?? "",
                                        style: TextStyle(
                                          fontSize: 16.sp,
                                          fontWeight: isRead ? FontWeight.w400 : FontWeight.bold,
                                          color: R.color.gray,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 5,
                                      ),
                                      Text(
                                        DateUtil.parseDateDefault(data.createDate, Const.DATE_FORMAT_POST) ?? "",
                                        style: TextStyle(
                                          fontSize: 16.sp,
                                          color: R.color.grey500,
                                        ),
                                      ),
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                  Visibility(
                    visible: state is NotificationLoading,
                    child: PendingAction(),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
