package com.cygnusa.hyperm;

import android.annotation.SuppressLint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.appcompat.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.gson.Gson;
import com.cygnusa.hyperm.adapter.RedeemPointListAdapter;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.util.TransparentProgressDialog;
import com.cygnusa.hyperm.util.UtilService;
import com.cygnusa.hyperm.wrappers.ErrorResponse;
import com.cygnusa.hyperm.wrappers.RedeemCategory;
import com.cygnusa.hyperm.wrappers.RedeemCategoryListResponse;
import com.tylersuehr.esr.EmptyStateRecyclerView;
import com.tylersuehr.esr.TextStateDisplay;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RedeemPointsCategoryActivity extends AppCompatActivity {
    private EmptyStateRecyclerView redeemList;
    RedeemPointListAdapter adapter;
    TransparentProgressDialog progressDialog;
    ApiService restService;
    RedeemCategory Categories = new RedeemCategory();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_redeem);
        restService = ((MainApplication) getApplication()).getClient();
        redeemList = findViewById(R.id.redeemList);
        redeemList.setHasFixedSize(true);
        redeemList.setItemAnimator(new SlideInLeftAnimator());
        StaggeredGridLayoutManager gridLayoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        redeemList.setLayoutManager(gridLayoutManager);
        redeemList.setStateDisplay(EmptyStateRecyclerView.STATE_EMPTY,
                new TextStateDisplay(this, "No data available", ""));
        redeemList.post(new Runnable() {
            @Override
            public void run() {
               //startActivity(new Intent(RedeemPointsCategoryActivity.this, RedeemProductListActivity.class));
                getCategoryList(true);
            }
        });


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.back);
        toolbar.setContentInsetStartWithNavigation(0);
        toolbar.setTitle("");
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        @SuppressLint("PrivateResource")
        Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
        backArrow.setColorFilter(getResources().getColor(R.color.btn_bg_color), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(backArrow);
    }
    protected void onResume() {
        super.onResume();

        if (GlobalData.canLogout) {
            finish();
        }
    }
    private void getCategoryList(boolean canLoadProgress) {
        if (new UtilService().isNetworkAvailable(RedeemPointsCategoryActivity.this)) {
            if (canLoadProgress) {
                progressDialog = new TransparentProgressDialog(RedeemPointsCategoryActivity.this);
                progressDialog.show();
               // progressBar.setVisibility(View.GONE);
            } else {
               // progressBar.setVisibility(View.VISIBLE);
            }

            restService.getRedeemCategoryList().enqueue(new Callback<RedeemCategoryListResponse>() {
                @Override
                public void onResponse(Call<RedeemCategoryListResponse> call, Response<RedeemCategoryListResponse> response) {

                    if (progressDialog != null && progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                 //   progressBar.setVisibility(View.GONE);

                    if (response.body() != null && response.body().getStatus() == 200) {

                        if (adapter == null) {
                            Categories = response.body().getData();
                            adapter = new RedeemPointListAdapter(RedeemPointsCategoryActivity.this,  Categories);
                            redeemList.setAdapter(adapter);
                        } else {
                           // Categories.addAll(response.body().getData());

                        }

//                        if (Categories.getRoot_category(). == 0) {
//                            redeemList.invokeState(EmptyStateRecyclerView.STATE_EMPTY);
//                        } else {
//                            redeemList.invokeState(EmptyStateRecyclerView.STATE_OK);
//                        }
                    } else if (response.body() != null && response.body().getStatus() == 401) {
                        GlobalData.canLogout = true;
                        new Handler(HomeActivity.callback).sendEmptyMessage(0);
                        finish();
                    } else if (response.body() != null) {
                        GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    } else if (response.errorBody() != null) {
                        Gson gson = new Gson();
                        ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
                        GlobalData.showToast(RedeemPointsCategoryActivity.this, message.getMessage(), Toast.LENGTH_SHORT).show();
                        if (message.getStatus() == 401) {
                            GlobalData.canLogout = true;
                            new Handler(HomeActivity.callback).sendEmptyMessage(0);
                            finish();
                        }
                    }


                }

                @Override
                public void onFailure(Call<RedeemCategoryListResponse> call, Throwable t) {
                    if (progressDialog != null && progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                   // progressBar.setVisibility(View.GONE);
                    GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            confirmClose();
        }
        return super.onOptionsItemSelected(item);
    }

    private void confirmClose() {
        new Handler(MyWallet.callbackOrderHistory).sendEmptyMessage(0);
        finish();
        RedeemPointsCategoryActivity.this.overridePendingTransition(R.anim.animation_f_enter,
                R.anim.animation_f_leave);
    }

}
