import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/theme/colors.dart';
import 'package:tm_app/core/utils/size_config.dart';
import 'package:tm_app/view_models/notification_view_model.dart';
import 'package:tm_app/views/notification/strings/notification_strings.dart';
import 'package:tm_app/views/shared/tender_app_bar.dart';

import '../../shared/main_tab_bar.dart';
import '../widgets/notification_all_tab.dart';
import '../widgets/notification_important_tab.dart';
import '../widgets/notification_unread_tab.dart';

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

  @override
  State<MobileNotificationPage> createState() => _MobileNotificationPageState();
}

class _MobileNotificationPageState extends State<MobileNotificationPage>
    with SingleTickerProviderStateMixin {
  late TabController controller;
  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    controller = TabController(length: 3, vsync: this);

    _scrollController.addListener(() {
      final viewModel = context.read<NotificationViewModel>();
      if (_scrollController.position.pixels >=
              _scrollController.position.maxScrollExtent - 100 &&
          !viewModel.isLoading &&
          viewModel.hasMoreData) {
        viewModel.getNotifications(
          page: viewModel.currentPage + 1,
          isMobile: true,
        );
      }
    });
  }

  @override
  void dispose() {
    controller.dispose();
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final viewModel = context.watch<NotificationViewModel>();

    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      appBar: tenderMobileAppBar(title: NotificationStrings.notificationTitle),
      body: Column(
        children: [
          MainTabBar(
            controller: controller,
            titles: [
              NotificationStrings.all,
              NotificationStrings.unread,
              NotificationStrings.important,
            ],
          ),
          SizedBox(height: 12.0.h()),
          Padding(
            padding: EdgeInsetsDirectional.only(end: 24.0.w()),
            child: Align(
              alignment: Alignment.centerRight,
              child: InkWell(
                onTap: () {
                  viewModel.markAllAsRead(context);
                },
                borderRadius: BorderRadius.circular(99),
                child: Container(
                  width: 132.0.w(),
                  height: 32.0.h(),
                  decoration: BoxDecoration(
                    color: AppColors.primary20,
                    borderRadius: BorderRadius.circular(99),
                  ),
                  child: Center(
                    child: Text(
                      NotificationStrings.markAllAsReadButton,
                      style: TextStyle(
                        fontSize: 14.0.sp(),
                        fontWeight: FontWeight.w500,
                        color: AppColors.mainBlue,
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
          SizedBox(height: 12.0.h()),
          viewModel.isLoading
              ? Container()
              : Expanded(
                child: TabBarView(
                  controller: controller,
                  children: [
                    NotificationAllTab(scrollController: _scrollController),
                    NotificationUnreadTab(scrollController: _scrollController),
                    NotificationImportantTab(
                      scrollController: _scrollController,
                    ),
                  ],
                ),
              ),
          if (viewModel.isMoreLoading) ...[
            const Padding(
              padding: EdgeInsets.symmetric(vertical: 16),
              child: Center(
                child: CircularProgressIndicator(color: AppColors.secondary50),
              ),
            ),
          ],
          if (viewModel.isLoading &&
              (viewModel.notificationResponse?.data?.notifications?.isEmpty ??
                  true)) ...[
            const Spacer(),
            const Center(
              child: CircularProgressIndicator(color: AppColors.secondary50),
            ),
            const Spacer(),
          ],
        ],
      ),
    );
  }
}
