import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

import '../widgets/label_widgets.dart';

final log = Logger('Util');

Future showMsgDialog(BuildContext context, String title, String msg) {
  return showDialog(
    context: context,
    builder: (_) {
      return AlertDialog(
        title: new Text(title),
        content: new Text(msg),
        actions: <Widget>[
          new FlatButton(
            child: new Text("Close"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

Future<void> showConfirmDialog(
    BuildContext context, String translationKey, ok(),
    {List<String> translationVariables}) async {
  await showDialog(
      context: context,
      builder: (_) {
        return AlertDialog(
          title: Center(
            child: LocalText(
              context,
              translationKey,
              translationVariables: translationVariables,
              color: primaryColor,
            ),
          ),
          content: Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                FlatButton(
                    color: Colors.grey[300],
                    child: Text(
                      AppTranslations.of(context).text('btn.cancel'),
                      style: Provider.of<LanguageModel>(context).isEng
                          ? TextStyle()
                          : TextStyle(fontFamily: 'Myanmar3'),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop();
                    }),
                SizedBox(
                  width: 0,
                ),
                FlatButton(
                    color: primaryColor,
                    child: Text(AppTranslations.of(context).text('btn.ok'),
                        style: Provider.of<LanguageModel>(context).isEng
                            ? TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold)
                            : TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                fontFamily: 'Myanmar3')),
                    onPressed: () async {
                      Navigator.of(context).pop();
                      await ok();
                    })
              ],
            ),
          ),
        );
      });
}

Widget getStatus(String status) {
  return status == "Delivered"
      ? Text(status,
          style: TextStyle(
              color: primaryColor, fontSize: 18, fontWeight: FontWeight.bold))
      : status == "rejected"
          ? Chip(
              backgroundColor: Colors.red,
              avatar: Icon(
                Icons.remove,
                color: Colors.white,
                size: 14,
              ),
              label: Text(
                status,
                style: TextStyle(color: Colors.white, fontSize: 12),
              ))
          : status == "In progress"
              ? Text(
                  status,
                  style: TextStyle(color: Colors.white, fontSize: 12),
                )
              : status == "Pickuped"
                  ? Text(
                      status,
                      style: TextStyle(
                          color: primaryColor,
                          fontSize: 18,
                          fontWeight: FontWeight.bold),
                    )
                  : status == "Pending" || status == "Rescheduled"
                      ? Row(
                          children: <Widget>[
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Icon(Icons.schedule),
                            ),
                            Text(
                              status,
                              style: TextStyle(
                                  color: primaryColor,
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold),
                            )
                          ],
                        )
                      : status == "Assigned"
                          ? Row(
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Icon(Icons.check),
                                ),
                                Text(
                                  status,
                                  style: TextStyle(
                                      color: primaryColor,
                                      fontSize: 18,
                                      fontWeight: FontWeight.bold),
                                )
                              ],
                            )
                          : status == "Canceled"
                              ? Text(
                                  status,
                                  style: TextStyle(
                                      color: primaryColor,
                                      fontSize: 18,
                                      fontWeight: FontWeight.bold),
                                )
                              : status == "Delivered" || status == "Avaliable"
                                  ? Text(
                                      status,
                                      style: TextStyle(
                                          color: Colors.green, fontSize: 18),
                                    )
                                  : status == "Used"
                                      ? Text(
                                          status,
                                          style: TextStyle(
                                              color: Colors.red, fontSize: 18),
                                        )
                                      : status == "Paid"
                                          ? Row(
                                              children: <Widget>[
                                                Padding(
                                                  padding:
                                                      const EdgeInsets.all(8.0),
                                                  child: Icon(Icons.check),
                                                ),
                                                Text(
                                                  status,
                                                  style: TextStyle(
                                                      color: primaryColor,
                                                      fontSize: 18,
                                                      fontWeight:
                                                          FontWeight.bold),
                                                )
                                              ],
                                            )
                                          : Text(
                                              status ?? "",
                                              style: TextStyle(
                                                  color: primaryColor,
                                                  fontSize: 18,
                                                  fontWeight: FontWeight.bold),
                                            );
}

call(BuildContext context, String phone) {
  showConfirmDialog(context, "contact.phone.confim", () => launch("tel:$phone"),
      translationVariables: ["$phone"]);
}

Widget nameWidget(String name) {
  return Center(
    child: Padding(
      padding: const EdgeInsets.only(left: 10.0, top: 8),
      child: Text(
        name,
        style: TextStyle(
            color: Colors.black87, fontSize: 18, fontWeight: FontWeight.bold),
      ),
    ),
  );
}

Widget phoneWidget(BuildContext context, String phone) {
  return Container(
    padding: EdgeInsets.only(top: 10),
    child: Row(
      children: <Widget>[
        Icon(Icons.phone),
        Padding(
          padding: const EdgeInsets.only(right: 8.0),
          child: labeledText(context, phone, "user.phone"),
        ),
      ],
    ),
  );
}

Widget fcsInput(String label, IconData iconData,
    {TextEditingController controller,
    String value,
    bool autoFocus = false,
    TextInputType textInputType}) {
  return TextFormField(
      initialValue: value,
      controller: controller,
      cursorColor: primaryColor,
      maxLines: null,
      minLines: 1,
      autofocus: autoFocus,
      keyboardType: textInputType,
      decoration: InputDecoration(
        fillColor: Colors.white,
        labelText: label,
        labelStyle: TextStyle(fontSize: 16, color: Colors.grey),
        filled: true,
        icon: Icon(
          iconData,
          color: primaryColor,
        ),
        focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.grey, width: 1.0)),
      ));
}

Widget fcsInputReadOnly(String label, IconData iconData,
    {TextEditingController controller, String value}) {
  return TextFormField(
      initialValue: value,
      controller: controller,
      cursorColor: primaryColor,
      maxLines: null,
      minLines: 1,
      readOnly: true,
      decoration: InputDecoration(
        fillColor: Colors.white,
        border: InputBorder.none,
        labelText: label,
        labelStyle: TextStyle(fontSize: 16, color: Colors.grey),
        filled: true,
        icon: Icon(
          iconData,
          color: primaryColor,
        ),
      ));
}

Widget fcsDropDown(String label, IconData iconData,
    {TextEditingController controller}) {
  return Row(
    children: <Widget>[
      Padding(
        padding: const EdgeInsets.only(right: 8.0),
        child: Icon(iconData),
      ),
      Expanded(
          child: Container(
        height: 50.0,
        child: Row(children: <Widget>[
          Expanded(child: _dropDown()),
        ]),
      )),
    ],
  );
}

Widget _dropDown() {
  return DropdownButton<String>(
    value: "Ko Nge",
    icon: Icon(Icons.arrow_downward),
    iconSize: 24,
    elevation: 16,
    // style: TextStyle(color: Colors.deepPurple),
    underline: Container(
      height: 2,
      color: primaryColor,
    ),
    onChanged: (String newValue) {},
    items: <String>['Ko Nge', 'Two', 'Free', 'Four']
        .map<DropdownMenuItem<String>>((String value) {
      return DropdownMenuItem<String>(
        value: value,
        child: Text(value),
      );
    }).toList(),
  );
}

Widget fcsButton(BuildContext context, String text,
    {Function callack, IconData iconData}) {
  var languageModel = Provider.of<LanguageModel>(context);

  var style = languageModel.isEng
      ? TextStyle(
          fontSize: 16.0, color: Colors.white, fontWeight: FontWeight.bold)
      : TextStyle(
          fontSize: 16.0,
          color: Colors.white,
          fontWeight: FontWeight.bold,
          fontFamily: "Myanmar3");

  return Container(
    padding: EdgeInsets.only(left: 10, right: 10, top: 10),
    child: Container(
      height: 45.0,
      decoration: BoxDecoration(
        color: primaryColor,
        shape: BoxShape.rectangle,
      ),
      child: ButtonTheme(
        minWidth: 900.0,
        height: 100.0,
        child: FlatButton(
          onPressed: callack,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              iconData == null
                  ? Container()
                  : Icon(
                      iconData,
                      color: Colors.white,
                    ),
              SizedBox(
                width: 15,
              ),
              Text(text, style: style),
            ],
          ),
        ),
      ),
    ),
  );
}

String getLocalString(BuildContext context, String key) {
  return AppTranslations.of(context).text(key);
}

void showToast(GlobalKey key, String text) {
  final ScaffoldState scaffold = key.currentState;
  scaffold.showSnackBar(
    SnackBar(
      content: Text(text),
      backgroundColor: secondaryColor,
      duration: Duration(seconds: 1),
    ),
  );
}

bool hasUnicode(String text) {
  final int maxBits = 128;
  List<int> unicodeSymbols =
      text.codeUnits.where((ch) => ch > maxBits).toList();
  return unicodeSymbols.length > 0;
}
