import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
import 'package:provider/provider.dart';
import 'package:telnow_mobile_new/src/layouts/components/template.dart';
import 'package:telnow_mobile_new/src/layouts/web/menu-history/history_item_widget.dart';

import '../../../cubit/menu_history_cubit.dart';
import '../../../cubit/tab_history_cubit.dart';
import '../../../utils/U.dart';
import '../../../utils/provider.dart';
import '../../functions/history.dart' hide HistoryTab;

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

  @override
  State<StatefulWidget> createState() => _OngoingContainerState();
}

class _OngoingContainerState extends State<OngoingContainer>
    with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  late AnimationController _animationController;


  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 800),
    )..repeat(reverse: true);
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BlocBuilder<OngoingCubit, TabState>(
      builder: (BuildContext context, state) {
        UserModule userModule = Provider.of<UserModule>(context, listen: false);
        HistoryFunction historyFunction = HistoryFunction();
        var pendingData = context.read<MenuHistoryCubit>().state.pendingData;
        List<dynamic> rating = [];

        if(state.isLoading) {
          return loadingTemplateNoVoid();
        }

        if(!U.getInternetStatus() && pendingData.isNotEmpty) {
          return ListView.builder(
            itemCount: state.data.length,
            itemBuilder: (context, i) {
              return HistoryItemWidget(
                tab: 'ongoing',
                state: state,
                index: i,
                item: pendingData[i],
                userModule: userModule,
                rating: rating,
                animationController: _animationController
              );
            });
        }

        if(state.data.isEmpty) {
          return NoDataPage();
        }

        return LazyLoadScrollView(
          onEndOfPage: () => context.read<OngoingCubit>().loadNextPage(),
          scrollOffset: 300,
          child: ListView.builder(
            itemCount: state.data.length,
            itemBuilder: (context, i) {
              var dataRequest = state.data;

              return HistoryItemWidget(
                tab: 'ongoing',
                state: state,
                index: i,
                item: dataRequest[i],
                userModule: userModule,
                rating: rating,
                animationController: _animationController
              );
            }),
        );
      }
    );
  }

  // renderStatus(text, color){
  //   return Container(
  //     padding: EdgeInsets.symmetric(vertical: 3, horizontal: 10),
  //     margin: EdgeInsets.only(left: 10),
  //     decoration: BoxDecoration(
  //         color: color, borderRadius: BorderRadius.all(Radius.circular(3))
  //     ),
  //     child: Text(text, style: TextStyle(fontSize: 12, color: textColor)),
  //   );
  // }
  //
  // Widget renderRequested(list){
  //   if(list['receptionistId'] != null){
  //     var user = userModule.user();
  //     if(user['roomAttendant'] && user['userId'] != list['informantUserId'] && user['userId'] == list['receptionistId']){
  //       return Column(
  //         crossAxisAlignment: CrossAxisAlignment.start,
  //         children: [
  //           Text('${'requestedFor'.tr()}: ${list['informantName']??'-'}' ,style: TextStyle(color: textColor, fontSize: 13, fontWeight: FontWeight.w300)),
  //           SizedBox(height: 6),
  //         ],
  //       );
  //     }
  //
  //     if(user['userId'] == list['informantUserId'] && user['userId'] != list['receptionistId']){
  //       return Column(
  //         crossAxisAlignment: CrossAxisAlignment.start,
  //         children: [
  //           Text('${'requestedBy'.tr()}: ${list['receptionistName']??''}' ,style: TextStyle(color: textColor, fontSize: 13, fontWeight: FontWeight.w300)),
  //           SizedBox(height: 6),
  //         ],
  //       );
  //     }
  //   }
  //
  //   return Container();
  // }
}
