import 'dart:convert';

import 'package:fcs/domain/entities/setting.dart';
import 'package:fcs/domain/vo/term.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/term/term_edit.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 'package:zefyr/zefyr.dart';

typedef void ProfileCallback();

class TermPage extends StatefulWidget {
  const TermPage({
    Key key,
  }) : super(key: key);
  @override
  _TermPageState createState() => _TermPageState();
}

class _TermPageState extends State<TermPage> {
  ZefyrController _controller;
  FocusNode _focusNode;
  NotusDocument document = new NotusDocument();
  bool isLoading = false;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode();
  }

  NotusDocument _loadDocument(Setting setting) {
    bool isEng = Provider.of<LanguageModel>(context).isEng;
    String term = isEng ? setting.termsEng : setting.termsMm;
    NotusDocument doc;
    try {
      doc = NotusDocument.fromJson(jsonDecode(term));
    } catch (e) {}
    if (doc == null) {
      doc = NotusDocument();
    }
    return doc;
  }

  @override
  Widget build(BuildContext context) {
    Setting setting = Provider.of<MainModel>(context).setting;
    bool isEditable = context.select((MainModel m) => m.termEditable());

    return Scaffold(
      appBar: AppBar(
        shadowColor: Colors.transparent,
        backgroundColor: Colors.white,
        centerTitle: true,
        leading: new IconButton(
          icon: new Icon(
            CupertinoIcons.back,
            color: primaryColor,
          ),
          onPressed: () => Navigator.of(context).pop(),
        ),
        title: LocalText(
          context,
          'term',
          color: primaryColor,
          fontSize: 20,
        ),
        actions: isEditable
            ? [
                IconButton(
                    onPressed: () =>
                        Navigator.of(context).push<void>(CupertinoPageRoute(
                          builder: (context) =>
                              TermEdit(term: Term.fromSetting(setting)),
                        )),
                    icon: Icon(
                      CupertinoIcons.pen,
                      color: primaryColor,
                    ))
              ]
            : [],
      ),
      body: ZefyrTheme(
          data: ZefyrThemeData().copyWith(
            defaultLineTheme: LineTheme(
              padding: EdgeInsets.all(0),
              textStyle: TextStyle(fontFamily: "Myanmar3"),
            ),
          ),
          // data: ZefyrThemeData().copyWith(
          //     defaultLineTheme: LineTheme(
          //         textStyle: TextStyle(color: Colors.black),
          //         padding: EdgeInsets.all(0))),
          child: ZefyrScaffold(
            child: ZefyrEditor(
              mode: ZefyrMode.view,
              padding: EdgeInsets.all(16),
              controller: ZefyrController(_loadDocument(setting)),
              focusNode: _focusNode,
            ),
          )),
    );
  }
}
