package kz.com.aman.kassa.handler;

import android.widget.Toast;

import com.google.gson.Gson;

import io.flutter.plugin.common.MethodChannel;
import kz.com.aman.kassa.MainActivity;
import kz.com.aman.kassa.bank.M4BankActivity;
import kz.com.aman.kassa.model.AmanDao;
import kz.com.aman.kassa.plugins.BankNfcPlugins;
import ru.m4bank.mpos.library.external.authorization.AuthorizationCallbackHandler;
import ru.m4bank.mpos.service.authorization.network.AuthorizationResponse;
import ru.m4bank.mpos.service.commons.data.ResultType;


public class AuthorizationHandlerImpl implements AuthorizationCallbackHandler {
    private final BankNfcPlugins plugin;
    private final String login;
    private final String password;
    private final MethodChannel.Result result;
    private final Gson gson = new Gson();

    public AuthorizationHandlerImpl(BankNfcPlugins plugin, MethodChannel.Result result, String login, String password) {
        this.plugin = plugin;
        this.login = login;
        this.password = password;
        this.result =result;
    }

    @Override
    public void onLoginAndPasswordRequested() {
        plugin.getClientInterface().getAuthorizationManager().sendLoginAndPassword(this.login, this.password);
    }


    @Override
    public void onCompleted(ru.m4bank.mpos.service.result.Result result) {
        plugin.getActivity().runOnUiThread(() ->  {
            //Toast.makeText(plugin.getActivity(), result.getResultType() + " " + (result.getDescription() == null ? "" : result.getDescription()), Toast.LENGTH_SHORT).show();
            AmanDao<String> dao = new AmanDao<>();
            dao.setSuccess(ResultType.SUCCESSFUL.equals(result.getResultType()));
            dao.setMsg(result.getResultType() + " " + (result.getDescription() == null ? "" : result.getDescription()));
            this.result.success(gson.toJson(dao));
        });
    }

    @Override
    public void onWrongApiCalled() {
        plugin.getActivity().runOnUiThread(() -> Toast.makeText(plugin.getActivity(), "Wrong method has been called", Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onRepeat(int attemptNumber) {
        plugin.getActivity().runOnUiThread(() -> Toast.makeText(plugin.getActivity(), "Repeat attempt number " + attemptNumber, Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onCompleted(ru.m4bank.mpos.service.result.Result result, AuthorizationResponse authorizationResponse) {

        plugin.getActivity().runOnUiThread(() -> {
            //Toast.makeText(plugin.getActivity(), result.getResultType() + " " + (result.getDescription() == null ? "" : result.getDescription()), Toast.LENGTH_SHORT).show();
            AmanDao<String> dao = new AmanDao<>();
            dao.setSuccess(ResultType.SUCCESSFUL.equals(result.getResultType()));
            dao.setMsg(result.getResultType() + " " + (result.getDescription() == null ? "" : result.getDescription()));
            this.result.success(gson.toJson(dao));
        });
    }
}


