import 'dart:async';

import 'package:checker/base/base_screen.dart';
import 'package:checker/db.dart';
import 'package:checker/resources.dart';
import 'package:checker/strings.dart';
import 'package:flutter/material.dart';

import 'package:checker/base/base_state.dart';
import 'package:checker/consts.dart';
import 'package:checker/common.dart';

/// Класс содержит заголовки и текст блоков FAQ.
class Entry {
  Entry(this.title, this.text);
  final String title;
  final String text;
}

class EntryItem extends StatelessWidget {

  const EntryItem(this.entry);
  final Entry entry;

  bool isAutomaticallyImplyLeading() => true;

  Widget _buildTiles(BuildContext context, Entry root) {
    EdgeInsets margin = new EdgeInsets.only(left: 20.0, right: 20.0);
    TextStyle titleStyle = Theme.of(context).textTheme.button.copyWith(
        fontWeight: FontWeight.bold,
        color: faqTitlesColor);
    return new Container(margin: margin, child: new Card(
        child: new ExpansionTile(
            key: new PageStorageKey<Entry>(root),
            title:new Text(
                root.title,
                style: titleStyle),
            children: [
              new Container(
                  margin: margin,
                  padding: new EdgeInsets.only(top: 12.0, bottom: 20.0),
                  child: new Text(
                      root.text,
                      style: new TextStyle(
                          fontWeight: FontWeight.w300,
                          color: faqGrey,
                          fontSize: 14.0)
                  ),
                  decoration: new BoxDecoration(
                      border: new Border(
                          top: new BorderSide(
                              color: greyTextColor,
                              width: 0.5)
                      )
                  )
              )
            ]
        )
    )
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildTiles(context, entry);
  }
}

class FAQScreen extends BaseScreen {

  FAQScreen(SqliteHelper helper, String app) : super(helper, app);

  @override State createState() => new FAQScreenState<FAQScreen>(helper, app);
}

class FAQScreenState<T> extends BaseState<FAQScreen> {

  FAQScreenState(SqliteHelper helper, String app) : super(helper, app);

  List<Entry> data;

  @override String getTitle() {
    return StringsLocalization.help();
  }

  @override String getHintString() {
    return null;
  }

  @override
  void initState() {
    initPhoneAndUrl().then((_) {
      super.initState();
    });
  }

  @override Widget build(BuildContext context) {
    return new Scaffold(appBar: getAppBar(), body: getScreenContent());
  }

  Future initPhoneAndUrl() async {
    initHelp(await platform.invokeMethod('getSupportPhone'),
        await platform.invokeMethod('getSupportUrl'));
  }

  void initHelp(String phone, String url) {
    data = <Entry>[
      new Entry(StringsLocalization.registration(), StringsLocalization.registrationGuide()),
      new Entry(StringsLocalization.usage(), StringsLocalization.usageGuide()),
      new Entry(StringsLocalization.support(), StringsLocalization.supportGuide(phone, url)),
      new Entry(StringsLocalization.common(), StringsLocalization.commonGuide())
    ];
  }

  @override List<Widget> getMenuButtons() {
    return null;
  }

  /// Метод возвращает ListView с блоками faq.
  @override Widget getScreenContent() {
    if (data == null) {
      return getBackground();
    } else {
      return new ListView.builder(
          itemBuilder: (BuildContext context, int index) =>
          new EntryItem(data[index]),
          itemCount: data.length);
    }
  }

  @override bool isAutomaticallyImplyLeading() {
    return true;
  }
}
