package com.cscodetech.moverslorry.ui;

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

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

import com.bumptech.glide.Glide;
import com.cscodetech.moverslorry.R;
import com.cscodetech.moverslorry.adepter.ReviewAdapter;
import com.cscodetech.moverslorry.databinding.ActivityReviewInfoBinding;
import com.cscodetech.moverslorry.model.BidInfo;
import com.cscodetech.moverslorry.model.UserLogin;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.retrofit.GetResult;
import com.cscodetech.moverslorry.utils.CustPrograssbar;
import com.cscodetech.moverslorry.utils.Utility;
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 ReviewActivity extends BaseActivity implements GetResult.MyListener {

    private ActivityReviewInfoBinding binding;
    CustPrograssbar custPrograssbar;
    UserLogin userLogin;

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

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

        getBidInfo();

        // Wire clicks via ViewBinding
        binding.imgBack.setOnClickListener(this::onBindClick);
    }

    private void getBidInfo() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("owner_id", userLogin.getId());
            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(),MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().transProfile(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "1");
            custPrograssbar.prograssCreate(this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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


    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            custPrograssbar.closePrograssBar();
            if (callNo.equalsIgnoreCase("1")) {
                Gson gson = new Gson();
                BidInfo bidInfo = gson.fromJson(result.toString(), BidInfo.class);
                if (bidInfo.getResult().equalsIgnoreCase("true")) {
                    Glide.with(this).load(APIClient.BASE_URL + "/" + bidInfo.getLorrizprofile().getProPic()).thumbnail(Glide.with(this).load(R.drawable.tprofile)).centerCrop().into(binding.imgs);

                    binding.txtName.setText("" + bidInfo.getLorrizprofile().getName());
                    binding.txtRates.setText("" + bidInfo.getLorrizprofile().getReview());
                    binding.txtDate.setText("" + Utility.getInstance().parseDateToddMMyy(bidInfo.getLorrizprofile().getRdate()));
                    binding.txtLorry.setText("" + bidInfo.getLorrizprofile().getTotalLorry());
                    binding.txtReview.setText("" + bidInfo.getLorrizprofile().getTotalReview());
                    binding.txtTotalrview.setText(bidInfo.getLorrizprofile().getReview() + " â— " + bidInfo.getLorrizprofile().getTotalReview() + " " + getResources().getString(R.string.review));


                    ReviewAdapter adapter1 = new ReviewAdapter(this, bidInfo.getLorrizprofile().getTotalReviewUserWise());
                    binding.recyclerViewReview.setAdapter(adapter1);

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