import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/presentation/routes/routes.dart';
import 'package:rasadyar_chicken/presentation/utils/nested_keys_utils.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class WidelyUsedWidget extends StatelessWidget {
  const WidelyUsedWidget({super.key});

  @override
  Widget build(BuildContext context) {
    final WidelyUsedLogic controller = Get.put(WidelyUsedLogic());

    return Container(
      height: 110.h,
      margin: EdgeInsetsGeometry.all(6),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(width: 0.50, color: const Color(0xFFA9A9A9)),
      ),
      child: Stack(
        clipBehavior: Clip.none,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(12.w, 24.h, 12.w, 16.h),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                widelyUsed(
                  title: 'خرید خارج استان',
                  iconPath: Assets.vec.map1Svg.path,
                  isOnEdit: false,
                  cardColor: AppColor.greenLightActive,
                  labelColor: AppColor.greenNormal,
                  onTap: () async {
                    controller.rootLogic.currentPage.value = 0;
                    controller.rootLogic.currentPage.refresh();
                    await Future.delayed(Duration(milliseconds: 100));
                    Get.toNamed(ChickenRoutes.buysOutOfProvinceSteward, id: stewardFirstKey);
                  },
                ),

                widelyUsed(
                  title: 'خرید داخل استان',
                  iconPath: Assets.vec.buyOutProvinceSvg.path,
                  cardColor: AppColor.greenLightActive,
                  labelColor: AppColor.greenNormal,
                  onTap: () async {
                    controller.rootLogic.currentPage.value = 0;
                    controller.rootLogic.currentPage.refresh();
                    await Future.delayed(Duration(milliseconds: 100));
                    Get.toNamed(ChickenRoutes.buysInProvinceSteward, id: stewardFirstKey);
                  },
                  isOnEdit: false,
                ),

                widelyUsed(
                  title: 'فروش خارج استان',
                  iconPath: Assets.vec.map2Svg.path,
                  isOnEdit: false,
                  onTap: () async {
                    controller.rootLogic.currentPage.value = 1;
                    controller.rootLogic.currentPage.refresh();
                    await Future.delayed(Duration(milliseconds: 100));
                    Get.toNamed(ChickenRoutes.salesOutOfProvinceSteward, id: stewardSecondKey);
                  },
                ),

                widelyUsed(
                  title: 'فروش داخل استان',
                  iconPath: Assets.vec.saleOutProvinceSvg.path,
                  isOnEdit: false,
                  onTap: () async {
                    controller.rootLogic.currentPage.value = 1;
                    controller.rootLogic.currentPage.refresh();
                    await Future.delayed(Duration(milliseconds: 100));
                    Get.toNamed(ChickenRoutes.salesInProvinceSteward, id: stewardSecondKey);
                  },
                ),
              ],
            ),
          ),
          Positioned(
            top: -17,
            right: 11,
            child: Container(
              padding: EdgeInsets.all(4),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(width: 0.50, color: const Color(0xFFA9A9A9)),
              ),
              child: Text('پر کاربردها', textAlign: TextAlign.right, style: AppFonts.yekan16),
            ),
          ),
        ],
      ),
    );
  }

  Widget widelyUsed({
    required String title,
    required String iconPath,
    required VoidCallback onTap,
    required bool isOnEdit,
    Color? cardColor,
    Color? labelColor,
    Color? textColor,
  }) {
    return GestureDetector(
      onTap: !isOnEdit ? onTap : null,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        spacing: 4,
        children: [
          Stack(
            clipBehavior: Clip.none,
            children: [
              Container(
                width: 48.w,
                height: 48.h,
                padding: EdgeInsets.all(4),
                decoration: ShapeDecoration(
                  color: cardColor ?? Color(0xFFBECDFF),
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                ),
                child: Container(
                  width: 40.w,
                  height: 40.h,
                  decoration: ShapeDecoration(
                    color: labelColor ?? AppColor.blueNormal,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                  ),
                  padding: EdgeInsets.all(4),
                  child: SvgGenImage.vec(iconPath).svg(
                    width: 24.w,
                    height: 24.h,
                    colorFilter: ColorFilter.mode(Colors.white, BlendMode.srcIn),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              Visibility(
                visible: isOnEdit,
                child: Container(
                  width: 48,
                  height: 48,
                  padding: EdgeInsets.all(4),
                  decoration: ShapeDecoration(
                    color: Colors.white60,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                  ),
                ),
              ),

              Visibility(
                visible: isOnEdit,
                child: Positioned(
                  top: -15,
                  left: -12,
                  child: SizedBox(
                    width: 32.w,
                    height: 32.h,
                    child: GestureDetector(
                      onTap: () {},
                      behavior: HitTestBehavior.translucent,
                      child: Center(
                        child: Container(
                          width: 16,
                          height: 16,
                          decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.white),
                          alignment: Alignment.center,
                          child: Icon(CupertinoIcons.minus, color: AppColor.error, size: 15),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          Text(title, style: AppFonts.yekan10.copyWith(color: textColor ?? AppColor.textColor)),
        ],
      ),
    );
  }

  Widget addWidelyUsed({required VoidCallback onTap}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      spacing: 4,
      children: [
        Container(
          width: 48,
          height: 48,
          padding: EdgeInsets.all(4),
          decoration: ShapeDecoration(
            color: const Color(0xFFD9F7F0),
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
          ),
          child: Assets.vec.messageAddSvg.svg(
            width: 40,
            height: 40,
            colorFilter: ColorFilter.mode(AppColor.greenNormal, BlendMode.srcIn),
            fit: BoxFit.cover,
          ),
        ),
        Text('افزودن', style: AppFonts.yekan10.copyWith(color: AppColor.greenDarkHover)),
      ],
    );
  }
}
