import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/members_management_view.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/smart_linkage_view.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/temporary_password_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/unlocking_records_view.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
import 'package:syncrow_app/generated/assets.dart';

class DoorLockGrid extends StatelessWidget {
  final String uuid;
  const DoorLockGrid({super.key, required this.uuid});

  @override
  Widget build(BuildContext context) {
    final buttons = doorLockButtons(val: uuid);

    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        mainAxisSpacing: 10,
        crossAxisSpacing: 10,
        childAspectRatio: 1.75 / 1,
      ),
      itemCount: buttons.length,
      itemBuilder: (context, index) => DefaultContainer(
        onTap: () {
          //TODO: remove checking after adding the pages
          doorLockButtons()[index]['page'] != null
              ? Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) =>
                        doorLockButtons(val: uuid)[index]['page'] as Widget,
                  ),
                )
              : null;
        },
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ConstrainedBox(
              constraints: const BoxConstraints(maxHeight: 46, maxWidth: 50),
              child: SvgPicture.asset(
                doorLockButtons()[index]['image'] as String,
              ),
            ),
            const SizedBox(
              height: 15,
            ),
            Flexible(
              child: FittedBox(
                child: BodySmall(
                  text: doorLockButtons()[index]['title'] as String,
                  // doorLockButtons.keys.elementAt(index),
                  textAlign: TextAlign.center,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

List<Map<String, dynamic>> doorLockButtons({val}) => [
      {
        'title': 'Unlocking Records',
        'image': Assets.assetsIconsDoorlockAssetsUnlockingRecords,
        'page': const UnlockingRecordsView(),
      },
      {
        'title': 'Members Management',
        'image': Assets.assetsIconsDoorlockAssetsMembersManagement,
        'page': const MembersManagementView(),
      },
      if (HomeCubit.manageDeviceLocation)
        {
          'title': 'Temporary Password',
          'image': Assets.assetsIconsDoorlockAssetsTemporaryPassword,
          'page': TemporaryPasswordPage(deviceId: val),
        },
      {
        'title': 'Smart Linkage',
        'image': Assets.assetsIconsDoorlockAssetsSmartLinkage,
        'page': const SmartLinkgeView()
      },
    ];
