import 'dart:convert';
import 'package:aman_kassa_flutter/core/base/base_service.dart';
import 'package:aman_kassa_flutter/core/models/aman_dao.dart';
import 'package:flutter/services.dart';

class BankService extends BaseService {
  static const String _url = 'http://185.98.84.231:2000';
  final MethodChannel _channel = MethodChannel('channel:com.amanKassa/bank');


  Future<int> version() async {
    String result;
    try {
      result = await _channel.invokeMethod('version');
    } catch (e, stack) {
      log.e("BankService", e, stack);
      result = '0';
    }
    log.i(result);
    return int.parse(result) ?? 0;
  }

  Future<String> imei() async {
    String result;
    try {
      result = await _channel.invokeMethod('imei');
    } catch (e, stack) {
      log.e("BankService", e, stack);
      result = '0';
    }
    log.i(result);
    return result;
  }

  Future<bool> init() async {
    try {
      String response = await _channel.invokeMethod('init', <String, dynamic>{
        'serverUrl': _url,
      });
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao.success;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return false;
    }
  }

  Future<bool> connect() async {
    try {
      String response = await _channel.invokeMethod("connection");
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao.success;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return false;
    }
  }

  Future<bool> currency() async {
    try {
      String response = await _channel.invokeMethod("currency");
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao.success;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return false;
    }
  }

  Future<bool> permissions() async {
    try {
      String response = await _channel.invokeMethod("permissions");
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao.success;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return false;
    }
  }

  Future<AmanDao> auth({String login, String password}) async {
    try {
      String response = await _channel.invokeMethod("auth",
          <String, dynamic>{'login': login, 'password': password});
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return new AmanDao(msg: 'Ошибка авторизации');
    }
  }

  Future<AmanDao> pay({double amount}) async {
    try {

      double total = amount * 100;
      log.i('total: $total, ${total.toInt()}');
      String response = await _channel.invokeMethod("pay", <String, dynamic>{'amount': total.toInt()});
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return new AmanDao(msg: 'Ошибка оплаты');
    }
  }

  Future<bool> shutdown() async {
    try {
      String response = await _channel.invokeMethod("shutdown");
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao.success;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return false;
    }
  }

  Future<bool> cancel() async {
    try {
      String response = await _channel.invokeMethod("cancel");
      AmanDao dao = AmanDao.fromJson(json.decode(response));
      log.i('${dao.success} - ${dao.msg}');
      return dao.success;
    } catch (e, stack) {
      log.e("BankService", e, stack);
      return false;
    }
  }


}
