import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/ppm/recurrent_wo.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';

import 'room_inspection_card.dart';

class RoomTabsWidget extends StatefulWidget {
  final RecurrentWoData? model;

  const RoomTabsWidget({Key? key, required this.model}) : super(key: key);

  @override
  State<RoomTabsWidget> createState() => _RoomTabsWidgetState();
}

class _RoomTabsWidgetState extends State<RoomTabsWidget> {
  int selectedIndex = 0;
  bool isLoading = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: EdgeInsets.all(8.toScreenHeight),
          height: 57.toScreenHeight,
          width: double.infinity,
          decoration: BoxDecoration(
            color: AppColor.tabBarBackground(context),
            border: Border.all(color: AppColor.tabBarBorder(context)),
            borderRadius: BorderRadius.circular(16),
          ),
          child: ListView(
            scrollDirection: Axis.horizontal,
            // mainAxisAlignment: MainAxisAlignment.spaceAround,
            padding: EdgeInsets.zero,
            children: widget.model!.planRecurrentMedicalTaskRooms!.asMap().entries.map((task) {
              final int index = task.key;
              final String label = task.value.room!.roomId!;
              return GestureDetector(
                onTap: () {
                  setState(() {
                    isLoading = true;
                    selectedIndex = index;
                    //TODO replace with api response delay...
                    Future.delayed(const Duration(seconds: 1)).whenComplete(() {
                      setState(() {
                        isLoading = false;
                      });
                    });
                  });
                },
                child: Container(
                  // margin: EdgeInsets.only(right: 8.toScreenWidth),
                  height: 49.toScreenHeight,
                  padding: EdgeInsets.symmetric(horizontal: 30.toScreenWidth),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: selectedIndex == index ? AppColor.background(context) : Colors.transparent,
                    border: selectedIndex == index ? Border.all(color: AppColor.border2Color(context)) : null,
                    borderRadius: BorderRadius.circular(15),
                  ),
                  child: label.bodyText(context).custom(color: context.isDark ? Colors.white : AppColor.white936),
                ),
              );
            }).toList(),
          ),
        ),
        isLoading
            ? const ALoading().paddingOnly(top: 150)
            : ListView(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: EdgeInsets.zero,
                children: widget.model!.planRecurrentMedicalTaskRooms![selectedIndex].planRecurrentMedicalTaskRoomTabs!.map<Widget>((card) {
                  return RoomInspectionCard(inspectionModel: card);
                }).toList(),
              ),
      ],
    );
  }
}
