package com.yono.messeripos;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.PaymentService;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.models.VaNumberModels;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.utils.Utils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

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

public class BottomSheets extends BottomSheetDialogFragment {

    EditText inputAmount;
    TextView hargaChange, totalHarga;
    Button btnCash;
    Utils utils;
    String defaultText = "0";
    Client client = new Client();
    public static long grossAmount, input, result;
    SharedPreferences sharedPreferences;
    public static BottomSheetBehavior bottomSheetBehavior;
    RelativeLayout layoutFormCash;
    int statusUpdatePayment = 0;

    public static PaymentsModels paymentsModels;

    public BottomSheets(PaymentsModels paymentsModels, int statusUpdatePayment) {
        this.paymentsModels = paymentsModels;
        this.statusUpdatePayment = statusUpdatePayment;
    }

    public interface CashListener {
        void onCash(PaymentsModels paymentsModels);
    }

    private CashListener listener;

    public void setListener(CashListener listener) {
        this.listener = listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable
            ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_bottom_sheets,
                container, false);

        layoutFormCash = v.findViewById(R.id.form_cash);
        inputAmount = v.findViewById(R.id.etBayar);
        hargaChange = v.findViewById(R.id.hargaChange);
        btnCash = v.findViewById(R.id.btnCash);
        totalHarga = v.findViewById(R.id.totalHarga);
        utils = new Utils();
        btnCash.setEnabled(false);
//        bottomSheetBehavior = BottomSheetBehavior.from(layoutFormCash);
        sharedPreferences = getActivity().getSharedPreferences(MainActivity.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
//        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

        Log.i("BITTOM", "BottomSheets: "+utils.convertGson(paymentsModels)+"ID "+statusUpdatePayment);

        long ga = paymentsModels.getGrossAmount();
        String setCurrency = utils.convertPrice("Rp.", ga);

        totalHarga.setText(setCurrency);
        hargaChange.setText(defaultText);

//        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
//            @Override
//            public void onStateChanged(@NonNull View bottomSheet, int newState) {
//                switch (newState) {
//                    case BottomSheetBehavior.STATE_HIDDEN:
//                        break;
//                    case BottomSheetBehavior.STATE_EXPANDED:
//                        break;
//                    case BottomSheetBehavior.STATE_COLLAPSED:
//                        break;
//                    case BottomSheetBehavior.STATE_DRAGGING:
//                        break;
//                    case BottomSheetBehavior.STATE_SETTLING:
//                        break;int
//                }
//            }

//            @Override
//            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//                if (bottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
//                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
//                } else {
//                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
//                }
//            }
//        });

        inputAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (TextUtils.isEmpty(charSequence)) inputAmount.setText("0");

                grossAmount = paymentsModels.getGrossAmount();
                input = Long.parseLong(inputAmount.getText().toString());
                result = input - grossAmount;

                if (result >= 0) {
                    btnCash.setEnabled(true);
                } else {
                    btnCash.setEnabled(false);
                }

                hargaChange.setText(utils.convertPrice("Rp.", result));

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        btnCash.setOnClickListener(view -> bayar(view));

        return v;
    }

    private void bayar(View view) {

//        String orderId, String paymentType, String bank, long grossAmount
        PaymentsModels paymentsModels1 = new PaymentsModels();
        paymentsModels1.setTransactionStatus("success");
        paymentsModels1.setPaymentType("cash");
        paymentsModels1.setGrossAmount(grossAmount);
        paymentsModels1.setBank("");

        PaymentService paymentService = client.Client(PaymentService.class);

        if (statusUpdatePayment == 0){
            paymentsModels1.setOrderId(sharedPreferences.getString(ORDER_ID, ""));
            view.getContext().startActivity(new Intent(getActivity(), TransactionDetail.class)
                    .putExtra(MethodePayment.VIA_CASH, paymentsModels1));
//            paymentsModels1.setOrderId(sharedPreferences.getString(ORDER_ID, ""));
//            paymentService.postFromCart(paymentsModels1).enqueue(new Callback<DataResponse<PaymentsModels<List<VaNumberModels>>>>() {
//                @Override
//                public void onResponse(Call<DataResponse<PaymentsModels<List<VaNumberModels>>>> call, Response<DataResponse<PaymentsModels<List<VaNumberModels>>>> response) {
//                    Log.i("CASH_0", "onResponse: "+utils.convertGson(response.body()));
//                    if (response.body() != null){
//                        SharedPreferences.Editor editor = PaymentActivity.sharedPreferences.edit();
//                        editor.putBoolean(CHECKOUT, false);
//                        editor.putString(ORDER_ID, "");
//                        if (editor.commit()){
//                            Intent i = new Intent(getActivity(), MainActivity.class);
//                            view.getContext().startActivity(i);
//                        }
//                    }
//                }
//
//                @Override
//                public void onFailure(Call<DataResponse<PaymentsModels<List<VaNumberModels>>>> call, Throwable t) {
//
//                }
//            });
        }else{
            paymentsModels.setTransactionStatus("success");
            paymentsModels.setPaymentType("cash");

        }



    }
}
