package com.example.paymematedemo2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.qspl.paymemate.PayMeMateHostedActivity;
import com.qspl.paymemate.PreferencesManager;

import java.util.Date;
import java.util.HashMap;

public class ClientActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_cancel, btn_save;
    PreferencesManager preferencesManager;
    HashMap<String, String> hashMapKeys;
    EditText edt_amount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
        preferencesManager = PreferencesManager.getInstance(this);
        hashMapKeys = new HashMap<>();
        btn_save = findViewById(R.id.btn_save);
        edt_amount = findViewById(R.id.edt_amount);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                finish();
                break;

            case R.id.btn_save:

                if (edt_amount.getText().toString().equals("")) {
                    Toast.makeText(ClientActivity.this, "Please enter the amount", Toast.LENGTH_SHORT).show();
                    break;
                }
                HashMap<String, String> hashMapKeys = new HashMap<>();
                hashMapKeys.put("api_id", "jncr9xTebDeIblb70aLYUaX1xDOYdIm2Pz3koT" +
                        "3cB0r886xJbyh37dCbkwsFGLfjkkIfjzf95g/Szs5XtSNNJcXOFPHP/00XT3xNkRtYwVI=");
                hashMapKeys.put("notificationURL", "https://www.paymemate.com/defaultRedirectNotify");
                hashMapKeys.put("paymemateMerchantID", "m0z1r2f3e4v5x6m7q8c9l10g11d12n1388");
                hashMapKeys.put("hostedPageId", "L99BM");
                hashMapKeys.put("randomstr", new Date().getTime() + "");
                hashMapKeys.put("redirectionURL", "https://www.paymemate.com/defaultRedirectNotify");
                hashMapKeys.put("totalamount", edt_amount.getText().toString());
                hashMapKeys.put("uniqueReference", new Date().getTime() + "");
                hashMapKeys.put("usemyfee", "false");
                hashMapKeys.put("cancelURL", "https://www.paymemate.com/defaultRedirectNotify");

                Intent i = new Intent(ClientActivity.this, PayMeMateHostedActivity.class);
                i.putExtra("Request", hashMapKeys);
                startActivityForResult(i, 200);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 200) {
            if (data != null && data.hasExtra("TransactionDetails")) {
                edt_amount.setText("");
                Intent i = new Intent(ClientActivity.this, TransactionDetailsActivity.class);
                i.putExtra("data", data.getStringExtra("TransactionDetails"));
                startActivity(i);
            } else if (data != null && data.hasExtra("Cancelled")) {
                Toast.makeText(ClientActivity.this,
                        data.getStringExtra("Cancelled"), Toast.LENGTH_SHORT).show();
            }

        }

    }


}
