import 'package:checker/base/base_state.dart';
import 'package:checker/common.dart';
import 'package:checker/consts.dart';
import 'package:checker/db.dart';
import 'package:checker/strings.dart';
import 'package:flutter/material.dart';

/// Экран проведения покупки.
class PurchaseSuccessScreen extends StatefulWidget {

  PurchaseSuccessScreen(this.val, this.name, this.helper, this.app);

  final String val;
  final String name;
  final String app;
  final SqliteHelper helper;

  @override State createState() =>
      new PurchaseSuccessScreenState(val, name, helper, app);
}

class PurchaseSuccessScreenState<T> extends BaseState<PurchaseSuccessScreen> {

  PurchaseSuccessScreenState(String sum, String username, SqliteHelper helper,
      String app) {
    this.sum = sum;
    this.username = username;
    this.helper = helper;
    this.app = app;
  }

  String sum, username;
  int currency;

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

  @override String getHintString() {
    return null;
  }

  @override Widget build(BuildContext context) {
    if (currency == null) {
      helper.getCurrency().then((currency) {
        setState(() {
          this.currency = currency;
        });
      });
    }
    return getMainWidget();
  }

  @override Widget getScreenContent() {
    return new Column(children: <Widget>[
      getValueWithDescription(StringsLocalization.buyer(), username),
      getSuccessMessage(),
      new Expanded(child: new Center()),
      wrapButton(getScreenMargins(74.0), getScanButton())
    ]);
  }

  getScreenMargins(double bottom) {
    double side = 42.0;
    return new EdgeInsets.only(bottom: bottom, left: side, right: side);
  }

  getScanButton() {
    String title = StringsLocalization.scan();
    return buildRaisedButton(title, () => startScanner(context, app, helper));
  }

  getSuccessMessage() {
    return new Row(children: <Widget>[new Expanded(child: new Container(
        margin: new EdgeInsets.only(top: 20.0), height: 64.0,
        decoration: new BoxDecoration(color: greenBackground),
        child: new Center(
            child: new Text(getMessageTitle(), textAlign: TextAlign.center,
                style: new TextStyle(fontWeight: FontWeight.bold,
                    color: tokenActiveTextColor)))))
    ]);
  }

  getMessageTitle() {
    if (currency != null) {
      return StringsLocalization.purchaseCompleted(sum, currency);
    } else {
      return '';
    }
  }

}