package kz.com.aman.kassa

import android.app.Activity
import android.content.Intent
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import kz.com.aman.kassa.bank.M4BankActivity
import kz.com.aman.kassa.plugins.BankNfcPlugins


class MainActivity: FlutterActivity() {
    private val BANK_CHANNEL = "channel:com.amanKassa/bank"
    private val ACTIVITY_CHANNEL = "channel:com.amanKassa/activity"

    private lateinit var _result: MethodChannel.Result

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, BANK_CHANNEL).setMethodCallHandler(BankNfcPlugins(this));
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, ACTIVITY_CHANNEL).setMethodCallHandler {
            // Note: this method is invoked on the main thread.
            call, result ->
            if (call.method == "start") {
                _result = result
                val intent= Intent(this, M4BankActivity::class.java)
                startActivityForResult(intent, 8989)

                //result.success("ActivityStarted")
            } else  {
                result.notImplemented()
            }
        }
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 8989) {
            if (resultCode == Activity.RESULT_OK) {
                _result.success(data?.getStringExtra("result"))
            } else if (resultCode == Activity.RESULT_CANCELED) {
                _result.error("008", "123","aaa")
            } else
                _result.success(null)
        }

    }




}
