import 'package:fcs/domain/entities/setting.dart';
import 'package:fcs/domain/vo/contact.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/contact/contact_editor.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:package_info/package_info.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

import 'widgets.dart';

class ContactPage extends StatefulWidget {
  @override
  _ContactPageState createState() => _ContactPageState();
}

class _ContactPageState extends State<ContactPage> {
  @override
  Widget build(BuildContext context) {
    Setting setting = Provider.of<MainModel>(context).setting;
    bool isEditable = context.select((MainModel m) => m.contactEditable());

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        leading: new IconButton(
          icon: new Icon(
            CupertinoIcons.back,
            color: primaryColor,
          ),
          onPressed: () => Navigator.of(context).pop(),
        ),
        shadowColor: Colors.transparent,
        backgroundColor: Colors.white,
        title: LocalText(
          context,
          'contact.title',
          color: primaryColor,
          fontSize: 20,
        ),
        actions: isEditable
            ? [
                IconButton(
                    onPressed: () =>
                        Navigator.of(context).push<void>(CupertinoPageRoute(
                          builder: (context) => ContactEditor(
                              contact: Contact.fromSetting(setting)),
                        )),
                    icon: Icon(
                      CupertinoIcons.pen,
                      color: primaryColor,
                    ))
              ]
            : [],
      ),
      body: ListView(
        children: [
          itemTitle(context, "contact.callus"),
          contactItem(context, setting.usaContactNumber, CupertinoIcons.phone,
              onTap: () => _call(setting.usaContactNumber),
              labelKey: "contact.usa.phone"),
          contactItem(
            context,
            setting.mmContactNumber,
            CupertinoIcons.phone,
            onTap: () => _call(
              setting.mmContactNumber,
            ),
            labelKey: "contact.mm.phone",
          ),
          itemTitle(context, "contact.findus"),
          contactItem(
            context,
            setting.usaAddress,
            CupertinoIcons.location,
            labelKey: "contact.usa.address",
          ),
          contactItem(
            context,
            setting.mmAddress,
            CupertinoIcons.location,
            labelKey: "contact.mm.address",
          ),
          itemTitle(context, "contact.emailus"),
          contactItem(
            context,
            setting.emailAddress,
            CupertinoIcons.mail,
            onTap: () => _email(setting.emailAddress),
            labelKey: "contact.fcs.email",
          ),
          itemTitle(context, "contact.visitus"),
          contactItem(
            context,
            setting.facebookLink,
            FontAwesomeIcons.facebook,
            onTap: () => _opencontactItem(setting.facebookLink),
            labelKey: "contact.facebook",
          ),
        ],
      ),
    );
  }

  Future<String> getVersionNumber() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String version = packageInfo.version + "+" + packageInfo.buildNumber;
    return version;
  }

  _call(String phone) {
    launch("tel:${phone.trim().replaceAll(' ', '')}");
  }

  _email(String email) {
    launch("mailto:$email");
  }

  _opencontactItem(String contactItem) {
    launch("$contactItem");
  }
}
