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 get isWorkTime {
    final now = 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));
  }

  /// Helper to format working hours for UI
  String get workHoursString =>
      '${workStartTime.hour.toString().padLeft(2, '0')}:${workStartTime.minute.toString().padLeft(2, '0')} - '
      '${workEndTime.hour.toString().padLeft(2, '0')}:${workEndTime.minute.toString().padLeft(2, '0')}';

  /// 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)];
  }

  /// DIRECT ACTIONS
  void makeCall() {
    if (isWorkTime) {
      makePhoneCall(getRandomPhone());
    }
  }

  void sendWhatsApp() {
    launchCommunication('whatsapp', getRandomPhone(), 'Hello'.tr);
  }

  void sendEmail() {
    launchCommunication('email', 'support@intaleqapp.com', 'Hello'.tr);
  }

  /// SHOW DIALOG (Optional legacy support)
  void showContactDialog(BuildContext context) {
    bool withinHours = isWorkTime;

    showCupertinoModalPopup(
      context: context,
      builder: (context) => CupertinoActionSheet(
        title: Text('Contact Us'.tr),
        message: Text('Choose a contact option'.tr),
        actions: <Widget>[
          if (withinHours)
            CupertinoActionSheetAction(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Icon(CupertinoIcons.phone),
                  Text('Call Support'.tr),
                ],
              ),
              onPressed: () {
                Navigator.pop(context);
                makeCall();
              },
            ),
          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),
            ),
          CupertinoActionSheetAction(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                const Icon(
                  FontAwesome.whatsapp,
                  color: AppColor.greenColor,
                ),
                Text('Send WhatsApp Message'.tr),
              ],
            ),
            onPressed: () {
              Navigator.pop(context);
              sendWhatsApp();
            },
          ),
          CupertinoActionSheetAction(
            child: Text('Send Email'.tr),
            onPressed: () {
              Navigator.pop(context);
              sendEmail();
            },
          ),
        ],
        cancelButton: CupertinoActionSheetAction(
          child: Text('Cancel'.tr),
          onPressed: () => Navigator.pop(context),
        ),
      ),
    );
  }
}
