// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../l10n/app_localizations.dart';

class EnquiryScreen extends StatefulWidget {
  const EnquiryScreen({super.key});

  @override
  State<EnquiryScreen> createState() => _EnquiryScreen();
}

class _EnquiryScreen extends State<EnquiryScreen> {
  Future<void> _launchEmailAddress(String email) async {
    final Uri emailUri = Uri(scheme: 'mailto', path: email);
    if (await canLaunchUrl(emailUri)) {
      await launchUrl(emailUri);
    } else {
      debugPrint('${AppLocalizations.of(context).emailLaunchError} $email');
    }
  }

  Future<void> _launchPhoneNumber(String phone) async {
    final Uri phoneUri = Uri(scheme: 'tel', path: phone);
    if (await canLaunchUrl(phoneUri)) {
      await launchUrl(phoneUri);
    } else {
      debugPrint('${AppLocalizations.of(context).dialerLaunchError} $phone');
    }
  }

  Widget _buildContactItem(String role, String email, String phone) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(role,
            style: TextStyle(color: Theme.of(context).colorScheme.onSurface)),
        const SizedBox(height: 4),
        GestureDetector(
          onTap: () => _launchEmailAddress(email),
          child: Text(email,
              style: TextStyle(color: Theme.of(context).colorScheme.primary)),
        ),
        const SizedBox(height: 4),
        GestureDetector(
          onTap: () => _launchPhoneNumber(phone),
          child: Text(phone,
              style:
                  TextStyle(color: Theme.of(context).scaffoldBackgroundColor)),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context).enquiry),
        centerTitle: false,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // … existing Mail us / Call us / Write to us …
            const SizedBox(height: 20),
            Text(
              AppLocalizations.of(context).writeToUs,
            ),
            const SizedBox(height: 4),
            Text(
              "complaint@kccb.in",
              style: TextStyle(color: Theme.of(context).colorScheme.primary),
            ),

            const SizedBox(height: 20),
            Text(
              AppLocalizations.of(context).keyContacts,
              style: TextStyle(
                fontSize: 17,
                color: Theme.of(context).colorScheme.primary,
              ),
              // horizontal line
            ),
            Divider(color: Theme.of(context).colorScheme.outline),
            const SizedBox(height: 16),
            _buildContactItem(
              AppLocalizations.of(context).chairman,
              "chairman@kccb.in",
              "01892-222677",
            ),
            const SizedBox(height: 16),
            _buildContactItem(
              AppLocalizations.of(context).managingDirector,
              "md@kccb.in",
              "01892-224969",
            ),
            const SizedBox(height: 16),
            _buildContactItem(
              AppLocalizations.of(context).gmWest,
              "gmw@kccb.in",
              "01892-223280",
            ),
            const SizedBox(height: 16),
            _buildContactItem(
              AppLocalizations.of(context).gmNorth,
              "gmn@kccb.in",
              "01892-224607",
            ),
          ],
        ),
      ),
    );
  }
}
