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.util.Log;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.card.MaterialCardView;
import com.google.gson.Gson;
import com.yono.messeripos.models.MainViewModels;
import com.yono.messeripos.models.MainViewModelsCart;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.utils.Utils;

import java.util.List;

import static com.yono.messeripos.MainActivity.ORDER_ID;

public class MethodePayment extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    PaymentsModels paymentsModelsIntent, paymentsModels;
    MaterialCardView mcCash, mcBank, mcNfc;
    BottomSheets bottomSheet;
    NfcAdapter nfcAdapter;
    MainViewModelsCart mainViewModels;
    private long total = 0;
    Utils utils = new Utils();
    public static String METHODE_PAYMENT_DATA = "payment_methode_data";
    public static String VIA_CASH = "via_cash";
    public static String VIA_NFC = "via_nfc";

    @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();
        mainViewModels = new ViewModelProvider(MethodePayment.this).get(MainViewModelsCart.class);


        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());

        getdataCheckout();

        Log.i("paymentModels", "onCreate: "+utils.convertGson(paymentsModels));
        Log.i("paymentModelsIntent", "onCreate: "+utils.convertGson(paymentsModelsIntent));

    }

    private void getdataCheckout() {
        if (paymentsModelsIntent == null){
            mainViewModels.getCartById(sharedPreferences.getString(ORDER_ID, "")).observe(this, new Observer<List<ProductCartModels>>() {
                @Override
                public void onChanged(List<ProductCartModels> productCartModels) {
                    String js = new Gson().toJson(productCartModels);
                    Log.i("On payment", "onChanged: "+js);

                    for (ProductCartModels pd : productCartModels){
                        total = total + pd.getPrice_orders()*pd.getQuantity_orders();
                    }

                    paymentsModels.setOrderId(productCartModels.get(0).getId_orders());
                    paymentsModels.setGrossAmount(total);
                    Log.i("paymentModels", "onCreate: "+utils.convertGson(paymentsModels));
                }
            });
        }else{
            mainViewModels.getCartById(paymentsModelsIntent.getOrderId()).observe(this, new Observer<List<ProductCartModels>>() {
                @Override
                public void onChanged(List<ProductCartModels> productCartModels) {
                    String js = new Gson().toJson(productCartModels);
                    Log.i("On Intent_CHANGE", "onChanged: "+paymentsModelsIntent.getOrderId());

                    for (ProductCartModels pd : productCartModels){
                        total = total + pd.getPrice_orders()*pd.getQuantity_orders();
                    }

                    paymentsModelsIntent.setOrderId(productCartModels.get(0).getId_orders());
                    paymentsModelsIntent.setGrossAmount(total);
                    Log.i("paymentModelsIntent", "onCreate: "+utils.convertGson(paymentsModelsIntent));

//                    if (productCartModels.size() != 0){
//                        getSupportActionBar().setTitle(productCartModels.get(0).getId_orders());
//                        paymentsModelsIntent.setOrderId(productCartModels.get(0).getId_orders());
//                        paymentsModelsIntent.setGrossAmount(total);
//                        checkoutAdapter.setCheckoutAdapter(PaymentActivity.this, productCartModels);
//                        recyclerView.setLayoutManager(new LinearLayoutManager(PaymentActivity.this, RecyclerView.VERTICAL, false));
//                        recyclerView.setAdapter(checkoutAdapter);
//                        recyclerView.addItemDecoration(dividerItemDecoration);
////                        grandTotal.setText(utils.convertPrice("Rp. ", total));
//                    }

                }
            });
        }
    }

    private void nfc() {
        if (paymentsModelsIntent == null){
            startActivity(new Intent(MethodePayment.this, ScannerNfc.class)
                .putExtra(VIA_NFC, paymentsModels));
        }

    }

    private void bank() {
        if (paymentsModelsIntent == null){
            paymentsModels.setPaymentType("nfc");
            startActivity(new Intent(MethodePayment.this, PaymentActivity.class)
                    .putExtra(METHODE_PAYMENT_DATA, paymentsModels));
        }
    }

    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");
        }
    }
}
