package com.cscodetech.movers.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cscodetech.movers.R;
import com.cscodetech.movers.databinding.ActivityNotificationBinding;
import com.cscodetech.movers.model.Noti;
import com.cscodetech.movers.model.NotificationDatum;
import com.cscodetech.movers.model.UserLogin;
import com.cscodetech.movers.retrofit.GetResult;
import com.cscodetech.movers.utils.CustPrograssbar;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.List;

public class NotificationActivity extends BaseActivity implements GetResult.MyListener {

    private ActivityNotificationBinding binding;
    UserLogin user;
    CustPrograssbar custPrograssbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNotificationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        user = sessionmanager.getUserDetails();
        custPrograssbar = new CustPrograssbar();
        getNotification();
        binding.imgBack.setOnClickListener(v -> finish());
    }

    @androidx.annotation.RequiresApi(api = android.os.Build.VERSION_CODES.N)
    @android.annotation.SuppressLint("SetTextI18n")
    private void setNotiList(LinearLayout lnrView, List<NotificationDatum> list) {
        lnrView.removeAllViews();


        for (int i = 0; i < list.size(); i++) {
            android.view.LayoutInflater inflater = android.view.LayoutInflater.from(NotificationActivity.this);

            View view = inflater.inflate(R.layout.custome_noti, null);

            TextView txtTitel = view.findViewById(R.id.txt_titel);
            TextView txtDatetime = view.findViewById(R.id.txt_datetime);
            TextView txtDesc = view.findViewById(R.id.txt_desc);

            String date = com.cscodetech.movers.utils.Utility.getInstance().parseDateToddMMyy(list.get(i).getDate());

            txtTitel.setText("" + list.get(i).getMsg() + "  ");
            txtDesc.setText("" + list.get(i).getDescription());
            txtDatetime.setText("" + date);

            lnrView.addView(view);

        }

    }



    private void getNotification() {
        custPrograssbar.prograssCreate(NotificationActivity.this);

        org.json.JSONObject jsonObject = new org.json.JSONObject();
        try {
            jsonObject.put("uid", user.getId());
            okhttp3.RequestBody bodyRequest = okhttp3.RequestBody.create(jsonObject.toString(),okhttp3.MediaType.parse("application/json"));
            retrofit2.Call<com.google.gson.JsonObject> call = com.cscodetech.movers.retrofit.APIClient.getInterface().getNote(bodyRequest);
            com.cscodetech.movers.retrofit.GetResult getResult = new com.cscodetech.movers.retrofit.GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "1");
        } catch (org.json.JSONException e) {
            e.printStackTrace();
        }
    }

    @androidx.annotation.RequiresApi(api = android.os.Build.VERSION_CODES.N)
    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            custPrograssbar.closePrograssBar();
            if (callNo.equalsIgnoreCase("1")) {
                Gson gson = new Gson();
                Noti notification = gson.fromJson(result.toString(), Noti.class);
                if (notification.getNotificationData().size()!=0) {

                    setNotiList(binding.lvlMyorder, notification.getNotificationData());
                } else {
                    binding.lvlMyorder.setVisibility(View.GONE);
                    binding.lvlNotfound.setVisibility(View.VISIBLE);

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