import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.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/modules/cm_module/views/nurse/create_new_request_view.dart';
import 'package:test_sa/modules/tm_module/tasks_wo/create_task_view.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/pages/gas_refill_request_form.dart';
import 'package:test_sa/views/pages/device_transfer/create__device_transfer_request.dart';


class CreateRequestTypeBottomSheet extends StatelessWidget {
  final bool isEngineer;

  const CreateRequestTypeBottomSheet({super.key, required this.isEngineer});

  @override
  Widget build(BuildContext context) {
    List<CreateRequestModel> requestList = CreateRequestModel.requestsList(context, isEngineer);

    return Container(
      padding: const EdgeInsets.all(16.0),
      width: double.infinity,
      color: AppColor.background(context),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          context.translation.selectWorkOrder.heading4(context),
          GridView(
            padding: const EdgeInsets.only(top: 16, bottom: 16),
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, childAspectRatio: 1, crossAxisSpacing: 16, mainAxisSpacing: 16),
            children: <Widget>[
              for (var request in requestList)
                listCard(
                  context: context,
                  icon: request.icon,
                  label: request.title,
                  onTap: () {
                    Navigator.pushReplacementNamed(context, request.routeName);
                  },
                ),
            ],
          ),
        ],
      ),
    );
    return const Placeholder();
  }

  Widget listCard({required String icon, required String label, required VoidCallback onTap, required BuildContext context}) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        constraints: BoxConstraints(minWidth: 111.toScreenWidth, minHeight: 111.toScreenHeight),
        padding: EdgeInsets.symmetric(horizontal: 12.toScreenWidth, vertical: 12.toScreenHeight),
        decoration: BoxDecoration(borderRadius: BorderRadius.circular(12), border: Border.all(width: 1, color: AppColor.white70)),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
             Icon(
              Icons.add_circle,
              color: AppColor.iconColor(context),
              size: 32,
            ),
            //24.height,
            label.bodyText2(context).custom(color:context.isDark?Colors.white: AppColor.black20),
          ],
        ),
      ),
    );
  }
}

class CreateRequestModel {
  String title;
  String icon;
  String routeName;

  CreateRequestModel(this.title, this.icon, this.routeName);

  static List<CreateRequestModel> requestsList(BuildContext context, bool isEngineer) {
    List<CreateRequestModel> list = [];
    if (isEngineer) {
      if (Provider.of<SettingProvider>(context, listen: false).engineerCanCreateCM) {
        list.add(CreateRequestModel(context.translation.correctiveMaintenance, "add_icon", CreateNewRequest.id));
      }
      // list.add(CreateRequestModel(context.translation.task, "add_icon", CreateTaskView.id)); // todo enable it in NextRelease
    } else {
      list.add(CreateRequestModel(context.translation.correctiveMaintenance, "add_icon", CreateNewRequest.id));
      list.add(CreateRequestModel(context.translation.gasRefill, "add_icon", GasRefillRequestForm.routeName));
      list.add(CreateRequestModel(context.translation.transferAsset, "add_icon", CreateDeviceTransferRequest.id));
      // list.add(CreateRequestModel(context.translation.task, "add_icon", CreateTaskView.id)); // todo enable it in NextRelease
    }
    return list;
  }
}
