package com.cscodetech.movers.ui.findlorry;

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cscodetech.movers.R;
import com.cscodetech.movers.databinding.ActivityBookLorryDetailsBinding;
import com.cscodetech.movers.model.RestResponse;
import com.cscodetech.movers.model.postload.MyLoadData;
import com.cscodetech.movers.retrofit.APIClient;
import com.cscodetech.movers.retrofit.GetResult;
import com.cscodetech.movers.ui.BaseActivity;
import com.cscodetech.movers.ui.PaymentActivity;
import com.cscodetech.movers.ui.postload.BiderInfoActivity;
import com.cscodetech.movers.utils.CustPrograssbar;
import com.cscodetech.movers.utils.SessionManager;
import com.cscodetech.movers.utils.Utility;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

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

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

public class BookLorryDetailsActivity extends BaseActivity implements GetResult.MyListener {

    private ActivityBookLorryDetailsBinding binding;
    CustPrograssbar custPrograssbar;
    String loadId = "load_id";
    String status = "status";
    String applicationjson = "application/json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBookLorryDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        custPrograssbar = new CustPrograssbar();
        getBookLoadDetails();
        binding.imgBack.setOnClickListener(this::onBindClick);
        binding.txtReject.setOnClickListener(this::onBindClick);
        binding.btnRating.setOnClickListener(this::onBindClick);
        binding.txtAccept.setOnClickListener(this::onBindClick);
        binding.txtOffer.setOnClickListener(this::onBindClick);
        binding.txtPay.setOnClickListener(this::onBindClick);
        binding.imgCall.setOnClickListener(this::onBindClick);
        binding.imgs.setOnClickListener(this::onBindClick);
    }

    private void getBookLoadDetails() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", sessionmanager.getUserDetails().getId());
            jsonObject.put(loadId, getIntent().getStringExtra("lid"));
            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse(applicationjson));
            Call<JsonObject> call = APIClient.getInterface().bookDetails(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "1");
            custPrograssbar.prograssCreate(BookLorryDetailsActivity.this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getRateUpdate(String rate, String ratetext) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", sessionmanager.getUserDetails().getId());
            jsonObject.put(loadId, getIntent().getStringExtra("lid"));
            jsonObject.put("total_trate", rate);
            jsonObject.put("rate_ttext", ratetext);

            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse(applicationjson));
            Call<JsonObject> call = APIClient.getInterface().rateUpdate(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "3");
            custPrograssbar.prograssCreate(BookLorryDetailsActivity.this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    MyLoadData myLoadData;
    String tonne = "Tonne";

    @Override
    public void callback(JsonObject result, String callNo) {
        try {

            custPrograssbar.closePrograssBar();

            if (callNo.equalsIgnoreCase("1")) {
                Gson gson = new Gson();
                myLoadData = gson.fromJson(result.toString(), MyLoadData.class);
                binding.txtActionbar.setText(getResources().getString(R.string.load) + " #" + myLoadData.getLoadDetails().getId());
                Glide.with(this).load(APIClient.BASE_URL + "/" + myLoadData.getLoadDetails().getVehicleImg()).thumbnail(Glide.with(this).load(R.drawable.tprofile)).into(binding.imgt);
                binding.txtTitle.setText(myLoadData.getLoadDetails().getVehicleTitle());

                if (myLoadData.getLoadDetails().getAmtType().equalsIgnoreCase(tonne)) {
                    binding.txtPrice.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getAmount());
                    binding.txtTonneorfix.setText("/ " + myLoadData.getLoadDetails().getAmtType());
                    binding.txtTotal.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getTotalAmt());

                    if (myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("3") || myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("6")) {
                        binding.txtPricel.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getOfferPrice());
                        binding.txtTonneorfixl.setText(" /" + myLoadData.getLoadDetails().getAmtType());
                        binding.txtTotall.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getOfferTotal());

                    } else {
                        binding.txtPricel.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getAmount());
                        binding.txtTonneorfixl.setText(" /" + myLoadData.getLoadDetails().getAmtType());
                        binding.txtTotall.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getTotalAmt());
                    }
                } else {
                    binding.txtPrice.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getAmount());
                    binding.txtTonneorfix.setText("/ " + myLoadData.getLoadDetails().getAmtType());
                    binding.txtTotal.setVisibility(View.GONE);
                    if (myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("3") || myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("6")) {
                        binding.txtPricel.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getOfferTotal());
                        binding.txtTonneorfixl.setText(" /" + myLoadData.getLoadDetails().getOfferType());

                    } else {
                        binding.txtPricel.setText(sessionmanager.getStringData(SessionManager.currency) + myLoadData.getLoadDetails().getTotalAmt());
                        binding.txtTonneorfixl.setText(" /" + myLoadData.getLoadDetails().getAmtType());
                    }
                    binding.txtTotall.setVisibility(View.GONE);
                }
                binding.txtPick.setText(myLoadData.getLoadDetails().getPickupState());
                binding.txtPickaddress.setText(myLoadData.getLoadDetails().getPickupPoint());
                binding.txtDrop.setText(myLoadData.getLoadDetails().getDropState());
                binding.txtDropaddress.setText(myLoadData.getLoadDetails().getDropPoint());
                binding.txtDate.setText("" + Utility.getInstance().parseDateToddMMyy(myLoadData.getLoadDetails().getPostDate()));
                binding.txtTonne.setText("" + myLoadData.getLoadDetails().getWeight());
                binding.txtMaterial.setText("" + myLoadData.getLoadDetails().getMaterialName());

                Glide.with(this).load(APIClient.BASE_URL + "/" + myLoadData.getLoadDetails().getBidderImg()).thumbnail(Glide.with(this).load(R.drawable.tprofile)).into(binding.imgs);
                binding.txtName.setText(myLoadData.getLoadDetails().getBidderName());


                binding.txtRates.setText(myLoadData.getLoadDetails().getRate());
                binding.txtOrderStast.setText(myLoadData.getLoadDetails().getMessage());


                switch (myLoadData.getLoadDetails().getFlowId()) {
                    case "0":
                        binding.lvlPaymentinfo.setVisibility(View.GONE);
                        binding.txtStatus.setVisibility(View.VISIBLE);
                        binding.txtStatus.setText("Waiting for response");
                        break;
                    case "1":

                        binding.lvlPaymentinfo.setVisibility(View.GONE);
                        binding.txtStatus.setVisibility(View.GONE);
                        binding.txtPay.setVisibility(View.VISIBLE);
                        binding.imgCall.setVisibility(View.VISIBLE);

                        break;
                    case "2":
                    case "5":
                        binding.lvlPaymentinfo.setVisibility(View.GONE);
                        binding.txtStatus.setVisibility(View.VISIBLE);
                        binding.txtStatus.setText("" + myLoadData.getLoadDetails().getCommentReject());
                        binding.txtPay.setVisibility(View.GONE);
                        break;
                    case "3":
                        binding.lvlPaymentinfo.setVisibility(View.GONE);
                        binding.lvlLorryownerresponse.setVisibility(View.VISIBLE);
                        binding.txtStatus.setVisibility(View.GONE);

                        binding.txtPay.setVisibility(View.GONE);
                        break;
                    case "4":
                    case "7":
                        binding.imgCall.setVisibility(View.VISIBLE);
                        binding.lvlPaymentinfo.setVisibility(View.VISIBLE);
                        binding.txtStatus.setVisibility(View.GONE);
                        binding.txtPay.setVisibility(View.GONE);
                        binding.txtPmethod.setText("" + myLoadData.getLoadDetails().getpMethodName());
                        binding.txtTrasaction.setText("" + myLoadData.getLoadDetails().getOrderTransactionId());
                        binding.txtSubtotal.setText(sessionmanager.getStringData(currency) + myLoadData.getLoadDetails().getTotalAmt());
                        binding.txtWallet.setText(sessionmanager.getStringData(currency) + myLoadData.getLoadDetails().getWalAmt());
                        binding.txtTpayment.setText(sessionmanager.getStringData(currency) + myLoadData.getLoadDetails().getPayAmt());
                        break;
                    case "6":
                        binding.lvlPaymentinfo.setVisibility(View.GONE);
                        binding.txtStatus.setVisibility(View.VISIBLE);
                        binding.txtStatus.setText("Waiting for Offer Response");

                        break;
                    case "8":

                        binding.lvlPaymentinfo.setVisibility(View.VISIBLE);
                        binding.txtStatus.setVisibility(View.GONE);
                        binding.txtPay.setVisibility(View.GONE);
                        binding.txtPmethod.setText("" + myLoadData.getLoadDetails().getpMethodName());
                        binding.txtTrasaction.setText("" + myLoadData.getLoadDetails().getOrderTransactionId());
                        binding.txtSubtotal.setText(sessionmanager.getStringData(currency) + myLoadData.getLoadDetails().getTotalAmt());
                        binding.txtWallet.setText(sessionmanager.getStringData(currency) + myLoadData.getLoadDetails().getWalAmt());
                        binding.txtTpayment.setText(sessionmanager.getStringData(currency) + myLoadData.getLoadDetails().getPayAmt());
                        if (myLoadData.getLoadDetails().getIsRate().equalsIgnoreCase("0")) {
                            binding.btnRating.setVisibility(View.VISIBLE);
                            binding.btnRating.setText(getResources().getString(R.string.rate_to) + " " + myLoadData.getLoadDetails().getBidderName());
                        }
                        break;
                    default:

                        break;

                }
            } else if (callNo.equalsIgnoreCase("2")) {
                Gson gson = new Gson();
                RestResponse response = gson.fromJson(result.toString(), RestResponse.class);
                if (response.getResult().equals("true")) {

                    sessionmanager.setFloatData(wallet, Float.parseFloat(response.getWallet()));
                    finish();
                }
            } else if (callNo.equalsIgnoreCase("3")) {
                Gson gson = new Gson();
                RestResponse response = gson.fromJson(result.toString(), RestResponse.class);
                if (response.getResult().equals("true")) {

                    finish();
                }
            }

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

        }
    }

    private void getDecision(JSONObject jsonObject) {

        RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse(applicationjson));
        Call<JsonObject> call = APIClient.getInterface().offerDecision(bodyRequest);
        GetResult getResult = new GetResult(this);
        getResult.setMyListener(this);
        getResult.callForLogin(call, "2");
        custPrograssbar.prograssCreate(BookLorryDetailsActivity.this);

    }


    public void bidderOffers(Context context) {
        Activity activity = (Activity) context;
        BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(activity);
        View rootView = activity.getLayoutInflater().inflate(R.layout.lorry_book_offer_layout, null);
        mBottomSheetDialog.setContentView(rootView);
        TextView txtClick = rootView.findViewById(R.id.btn_sendoffer);
        EditText edAmount = rootView.findViewById(R.id.ed_amount);
        EditText edDescription = rootView.findViewById(R.id.ed_description);
        Switch swichtonne = rootView.findViewById(R.id.swichtonne);
        final String[] atype = {""};
        final double[] tamout = new double[1];
        swichtonne.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!TextUtils.isEmpty(edAmount.getText().toString())) {
                if (b) {
                    swichtonne.setText("Per Tonnes");
                    atype[0] = tonne;
                    tamout[0] = Double.parseDouble(edAmount.getText().toString()) * Double.parseDouble(myLoadData.getLoadDetails().getWeight());

                } else {
                    swichtonne.setText("Fix");
                    atype[0] = "Fixed";


                }
            }

        });

        txtClick.setOnClickListener(v -> {
            if (!swichtonne.isChecked()) {
                atype[0] = "Fixed";
                tamout[0] = Double.parseDouble(edAmount.getText().toString());
            }
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("uid", sessionmanager.getUserDetails().getId());
                jsonObject.put(loadId, getIntent().getStringExtra("lid"));
                jsonObject.put(status, "3");
                jsonObject.put("offer_description", edDescription.getText().toString());
                jsonObject.put("offer_price", edAmount.getText().toString());
                jsonObject.put("offer_type", atype[0]);
                jsonObject.put("offer_total", tamout[0]);

                getDecision(jsonObject);

                mBottomSheetDialog.cancel();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        });
        mBottomSheetDialog.show();
    }

    String clickOff = "";


    public void onBindClick(View view) {
        int id = view.getId();
        if (id == R.id.img_back) {
            finish();


        } else if (id == R.id.btn_rating) {
            sendReview(this);
        } else if (id == R.id.img_call) {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + myLoadData.getLoadDetails().getBidderMobile()));
            startActivity(intent);
        } else if (id == R.id.txt_reject) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("uid", sessionmanager.getUserDetails().getId());
                jsonObject.put(loadId, getIntent().getStringExtra("lid"));
                jsonObject.put(status, "3");
                jsonObject.put("comment_reject", "");

                getDecision(jsonObject);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (id == R.id.txt_accept) {
            clickOff = "accept";
            startActivity(new Intent(this, PaymentActivity.class).putExtra("tAmount", myLoadData.getLoadDetails().getOfferTotal()));
        } else if (id == R.id.txt_offer) {
            clickOff = "Offer";
            bidderOffers(this);
        } else if (id == R.id.txt_pay) {
            clickOff = "aceptandpay";
            startActivity(new Intent(this, PaymentActivity.class).putExtra("tAmount", myLoadData.getLoadDetails().getTotalAmt()));
        } else if (id == R.id.imgs) {

            startActivity(new Intent(this, BiderInfoActivity.class)
                    .putExtra("oid", myLoadData.getLoadDetails().getBidderId())
                    .putExtra("lid", myLoadData.getLoadDetails().getLorryIid()));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();


        if (Utility.getInstance().paymentsucsses == 1) {
            Utility.getInstance().paymentsucsses = 0;
            if ("accept".equals(clickOff)) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("uid", sessionmanager.getUserDetails().getId());
                    jsonObject.put(loadId, getIntent().getStringExtra("lid"));
                    jsonObject.put(status, "1");
                    jsonObject.put("comment_reject", "");
                    jsonObject.put("amount", myLoadData.getLoadDetails().getAmount());
                    jsonObject.put("amt_type", myLoadData.getLoadDetails().getAmtType());
                    jsonObject.put("total_amt", myLoadData.getLoadDetails().getTotalAmt());
                    jsonObject.put("wal_amt", Utility.getInstance().tWallet);
                    jsonObject.put("p_method_id", Utility.getInstance().paymentMID);
                    jsonObject.put("trans_id", Utility.getInstance().tragectionID);
                    jsonObject.put("description", "");

                    getDecision(jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else if ("aceptandpay".equals(clickOff)) {
                JSONObject jsonObject1 = new JSONObject();
                try {
                    jsonObject1.put("uid", sessionmanager.getUserDetails().getId());
                    jsonObject1.put(loadId, getIntent().getStringExtra("lid"));
                    jsonObject1.put(status, "4");
                    jsonObject1.put("wal_amt", Utility.getInstance().tWallet);
                    jsonObject1.put("p_method_id", Utility.getInstance().paymentMID);
                    jsonObject1.put("trans_id", Utility.getInstance().tragectionID);
                    getDecision(jsonObject1);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public void sendReview(Context context) {
        Activity activity = (Activity) context;
        BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(activity);
        View rootView = activity.getLayoutInflater().inflate(R.layout.sendrview_layout, null);
        mBottomSheetDialog.setContentView(rootView);
        TextView txtClick = rootView.findViewById(R.id.btn_load);
        ImageView img_p = rootView.findViewById(R.id.img_p);
        TextView txt_name = rootView.findViewById(R.id.txt_name);
        TextView txt_lorry = rootView.findViewById(R.id.txt_lorry);
        EditText edRevire = rootView.findViewById(R.id.ed_revire);
        RatingBar rating = rootView.findViewById(R.id.rating);
        txtClick.setText(getResources().getString(R.string.rate_to) + " " + myLoadData.getLoadDetails().getBidderName());

        Glide.with(this).load(APIClient.BASE_URL + "/" + myLoadData.getLoadDetails().getBidderImg()).thumbnail(Glide.with(this).load(R.drawable.tprofile)).into(img_p);
        txt_name.setText("" + myLoadData.getLoadDetails().getBidderName());
        txt_lorry.setText("" + myLoadData.getLoadDetails().getLorryNumber());
        txtClick.setOnClickListener(v -> {
            mBottomSheetDialog.cancel();
            String ratin = String.valueOf(rating.getRating());
            getRateUpdate(ratin, edRevire.getText().toString());


        });
        mBottomSheetDialog.setCancelable(true);
        mBottomSheetDialog.show();
    }

}