package com.yono.messeripos;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Window;
import android.view.WindowManager;

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

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.card.MaterialCardView;
import com.yono.messeripos.models.PaymentsModels;

public class MethodePayment extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    PaymentsModels paymentsModelsIntent, paymentsModels;
    MaterialCardView mcCash, mcBank, mcNfc;
    BottomSheets bottomSheet;
    NfcAdapter nfcAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.methode_payment);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

        nfcAdapter= NfcAdapter.getDefaultAdapter(this);

        mcCash = findViewById(R.id.cvCash);
        mcBank = findViewById(R.id.cvBankTransfer);
        mcNfc = findViewById(R.id.cvNFC);

        sharedPreferences = getSharedPreferences(MainActivity.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
        paymentsModelsIntent = getIntent().getParcelableExtra(CartActivity.CODE_SEND_CHECKOUT_HISTORY);
        paymentsModels = new PaymentsModels();


        MaterialToolbar toolbars = findViewById(R.id.appBarPaymentMethode);
        setSupportActionBar(toolbars);
        getSupportActionBar().setTitle("Methode Payment");


        if (nfcAdapter == null){
            mcNfc.setEnabled(false);
        }else{
            mcNfc.setEnabled(true);
        }

        mcCash.setOnClickListener(view -> cash());
        mcBank.setOnClickListener(view -> bank());
        mcNfc.setOnClickListener(view -> nfc());

    }

    private void nfc() {
        startActivity(new Intent(MethodePayment.this, ScannerNfc.class));
    }

    private void bank() {
        startActivity(new Intent(MethodePayment.this, PaymentActivity.class));
    }

    private void cash() {
        if (paymentsModelsIntent == null){
            bottomSheet = new BottomSheets(paymentsModels,0);
            bottomSheet.setCancelable(true);
            bottomSheet.show(getSupportFragmentManager(),
                    "ModalBottomSheet");
        }else{
            bottomSheet = new BottomSheets(paymentsModelsIntent,1);
            bottomSheet.setCancelable(true);
            bottomSheet.show(getSupportFragmentManager(),
                    "ModalBottomSheet");
        }
    }
}
