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? closeDayValue;
  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://195.200.74.83:5000',
    });
    setState(() {
      initValue = result;
    });
  }

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

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

  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': '8147'});
    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;
    });
  }

  closeDay() async {
    String result;
    try {
      String response = await _channel.invokeMethod("closeDay");
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      result = '${dao.data} - ${dao.msg}';
    } catch (e) {
      result = (e.toString());
    }
    setState(() {
      closeDayValue = 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>[
                    ElevatedButton(
                        child: Text('Activity m4Bank'), onPressed: activity),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('version: $versionValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('Version'), onPressed: version),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('init: $initValue' , overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('Init'), onPressed: initialize),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('connection: $connectionValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('Connect'), onPressed: connect),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('auth: $authValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(child: Text('Auth'), onPressed: auth),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('cancel: $cancelValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(child: Text('Cancel'), onPressed: cancel),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('shutdown: $shutdownValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('Shutdown'), onPressed: shutdown),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('transaction: $transactionValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('Transaction'), onPressed: transaction),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('get: $getValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('Get'), onPressed: get),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('error: $errorValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('Error'), onPressed: error),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text('error: $closeDayValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                        ElevatedButton(
                            child: Text('CloseDay'), onPressed: closeDay),
                      ],
                    ),
                    Text('pay: $payValue', overflow: TextOverflow.clip, maxLines: 2, softWrap: false),
                    ElevatedButton(child: Text('Payment'), onPressed: pay),
                  ],
                ),
              ),
            ),
    );
  }
}
