package com.cscodetech.movers.ui.postload;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

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

import com.bumptech.glide.Glide;
import com.cscodetech.movers.R;
import com.cscodetech.movers.adepter.BidsResponseAdapter;
import com.cscodetech.movers.databinding.ActivityPostLoadDetailsBinding;
import com.cscodetech.movers.model.RestResponse;
import com.cscodetech.movers.model.postload.BidStatus;
import com.cscodetech.movers.model.postload.LoadDetails;
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.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 java.text.DecimalFormat;
import java.text.NumberFormat;

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


public class PostLoadDetailsActivity extends BaseActivity implements BidsResponseAdapter.RecyclerTouchListener, GetResult.MyListener {

    private ActivityPostLoadDetailsBinding binding;
    SessionManager sessionManager;
    CustPrograssbar custPrograssbar;
    LoadDetails loadDetails;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPostLoadDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        sessionManager = new SessionManager(this);
        custPrograssbar = new CustPrograssbar();
        binding.recyclerViewBidrs.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.recyclerViewBidrs.setItemAnimator(new DefaultItemAnimator());
        getLoadDetails();
        binding.imgBack.setOnClickListener(this::onBindClick);
        binding.imgMenu.setOnClickListener(this::onBindClick);
        binding.lvlBidderinfo.setOnClickListener(this::onBindClick);
        binding.btnRating.setOnClickListener(this::onBindClick);
        binding.imgs.setOnClickListener(this::onBindClick);

    }


    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_menu) {
            PopupMenu popupMenu = new PopupMenu(this, binding.imgMenu);
            popupMenu.inflate(R.menu.postload_menu);

            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int itemId = item.getItemId();
                    if (itemId == R.id.navigation_edit) {
                        startActivity(new Intent(PostLoadDetailsActivity.this, PostLoadActivity.class).putExtra("edit", loadDetails));
                    } else if (itemId == R.id.navigation_delete) {
                        deletePostLoad(PostLoadDetailsActivity.this);

                        //do something
                    }
                    return false;
                }
            });
            popupMenu.show();
        } else if (id == R.id.lvl_bidderinfo) {
            startActivity(new Intent(this, BiderInfoActivity.class));
        } else if (id == R.id.imgs) {
            startActivity(new Intent(this, BiderInfoActivity.class)
                    .putExtra("oid", loadDetails.getBidderId())
                    .putExtra("lid", loadDetails.getLorryIid()));
        }

    }

    BidStatus bidStatus;

    @Override
    public void onClickBidsInfo(BidStatus item, int position) {
        bidStatus = item;
        bidderDetails(this, item);
    }

    @Override
    public void onClickBidsInfoReject(BidStatus item, int position) {
        bidStatus = item;
        AlertDialog.Builder builder = new AlertDialog.Builder(PostLoadDetailsActivity.this);
        builder.setMessage(getResources().getString(R.string.doyoureject));
        builder.setTitle(getResources().getString(R.string.reject));
        builder.setCancelable(false);
        builder.setPositiveButton(getResources().getString(R.string.continues), (DialogInterface.OnClickListener) (dialog, which) -> {
            finish();
            makeDecision("0");
        });
        builder.setNegativeButton(getResources().getString(R.string.no), (DialogInterface.OnClickListener) (dialog, which) -> {
            dialog.cancel();
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            custPrograssbar.closePrograssBar();
            if (callNo.equalsIgnoreCase("2")) {
                Gson gson = new Gson();
                MyLoadData myLoadData = gson.fromJson(result.toString(), MyLoadData.class);
                loadDetails = myLoadData.getLoadDetails();

                binding.txtTitle.setText("" + loadDetails.getVehicleTitle());
                binding.txtPrice.setText(sessionManager.getStringData(SessionManager.currency) + loadDetails.getAmount());
                binding.txtTonneorfix.setText(" /" + loadDetails.getAmtType());
                binding.txtPick.setText("" + loadDetails.getPickupState());
                binding.txtPickaddress.setText("" + loadDetails.getPickupPoint());
                binding.txtDrop.setText("" + loadDetails.getDropState());
                binding.txtDropaddress.setText("" + loadDetails.getDropPoint());
                binding.txtDate.setText("" + Utility.getInstance().parseDateToddMMyy(loadDetails.getPostDate()));
                binding.txtTonne.setText("" + loadDetails.getWeight());
                binding.txtMaterial.setText("" + loadDetails.getMaterialName());
                binding.txtLoadid.setText(getResources().getString(R.string.load) + " #" + loadDetails.getId());
                Glide.with(this).load(APIClient.BASE_URL + "/" + loadDetails.getVehicleImg()).thumbnail(Glide.with(this).load(R.drawable.tprofile)).into(binding.imgt);


                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());
                } else {
                    binding.txtTotal.setVisibility(View.GONE);
                }


                if (loadDetails.getHoursType().equalsIgnoreCase("infinite")) {
                    binding.lvlTimers.setVisibility(View.GONE);
                } else {

                    long milliseconds = loadDetails.getSvisibleHours() * 60 * 60 * 1000;
                    new CountDownTimer(milliseconds, 1000) {
                        public void onTick(long millisUntilFinished) {
                            // Used for formatting digit to be in 2 digits only
                            NumberFormat f = new DecimalFormat("00");
                            long hour = (millisUntilFinished / 3600000) % 24;
                            long min = (millisUntilFinished / 60000) % 60;
                            long sec = (millisUntilFinished / 1000) % 60;
                            binding.txtHours.setText(f.format(hour));
                            binding.txtMits.setText(f.format(min));
                            binding.txtSecound.setText(f.format(sec));
                        }

                        // When the task is over it will print 00:00:00 there
                        public void onFinish() {
                            binding.txtHours.setText("00:00:00");
                        }
                    }.start();


                }

                if (myLoadData.getLoadDetails().getBidStatus().size() == 0 && myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("0")) {
                    loadDetails = myLoadData.getLoadDetails();
                    binding.lvlBidResponse.setVisibility(View.GONE);
                    binding.lvlBidderinfo.setVisibility(View.GONE);
                    binding.lvlPaymentinfo.setVisibility(View.GONE);
                    binding.imgMenu.setVisibility(View.VISIBLE);
                } else if (myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("0")) {
                    binding.lvlBidResponse.setVisibility(View.VISIBLE);
                    binding.lvlNobids.setVisibility(View.GONE);
                    binding.lvlBidderinfo.setVisibility(View.GONE);
                    binding.lvlPaymentinfo.setVisibility(View.GONE);

                    BidsResponseAdapter bidsResponseAdapter = new BidsResponseAdapter(this, myLoadData.getLoadDetails().getBidStatus(), this);
                    binding.recyclerViewBidrs.setAdapter(bidsResponseAdapter);
                } else {
                    binding.lvlBidResponse.setVisibility(View.GONE);
                    if (myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("3")) {
                        binding.lvlNobids.setVisibility(View.GONE);
                    } else {
                        binding.lvlNobids.setVisibility(View.VISIBLE);
                        binding.txtOrderStast.setText(myLoadData.getLoadDetails().getMessage());
                    }

                    binding.lvlBidderinfo.setVisibility(View.VISIBLE);
                    binding.lvlPaymentinfo.setVisibility(View.VISIBLE);

                    Glide.with(this).load(APIClient.BASE_URL + "/" + loadDetails.getBidderImg()).thumbnail(Glide.with(this).load(R.drawable.tprofile)).into(binding.imgs);

                    binding.txtName.setText("" + loadDetails.getBidderName());
                    binding.txtPricel.setText(sessionManager.getStringData(SessionManager.currency) + loadDetails.getAmount());
                    binding.txtRates.setText(loadDetails.getRate());
                    binding.txtTonneorfixl.setText(" /" + loadDetails.getAmtType());
                    binding.txtOfferprice.setText(sessionManager.getStringData(SessionManager.currency) + loadDetails.getTotalAmt());


                    binding.txtPmethod.setText("" + loadDetails.getpMethodName());
                    binding.txtTrasaction.setText("" + loadDetails.getOrderTransactionId());
                    binding.txtSubtotal.setText(sessionManager.getStringData(SessionManager.currency) + loadDetails.getAmount());
                    binding.txtTpayment.setText(sessionManager.getStringData(SessionManager.currency) + loadDetails.getTotalAmt());

                    if (loadDetails.getWalAmt().equalsIgnoreCase("0")) {
                        binding.lvlWalletRow.setVisibility(View.GONE);
                    } else {
                        binding.lvlWalletRow.setVisibility(View.VISIBLE);
                        binding.txtWallet.setText(getResources().getString(R.string.load) + " #" + loadDetails.getWalAmt());
                    }
                    if (myLoadData.getLoadDetails().getFlowId().equalsIgnoreCase("3") && myLoadData.getLoadDetails().getIsRate().equalsIgnoreCase("0")) {
                        binding.btnRating.setVisibility(View.VISIBLE);
                        binding.btnRating.setText(getResources().getString(R.string.rate_to) + " " + loadDetails.getBidderName());

                    }

                }
            } else if (callNo.equalsIgnoreCase("3")) {
                Gson gson = new Gson();
                RestResponse response = gson.fromJson(result.toString(), RestResponse.class);
                if (response.getResult().equals("true")) {
                    Utility.getInstance().removepost = true;
                    finish();
                }
            } else if (callNo.equalsIgnoreCase("4")) {
                Gson gson = new Gson();
                RestResponse response = gson.fromJson(result.toString(), RestResponse.class);
                Toast.makeText(this,response.getResponseMsg(),Toast.LENGTH_SHORT).show();
                if (response.getResult().equals("true")) {

                    finish();
                }
            }
        } catch (Exception e) {
            Log.e("Errror--", "-" + e.toString());
        }

    }


    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) + " " + loadDetails.getBidderName());

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


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


    private void getRateUpdate(String rate, String ratetext) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", sessionManager.getUserDetails().getId());
            jsonObject.put("load_id", getIntent().getStringExtra("lid"));
            jsonObject.put("total_trate", rate);
            jsonObject.put("rate_ttext", ratetext);
            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(),MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().rateUpdate(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "4");
            custPrograssbar.prograssCreate(PostLoadDetailsActivity.this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void getLoadDetails() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", sessionManager.getUserDetails().getId());
            jsonObject.put("load_id", getIntent().getStringExtra("lid"));

            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(),MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().loadDetails(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "2");

            custPrograssbar.prograssCreate(this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void deletePostLoad(Context context) {
        Activity activity = (Activity) context;
        BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(activity);
        View rootView = activity.getLayoutInflater().inflate(R.layout.delete_loadpos_layout, null);
        mBottomSheetDialog.setContentView(rootView);
        TextView txtClick = rootView.findViewById(R.id.txt_reject);
        TextView btnYes = rootView.findViewById(R.id.btn_yes);
        txtClick.setOnClickListener(v -> {
            mBottomSheetDialog.cancel();


        });

        btnYes.setOnClickListener(v -> {
            mBottomSheetDialog.cancel();
            deleteLoadDetails();

        });
        mBottomSheetDialog.show();
    }


    private void deleteLoadDetails() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", sessionManager.getUserDetails().getId());
            jsonObject.put("record_id", getIntent().getStringExtra("lid"));

            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().deleteLoad(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "3");

            custPrograssbar.prograssCreate(this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }




    private void makeDecision(String status) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", sessionManager.getUserDetails().getId());
            jsonObject.put("status", status);
            jsonObject.put("load_id", loadDetails.getId());
            jsonObject.put("owner_id", bidStatus.getBidderId());
            jsonObject.put("lorry_id", bidStatus.getLorryid());
            jsonObject.put("amount", bidStatus.getAmount());
            jsonObject.put("amt_type", bidStatus.getAmtType());
            jsonObject.put("total_amt", bidStatus.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", bidStatus.getDescription());

            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(),MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().makeDecision(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "4");

            custPrograssbar.prograssCreate(this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void bidderDetails(Context context, BidStatus item) {
        Activity activity = (Activity) context;
        BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(activity);
        View rootView = activity.getLayoutInflater().inflate(R.layout.biddetails_layout, null);
        mBottomSheetDialog.setContentView(rootView);
        TextView txt_name = rootView.findViewById(R.id.txt_name);
        TextView txt_price = rootView.findViewById(R.id.txt_price);
        TextView txt_rates = rootView.findViewById(R.id.txt_rates);
        TextView txt_nooflorry = rootView.findViewById(R.id.txt_nooflorry);
        TextView txt_since = rootView.findViewById(R.id.txt_since);
        TextView txt_desc = rootView.findViewById(R.id.txt_desc);
        TextView txtClick = rootView.findViewById(R.id.btn_load);
        ImageView imgs = rootView.findViewById(R.id.imgs);

        txt_name.setText("" + item.getBidderName());
        txt_price.setText(sessionManager.getStringData(SessionManager.currency) + item.getTotalAmt());
        txt_rates.setText("" + item.getRate());
        txt_nooflorry.setText("" + item.getTotalLorry() + " " + getResources().getString(R.string.lorry));
        txt_since.setText(getResources().getString(R.string.since) + " " + item.getJoinDate());
        txt_desc.setText("" + item.getDescription());
        Log.e("imageuri","-->"+item.getBidderImg());
        Log.e("item.getTotalAmt()","-->"+item.getTotalAmt());
        Glide.with(this).load(APIClient.BASE_URL + "/" + item.getBidderImg()).thumbnail(Glide.with(this).load(R.drawable.tprofile)).into(imgs);


        txtClick.setOnClickListener(v -> {
            mBottomSheetDialog.cancel();

            startActivity(new Intent(this, PaymentActivity.class).putExtra("tAmount", item.getTotalAmt()));

        });
        mBottomSheetDialog.show();
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (Utility.getInstance().paymentsucsses == 1) {
            Utility.getInstance().paymentsucsses = 0;
            makeDecision("1");
        }
    }
}