import 'package:community_material_icon/community_material_icon.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_email_sender/flutter_email_sender.dart';
import 'package:prosappco/src/components/pop_appbar.dart';
import 'package:prosappco/src/models/setting_model.dart';
import 'package:url_launcher/url_launcher.dart';

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

  @override
  State<SupportScreen> createState() => SsupportStateScreen();
}

class SsupportStateScreen extends State<SupportScreen> {
  SettingModel? settings;
  final String subject = 'Soporte Prosapp';
  final String body = '';

  Future<void> _sendWhatsapp(String? number) async {
    final _whatsappUrl =
        'https://wa.me/$number?text=${Uri.parse('Hola! soy usuario de Prosapp y quisiera conocer mas sobre esta app')}';

    if (!await launch(_whatsappUrl)) {
      throw Exception('Could not launch $_whatsappUrl');
    }
  }

  Future<void> _sendEmail(String recipients) async {
    final Email email = Email(
      body: body,
      subject: subject,
      recipients: [recipients],
      isHTML: false,
    );

    await FlutterEmailSender.send(email);
  }

  void _sendEmailWeb(String recipients) async {
    final email =
        'mailto:$recipients?subject=${Uri.encodeComponent(recipients)}&body=${Uri.encodeComponent(body)}';
    if (await canLaunch(email)) {
      await launch(email);
    } else {
      // No se pudo abrir el cliente de correo electrónico
      // Puedes mostrar un diálogo o realizar otra acción en este caso
    }
  }

  @override
  void initState() {
    super.initState();
    if (settings == null) {
      SettingModel.getSettings().then(
        (SettingModel value) => setState(() {
          settings = value;
        }),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PopAppbar(
          onPressed: () {
            Navigator.pop(context);
          },
          label: 'Soporte'),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 30, left: 35, right: 35),
            child: Text(
              '${settings?.titulo}',
              style: const TextStyle(fontWeight: FontWeight.w600),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 30, horizontal: 35),
            child: Text('${settings?.parrafo}'),
          ),
          Row(
            children: [
              const Expanded(child: SizedBox()),
              ElevatedButton(
                onPressed: () {
                  _sendWhatsapp(settings?.numero);
                },
                style: ElevatedButton.styleFrom(
                  foregroundColor: Colors.white,
                  backgroundColor: const Color(0xFF2BA4EC),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(50),
                    side: const BorderSide(
                      color: Color(0xFF2BA4EC),
                      width: 2,
                    ),
                  ),
                ),
                child: const Padding(
                  padding: EdgeInsets.symmetric(vertical: 18, horizontal: 0),
                  child: Icon(
                    CommunityMaterialIcons.whatsapp,
                    size: 30,
                    color: Colors.white,
                  ),
                ),
              ),
              const SizedBox(width: 20),
              ElevatedButton(
                onPressed: () {
                  if (kIsWeb) {
                    _sendEmailWeb(settings?.email ?? '');
                  } else {
                    _sendEmail(settings?.email ?? '');
                  }
                },
                style: ElevatedButton.styleFrom(
                  foregroundColor: Colors.white,
                  backgroundColor: const Color(0xFF2BA4EC),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(50),
                    side: const BorderSide(
                      color: Color(0xFF2BA4EC),
                      width: 2,
                    ),
                  ),
                ),
                child: const Padding(
                  padding: EdgeInsets.symmetric(vertical: 18, horizontal: 0),
                  child: Icon(
                    Icons.email_outlined,
                    size: 30,
                    color: Colors.white,
                  ),
                ),
              ),
              const Expanded(child: SizedBox()),
            ],
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 50),
            child: Column(
              children: [
                const Text(
                  'Horario de atención:',
                  style: TextStyle(fontWeight: FontWeight.w600),
                ),
                const SizedBox(height: 20),
                Text('${settings?.dias}'),
                const SizedBox(height: 5),
                Text('${settings?.horas}'),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
