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

  Future<void> _launchUrl(String url) async {
    final Uri uri = Uri.parse(url);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri);
    } else {
      // Consider adding a 'urlLaunchError' key to your AppLocalizations
      debugPrint('Could not launch $url');
    }
  }

  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: [
            const SizedBox(height: 20),
            GestureDetector(
                onTap: () => _launchUrl("https://kccb.in/complaint-form"),
                child: Row(mainAxisSize: MainAxisSize.min, children: [
                  Text(
                    "Complaint Form",
                    style: TextStyle(
                      fontSize: 17,
                      color: Theme.of(context).colorScheme.primary,
                      decorationColor: Theme.of(context).colorScheme.primary,
                    ),
                  ),
                  const SizedBox(width: 4),
                  Icon(
                    Icons.open_in_new,
                    color: Theme.of(context).colorScheme.primary,
                    size: 16.0,
                  ),
                ])),
            const SizedBox(height: 40),
            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",
            ),
          ],
        ),
      ),
    );
  }
}
