import 'package:flutter/material.dart';
import 'package:flutter_mdd_simulator/screen/TapCard.dart';
import 'package:intl/intl.dart';
import 'package:prepaid_lib_flutter/unik_lib_flutter.dart';

import 'PreferencesHelper.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  PreferencesHelper.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(child: NfcScan()),
      ),
    );
  }
}

class NfcScan extends StatefulWidget {
  NfcScan({Key key}) : super(key: key);

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

class _MyAppState extends State<NfcScan> {
  String _balance = "0", _cardNumber = "000000000000000", _status = "Unknown";
  String _bgCard = "images/mandiri_bg.png";
  bool _isLoading = false;
  String paidAmount;
  String _identifier = 'Unknown';
  String _bankName;

  @override
  void initState() {
    super.initState();
    String mid = "1234567abc";
    int env = 0; // 0 = dev, 1 = prod
    UnikLibFlutter.initUnikLib(mid, env); //initial library
  }

  //function to get card info
  void getCardInfo() async {
    List<String> cardUid = List(1);
    List<String> cardNumber = List(1);
    List<String> balance = List(1);
    List<String> bankName = List(1);
    List<String> cardAttr = List(1);
    List<String> cardInfo = List(1);

    bool isSuccess = await UnikLibFlutter.getCardInfo(
        cardUid, cardNumber, balance, bankName, cardAttr, cardInfo);
    _setStateCard(isSuccess, cardNumber[0], balance[0], bankName[0]);
  }

  //function to update balance
  void updateBalance() async {
    List<String> status = List(1);
    List<String> cardNumber = List(1);
    List<String> balance = List(1);
    List<String> bankName = List(1);
    bool isSuccess = await UnikLibFlutter.updateBalance(
        status, cardNumber, balance, bankName);
    print("status update ${status[0]}");
    this.setState(() {
      _status = status[0];
    });
    _setStateCard(isSuccess, cardNumber[0], balance[0], bankName[0]);
  }

  //function to change UI card and balance information
  void _setStateCard(bool isSuccess, String cardNumber, String balance,
      String bankName) {
    if (isSuccess) {
      this.setState(() {
        _cardNumber = cardNumber ?? "0000000000000000";
        _balance = balance ?? "0";
        _bgCard =
        (bankName == "BNI") ? "images/bni_bg.png" : "images/mandiri_bg.png";
        _bankName = bankName;
      });
    } else {
      print("Something went wrong");
      this.setState(() {
        _bgCard =
        (bankName == "BNI") ? "images/bni_bg.png" : "images/mandiri_bg.png";
        _bankName = bankName;
      });
    }
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    void _showBottomSheet(dynamic screen, dynamic height) {
      showModalBottomSheet(
          isScrollControlled: true,
          shape: RoundedRectangleBorder(
            borderRadius: new BorderRadius.only(
              topLeft: const Radius.circular(25.0),
              topRight: const Radius.circular(25.0),
            ),
          ),
          context: context,
          builder: (builder) {
            return new Container(
              height: MediaQuery
                  .of(context)
                  .size
                  .height * height,
              color: Colors.transparent,
              //could change this to Color(0xFF737373),
              //so you don't have to change MaterialApp canvasColor
              child: new Container(
                  child: new Center(
                    child: screen,
                  )),
            );
          });
    }

    return Scaffold(
      body: Container(
          height: MediaQuery
              .of(context)
              .size
              .height,
          width: MediaQuery
              .of(context)
              .size
              .width,
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: MediaQuery
                      .of(context)
                      .size
                      .width,
                  height: 200,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        fit: BoxFit.fill,
                        image: AssetImage('images/bg_home.png'),
                      )),
                  child: Align(
                    alignment: Alignment(0, 17),
                    child: Container(
                        padding: EdgeInsets.only(left: 20, right: 20),
                        decoration: BoxDecoration(
                          image: DecorationImage(
                              fit: BoxFit.fill, image: AssetImage(_bgCard)),
                          borderRadius: BorderRadius.circular(25),
                          color: Colors.white,
                          boxShadow: [
                            BoxShadow(
                              color: Colors.grey.withOpacity(0.3),
                              spreadRadius: 2,
                              blurRadius: 2,
                              offset:
                              Offset(3, 3), // changes position of shadow
                            ),
                          ],
                        ),
                        width: 322,
                        height: 190,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(
                              height: 70,
                            ),
                            DefaultTextStyle(
                                style: TextStyle(color: Colors.black),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                        _cardNumber.replaceAllMapped(
                                            RegExp(r".{4}"),
                                                (match) => "${match.group(
                                                0)} "),
                                        style: TextStyle(
                                            color: Colors.white, fontSize: 18)),
                                    SizedBox(
                                      height: 17,
                                    ),
                                    Text("Saldo",
                                        style: TextStyle(
                                            color: Colors.white38,
                                            fontSize: 15)),
                                    SizedBox(
                                      height: 3,
                                    ),
                                    Text(
                                        NumberFormat.simpleCurrency(
                                            locale: 'id_ID')
                                            .format(int.parse(_balance)),
                                        style: TextStyle(color: Colors.white)),
                                  ],
                                ))
                          ],
                        )),
                  ),
                ),
                SizedBox(
                  height: 100,
                ),
                Container(
                    width: 300,
                    child: ElevatedButton.icon(
                      label: Text('Update'),
                      icon: Icon(Icons.update_rounded),
                      onPressed: () {
                        updateBalance();
                        _showBottomSheet(TapCard(), 0.50);
                      },
                    )),
                SizedBox(
                  height: 30,
                ),
                Align(
                  alignment: Alignment.center,
                  child:
                  _isLoading ? CircularProgressIndicator() : Text(_status),
                )
              ],
            ),
          )),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          getCardInfo();
          _showBottomSheet(TapCard(), 0.50);
        },
        child: const Icon(Icons.cached),
        backgroundColor: Colors.blue,
      ),
    );
  }

  Future<void> initPlatformState() async {
    // String identifier;
    // try {
    //   identifier = await UniqueIdentifier.serial;
    // } on PlatformException {
    //   identifier = 'Failed to get Unique Identifier';
    // }
    //
    // if (!mounted) return;
    //
    // setState(() {
    //   _identifier = identifier;
    // });
    //
    // print("Unique $_identifier");
  }
}
