import 'package:fcs/domain/entities/faq.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/faq/faq_edit_page.dart';
import 'package:fcs/pages/faq/model/faq_model.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class FAQDetailPage extends StatefulWidget {
  final FAQ faq;
  const FAQDetailPage({this.faq});
  @override
  _FAQDetailPageState createState() => _FAQDetailPageState();
}

class _FAQDetailPageState extends State<FAQDetailPage> {
  bool _isLoading = false;
  FAQ faq;

  intState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    faq = context.select((FAQModel m) => m.getFAQ(widget.faq.id));
    if (faq == null) return Text("Deleted");

    bool isEditable = context.select((MainModel m) => m.faqEditable());
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        body: CustomScrollView(slivers: [
          SliverAppBar(
            leading: IconButton(
              icon: Icon(
                CupertinoIcons.back,
                color: primaryColor,
                size: 50,
              ),
              onPressed: () => Navigator.of(context).pop(),
            ),
            backgroundColor: Colors.white,
            expandedHeight: 100.0,
            floating: false,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              titlePadding: EdgeInsets.symmetric(vertical: 10),
            ),
            actions: isEditable
                ? [
                    IconButton(
                        onPressed: () {
                          showConfirmDialog(context, "faq.edit.delete.confirm",
                              () {
                            _delete();
                          });
                        },
                        icon: Icon(
                          CupertinoIcons.delete,
                          color: primaryColor,
                          size: 30,
                        )),
                    IconButton(
                        onPressed: () =>
                            Navigator.of(context).push<void>(CupertinoPageRoute(
                              builder: (context) => FAQEditor(faq: faq),
                            )),
                        icon: Icon(
                          CupertinoIcons.pen,
                          color: primaryColor,
                        ))
                  ]
                : [],
          ),
          SliverList(
              delegate: SliverChildListDelegate([
            Padding(
              padding: const EdgeInsets.all(28.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  getQuestion(context, faq),
                  SizedBox(
                    height: 50,
                  ),
                  getAnwser(context, faq)
                ],
              ),
            ),
          ]))
        ]),
      ),
    );
  }

  Widget getQuestion(BuildContext context, FAQ faq) {
    bool isEng = Provider.of<LanguageModel>(context).isEng;
    return TextLocalStyle(
      context,
      faq.question(isEng),
      fontSize: 22,
      fontWeight: FontWeight.bold,
    );
  }

  Widget getAnwser(BuildContext context, FAQ faq) {
    bool isEng = Provider.of<LanguageModel>(context).isEng;
    return TextLocalStyle(
      context,
      faq.answer(isEng),
      fontSize: 16,
      fontWeight: FontWeight.w200,
    );
  }

  _delete() async {
    setState(() {
      _isLoading = true;
    });
    try {
      FAQModel faqModel = Provider.of<FAQModel>(context, listen: false);
      await faqModel.deleteFAQ(faq);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
