import 'dart:convert';

import 'package:aman_kassa_flutter/core/models/aman_dao.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class BankView extends StatefulWidget {
  BankView();

  @override
  _BankViewState createState() => _BankViewState();
}

class _BankViewState extends State<BankView> {
  static const MethodChannel _channel =
      MethodChannel('channel:com.amanKassa/bank');
  static const MethodChannel _activity =
      MethodChannel('channel:com.amanKassa/activity');
  String initValue;
  String connectionValue;
  String authValue;
  String payValue;
  String cancelValue;
  String shutdownValue;
  String versionValue;
  String transactionValue;
  String getValue;
  String errorValue;
  bool loading = false;

  @override
  void initState() {
    super.initState();
    //load();
  }

  initialize() async {
    String result = await _channel.invokeMethod('init', <String, dynamic>{
      'serverUrl': 'http://185.98.84.231:2000',
    });
    setState(() {
      initValue = result ?? 'none';
    });
  }

  version() async {
    String result = await _channel.invokeMethod('version');
    setState(() {
      versionValue = result ?? 'none';
    });
  }

  transaction() async {
    String result = await _channel.invokeMethod('transaction');
    setState(() {
      transactionValue = result ?? 'none';
    });
  }

  connect() async {
    String result = await _channel.invokeMethod("connection");
    setState(() {
      connectionValue = result;
    });
  }

  auth() async {
    String result = await _channel.invokeMethod("auth",
        <String, dynamic>{'login': 'uvaissov@gmail.com', 'password': '1234'});
    setState(() {
      authValue = result;
    });
  }

  pay() async {
    String result;
    try {
      result =
          await _channel.invokeMethod("pay", <String, dynamic>{'amount': 100});
    } catch (e) {
      result = (e.toString());
    }
    setState(() {
      payValue = result;
    });
  }

  cancel() async {
    String result;
    try {
      result = await _channel.invokeMethod("cancel");
    } catch (e) {
      result = (e.toString());
    }
    setState(() {
      cancelValue = result;
    });
  }

  shutdown() async {
    String result;
    try {
      result = await _channel.invokeMethod("shutdown");
    } catch (e) {
      result = (e.toString());
    }
    setState(() {
      shutdownValue = result;
    });
  }

  get() async {
    String result;
    try {
      String response = await _channel.invokeMethod("get");
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      result = '${dao.data} - ${dao.msg}';
    } catch (e) {
      result = (e.toString());
    }
    setState(() {
      getValue = result;
    });
  }

  error() async {
    String result;
    try {
      String json = await _channel.invokeMethod("error");
      dynamic data = JsonDecoder().convert(json);
      result = data["msg"];
    } catch (e) {
      result = (e.toString());
    }
    setState(() {
      errorValue = result;
    });
  }

  activity() async {
    String result = await _activity.invokeMethod("start");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Банковская страница'),
      ),
      body: loading
          ? Container(child: Center(child: CircularProgressIndicator()))
          : SingleChildScrollView(
              child: Container(
                margin: const EdgeInsets.symmetric(horizontal: 14.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    RaisedButton(
                        child: Text('Activity m4Bank'), onPressed: activity),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('version: $versionValue'),
                        RaisedButton(
                            child: Text('Version'), onPressed: version),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('init: $initValue'),
                        RaisedButton(
                            child: Text('Init'), onPressed: initialize),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('connection: $connectionValue'),
                        RaisedButton(
                            child: Text('Connect'), onPressed: connect),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('auth: $authValue'),
                        RaisedButton(child: Text('Auth'), onPressed: auth),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('cancel: $cancelValue'),
                        RaisedButton(child: Text('Cancel'), onPressed: cancel),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('shutdown: $shutdownValue'),
                        RaisedButton(
                            child: Text('Shutdown'), onPressed: shutdown),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('transaction: $transactionValue'),
                        RaisedButton(
                            child: Text('Transaction'), onPressed: transaction),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('get: $getValue'),
                        RaisedButton(
                            child: Text('Get'), onPressed: get),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('error: $errorValue'),
                        RaisedButton(
                            child: Text('Error'), onPressed: error),
                      ],
                    ),
                    Text('pay: $payValue'),
                    RaisedButton(child: Text('Payment'), onPressed: pay),
                  ],
                ),
              ),
            ),
    );
  }
}
