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/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(
        floatingActionButton: isEditable
            ? FloatingActionButton.extended(
                onPressed: () {
                  Navigator.of(context).push(BottomUpPageRoute(FAQEditor()));
                },
                icon: Icon(Icons.add),
                label: LocalText(context, "faq.add.title", color: Colors.white),
                backgroundColor: primaryColor,
              )
            : Container(),
        body: CustomScrollView(
          slivers: [
            SliverAppBar(
              leading: IconButton(
                icon: Icon(
                  CupertinoIcons.back,
                  size: 30,
                ),
                onPressed: () => Navigator.of(context).pop(),
              ),
              backgroundColor: primaryColor,
              expandedHeight: 150.0,
              floating: false,
              pinned: true,
              flexibleSpace: FlexibleSpaceBar(
                centerTitle: true,
                titlePadding:
                    EdgeInsets.symmetric(vertical: 10, horizontal: 45),
                title: LocalText(
                  context,
                  "faq.title",
                  fontSize: 20,
                  color: Colors.white,
                ),
              ),
              actions: isEditable
                  ? [
                      IconButton(
                          onPressed: () => setState(() {
                                isEditMode = !isEditMode;
                              }),
                          icon: Icon(
                            Icons.edit,
                            color: Colors.white,
                          ))
                    ]
                  : [],
            ),
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) {
                  return _faqItem(context, faqModel.faqs[index]);
                },
                childCount: faqModel.faqs.length,
              ),
            )
          ],
        ));
  }

  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.w200,
        ),
        _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(BottomUpPageRoute(PaymentMethodPage()));
    } else if (linkPage == page_buying_instructions) {
      Navigator.of(context).push(BottomUpPageRoute(BuyingOnlinePage()));
    }
  }
}
