package com.cscodetech.movers.ui;

import static com.cscodetech.movers.utils.SessionManager.currency;
import static com.cscodetech.movers.utils.SessionManager.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cscodetech.movers.R;
import com.cscodetech.movers.adepter.PaymentAdapter;
import com.cscodetech.movers.databinding.ActivityPaymentBinding;
import com.cscodetech.movers.model.Payment;
import com.cscodetech.movers.model.PaymentdataItem;
import com.cscodetech.movers.model.UserLogin;
import com.cscodetech.movers.retrofit.APIClient;
import com.cscodetech.movers.retrofit.GetResult;
import com.cscodetech.movers.ui.bkash.BkashPaymenrActivity;
import com.cscodetech.movers.utils.CustPrograssbar;
import com.cscodetech.movers.utils.Utility;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

public class PaymentActivity extends BaseActivity implements PaymentAdapter.RecyclerTouchListener, GetResult.MyListener {

    private ActivityPaymentBinding binding;
    UserLogin userLogin;
    CustPrograssbar custPrograssbar;
    List<PaymentdataItem> paymentList = new ArrayList<>();
    String bidAmount;
    PaymentdataItem paymentItemr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPaymentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        custPrograssbar = new CustPrograssbar();

        userLogin = sessionmanager.getUserDetails();

        bidAmount = getIntent().getStringExtra("tAmount");

        binding.txtTpayment.setText(sessionmanager.getStringData(currency) + bidAmount);
        binding.txtSubtotal.setText(sessionmanager.getStringData(currency) + bidAmount);
        binding.btnContinus.setText(getResources().getString(R.string.continues) +" "+ sessionmanager.getStringData(currency)+bidAmount);


        binding.recyclerViewPayment.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.recyclerViewPayment.setItemAnimator(new DefaultItemAnimator());
        getPaymentList();

        if (sessionmanager.getFloatData(wallet) == 0) {
            binding.lvlWalletRow.setVisibility(View.GONE);
            binding.lvlWallet.setVisibility(View.GONE);
        }else {
            binding.txtMywallet.setText(getResources().getString(R.string.balance) + sessionmanager.getStringData(currency) + sessionmanager.getFloatData(wallet));
        }
        Utility.getInstance().payAmount=Double.parseDouble(bidAmount);


        binding.swichtwallet.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                if (sessionmanager.getFloatData(wallet) < Float.parseFloat(bidAmount)) {
                    double t = Double.parseDouble(bidAmount) - sessionmanager.getFloatData(wallet);
                    binding.txtMywallet.setText(getResources().getString(R.string.balance) +" "+ sessionmanager.getStringData(currency) + "0");
                    Utility.getInstance().tWallet = sessionmanager.getFloatData(wallet);
                    binding.lvlPaymentinfo.setVisibility(View.VISIBLE);
                    binding.txtWallet.setText(sessionmanager.getStringData(currency) + Utility.getInstance().tWallet);
                    binding.txtTpayment.setText(sessionmanager.getStringData(currency)+t);
                    binding.btnContinus.setText(getResources().getString(R.string.continues) +" "+ sessionmanager.getStringData(currency)+t);
                    Utility.getInstance().payAmount=t;
                } else {
                    double t = sessionmanager.getFloatData(wallet) - Double.parseDouble(bidAmount);
                    binding.txtMywallet.setText(getResources().getString(R.string.balance) +" "+ sessionmanager.getStringData(currency) + t);
                    binding.txtTpayment.setText(sessionmanager.getStringData(currency) + "0");
                    binding.btnContinus.setText(getResources().getString(R.string.continues) + " With wallet");

                    Utility.getInstance().tWallet = Double.parseDouble(bidAmount);
                    binding.lvlPaymentinfo.setVisibility(View.GONE);
                    binding.txtWallet.setText(sessionmanager.getStringData(currency) + Utility.getInstance().tWallet);
                    Utility.getInstance().payAmount=0.0;
                }
            } else {
                binding.txtTpayment.setText(sessionmanager.getStringData(currency) + Double.parseDouble(bidAmount));
                binding.txtMywallet.setText(getResources().getString(R.string.balance) +" "+ sessionmanager.getStringData(currency) + sessionmanager.getFloatData(wallet));
                binding.lvlPaymentinfo.setVisibility(View.VISIBLE);
                Utility.getInstance().tWallet = 0;
                binding.txtWallet.setText(sessionmanager.getStringData(currency) + Utility.getInstance().tWallet);
                binding.btnContinus.setText(getResources().getString(R.string.continues) +" "+ sessionmanager.getStringData(currency)+bidAmount);
                Utility.getInstance().payAmount=Double.parseDouble(bidAmount);
            }
        });
        binding.imgBack.setOnClickListener(this::onBindClick);
        binding.btnContinus.setOnClickListener(this::onBindClick);

    }

    private void getPaymentList() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", userLogin.getId());


            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().paymentgateway(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "2");
            custPrograssbar.prograssCreate(this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    String amount="amount";
    String detail="detail";
    public void onBindClick(View view) {
        int id = view.getId();
        if (id == R.id.img_back) {
            finish();
        } else if (id == R.id.btn_continus) {
            if (Utility.getInstance().payAmount != 0) {

                switch (paymentItemr.getTitle()) {
                    case "Razorpay":
                        int temtoal = (int) Math.round(Utility.getInstance().payAmount);
                        startActivity(new Intent(PaymentActivity.this, RazerpayActivity.class).putExtra(amount, temtoal).putExtra(detail, paymentItemr));
                        finish();
                        break;
                    case "Cash On Delivery":

                        break;
                    case "Paypal":
                        startActivity(new Intent(PaymentActivity.this, PaypalActivity.class).putExtra(amount, Utility.getInstance().payAmount).putExtra(detail, paymentItemr));
                        finish();
                        break;
                    case "Stripe":
                        startActivity(new Intent(PaymentActivity.this, StripPaymentActivity.class).putExtra(amount, Utility.getInstance().payAmount).putExtra(detail, paymentItemr));
                        finish();
                        break;
                    case "FlutterWave":
                        startActivity(new Intent(PaymentActivity.this, FlutterwaveActivity.class).putExtra(amount, Utility.getInstance().payAmount));
                        finish();
                        break;
                    case "Paytm":
                        startActivity(new Intent(PaymentActivity.this, PaytmActivity.class).putExtra(amount, Utility.getInstance().payAmount));
                        finish();
                        break;
                    case "SenangPay":
                        startActivity(new Intent(PaymentActivity.this, SenangpayActivity.class).putExtra(amount, Utility.getInstance().payAmount).putExtra(detail, paymentItemr));
                        finish();
                        break;
                    case "PayStack":
                        int temtoal1 = (int) Math.round(Utility.getInstance().payAmount);
                        startActivity(new Intent(PaymentActivity.this, PaystackActivity.class).putExtra(amount, temtoal1).putExtra(detail, paymentItemr));
                        finish();
                        break;

                    case "SSLCOMMERZ":
                        startActivity(new Intent(PaymentActivity.this, SSLCommerzActivity.class).putExtra(amount, Utility.getInstance().payAmount).putExtra(detail, paymentItemr));
                        finish();

                        break;
                    case "Bkash":
                        startActivity(new Intent(PaymentActivity.this, BkashPaymenrActivity.class).putExtra(amount, Utility.getInstance().payAmount).putExtra(detail, paymentItemr));
                        finish();

                        break;
                    default:

                        break;
                }
            } else {
                Utility.getInstance().paymentsucsses = 1;
                Utility.getInstance().paymentMID = "3";
                Utility.getInstance().tragectionID = "0";
                finish();
            }
        }
    }

    @Override
    public void onClickPaymentInfo(PaymentdataItem paymentItem, int position) {

        this.paymentItemr = paymentItem;
        Utility.getInstance().paymentMID = paymentItem.getId();


    }

    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            custPrograssbar.closePrograssBar();
            if (callNo.equalsIgnoreCase("2")) {
                Gson gson = new Gson();
                Payment payment = gson.fromJson(result.toString(), Payment.class);
                if (payment.getResult().equalsIgnoreCase("true")) {
                    paymentList = new ArrayList<>();
                    for (int i = 0; i < payment.getPaymentdata().size(); i++) {
                        if (payment.getPaymentdata().get(i).getPShow().equalsIgnoreCase("1")) {
                            paymentList.add(payment.getPaymentdata().get(i));
                        }
                    }

                    PaymentAdapter paymentAdapter = new PaymentAdapter(this, paymentList, this);
                    binding.recyclerViewPayment.setAdapter(paymentAdapter);

                }
            }
        } catch (Exception e) {
            Log.e("Error","-"+e.getMessage());

        }
    }


}