import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_font_icons/flutter_font_icons.dart';
import 'package:get/get.dart';

import '../../../constant/colors.dart';
import '../functions/launch.dart';

class ContactUsController extends GetxController {
  /// WORKING HOURS (10:00 → 16:00)
  final TimeOfDay workStartTime = const TimeOfDay(hour: 10, minute: 0);
  final TimeOfDay workEndTime = const TimeOfDay(hour: 16, minute: 0);

  bool _isWithinWorkTime(TimeOfDay now) {
    return (now.hour > workStartTime.hour ||
            (now.hour == workStartTime.hour &&
                now.minute >= workStartTime.minute)) &&
        (now.hour < workEndTime.hour ||
            (now.hour == workEndTime.hour && now.minute <= workEndTime.minute));
  }

  /// PHONE LIST (USED FOR CALLS + WHATSAPP)
  final List<String> phoneNumbers = [
    '+963952475734',
    '+963952475740',
    '+963952475742'
  ];

  /// RANDOM PHONE SELECTOR
  String getRandomPhone() {
    final random = Random();
    return phoneNumbers[random.nextInt(phoneNumbers.length)];
  }

  /// SHOW DIALOG
  void showContactDialog(BuildContext context) {
    TimeOfDay now = TimeOfDay.now();
    bool withinHours = _isWithinWorkTime(now);

    showCupertinoModalPopup(
      context: context,
      builder: (context) => CupertinoActionSheet(
        title: Text('Contact Us'.tr),
        message: Text('Choose a contact option'.tr),
        actions: <Widget>[
          /// 📞 CALL (RANDOM) — ONLY DURING WORK HOURS
          if (withinHours)
            CupertinoActionSheetAction(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Icon(CupertinoIcons.phone),
                  Text('Call Support'.tr),
                ],
              ),
              onPressed: () {
                final phone = getRandomPhone();
                makePhoneCall(phone);
              },
            ),

          /// ⛔ OUTSIDE WORK HOURS — SHOW INFO
          if (!withinHours)
            CupertinoActionSheetAction(
              child: Text(
                'Work time is from 10:00 AM to 16:00 PM.\nYou can send a WhatsApp message or email.'
                    .tr,
                textAlign: TextAlign.center,
              ),
              onPressed: () => Navigator.pop(context),
            ),

          /// 💬 WHATSAPP (RANDOM)
          CupertinoActionSheetAction(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                const Icon(
                  FontAwesome.whatsapp,
                  color: AppColor.greenColor,
                ),
                Text('Send WhatsApp Message'.tr),
              ],
            ),
            onPressed: () {
              final phone = getRandomPhone();
              launchCommunication('whatsapp', phone, 'Hello'.tr);
            },
          ),

          /// 📧 EMAIL
          CupertinoActionSheetAction(
            child: Text('Send Email'.tr),
            onPressed: () => launchCommunication(
                'email', 'support@intaleqapp.com', 'Hello'.tr),
          ),
        ],

        /// ❌ CANCEL BUTTON
        cancelButton: CupertinoActionSheetAction(
          child: Text('Cancel'.tr),
          onPressed: () => Navigator.pop(context),
        ),
      ),
    );
  }
}
