import 'package:fcs/domain/constants.dart';
import 'package:fcs/domain/entities/faq.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/buying_instruction/buying_online.dart';
import 'package:fcs/pages/faq/faq_edit_page.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/payment_methods/payment_method_page.dart';
import 'package:fcs/pages/rates/shipment_rates.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/fcs_expansion_tile.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'model/faq_model.dart';

const Duration _kExpand = Duration(milliseconds: 200);

class FAQListPage extends StatefulWidget {
  @override
  _FAQListPageState createState() => _FAQListPageState();
}

class _FAQListPageState extends State<FAQListPage>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation<double> _iconTurns;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(duration: _kExpand, vsync: this);
    var _halfTween = Tween<double>(begin: 0.0, end: 0.5);
    var _easeInTween = CurveTween(curve: Curves.easeIn);
    _iconTurns = _controller.drive(_halfTween.chain(_easeInTween));
  }

  @override
  Widget build(BuildContext context) {
    FAQModel faqModel = Provider.of<FAQModel>(context);
    bool isEditable = context.select((MainModel m) => m.faqEditable());

    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: LocalText(
            context,
            "faq.title",
            color: Colors.white,
            fontSize: 20,
          ),
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
          actions: isEditable
              ? [
                  IconButton(
                      onPressed: () => setState(() {
                            isEditMode = !isEditMode;
                          }),
                      icon: Icon(
                        Icons.edit,
                        color: Colors.white,
                      ))
                ]
              : [],
        ),
        floatingActionButton: isEditable
            ? FloatingActionButton.extended(
                onPressed: () {
                  Navigator.of(context).push(
                      CupertinoPageRoute(builder: (context) => FAQEditor()));
                },
                icon: Icon(Icons.add),
                label: LocalText(context, "faq.add.title", color: Colors.white),
                backgroundColor: primaryColor,
              )
            : Container(),
        body: ListView.builder(
            itemCount: faqModel.faqs.length,
            itemBuilder: (BuildContext context, int index) {
              return _faqItem(context, faqModel.faqs[index]);
            }));
  }

  bool isEditMode = false;

  Widget _faqItem(BuildContext context, FAQ faq) {
    bool isEng = Provider.of<LanguageModel>(context).isEng;

    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: FcsExpansionTile(
            isEdit: isEditMode,
            title: TextLocalStyle(
              context,
              faq.question(isEng),
              fontSize: 16,
              fontWeight: FontWeight.w600,
              color: primaryColor,
            ),
            onEditPress: () {
              Navigator.of(context).push<void>(CupertinoPageRoute(
                builder: (context) => FAQEditor(faq: faq),
              ));
            },
            children: [getAnwser(context, faq)],
          ),
        ),
        Divider(
          thickness: 2,
        ),
      ],
    );
  }

  Widget getAnwser(BuildContext context, FAQ faq) {
    bool isEng = Provider.of<LanguageModel>(context).isEng;
    return Column(
      children: [
        TextLocalStyle(
          context,
          faq.answer(isEng),
          fontSize: 16,
          fontWeight: FontWeight.w400,
        ),
        _pageLink(
          faq.pageLink,
          isEng ? faq.pageLinkLabelEng : faq.pageLinkLabelMm,
        ),
      ],
    );
  }

  Widget _pageLink(String linkPage, String text) {
    return linkPage == null || linkPage == "" || text == null || text == ""
        ? Container()
        : Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              FlatButton(
                color: primaryColor,
                onPressed: () => _selectLinkPage(linkPage),
                child: LocalText(context, "", text: text, color: Colors.white),
              )
            ],
          );
  }

  _selectLinkPage(String linkPage) {
    if (linkPage == page_payment_methods) {
      Navigator.of(context)
          .push(CupertinoPageRoute(builder: (context) => PaymentMethodPage()));
    } else if (linkPage == page_buying_instructions) {
      Navigator.of(context)
          .push(CupertinoPageRoute(builder: (context) => BuyingOnlinePage()));
    } else if (linkPage == page_rates) {
      Navigator.of(context)
          .push(CupertinoPageRoute(builder: (context) => ShipmentRates()));
    }
  }
}
