package com.app.rewardapp.ui.activity;

import static com.ads.androidsdk.sdk.util.Tool.isConnected;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.app.rewardapp.R;
import com.app.rewardapp.Responsemodel.CallbackResp;
import com.app.rewardapp.Responsemodel.RedeemResponse;
import com.app.rewardapp.adapters.RedeemAdapter;
import com.app.rewardapp.adsManager.RewardAds;
import com.app.rewardapp.databinding.ActivityWithdrawBinding;
import com.app.rewardapp.databinding.RedeemdialogBinding;
import com.app.rewardapp.listener.OnItemClickListener;
import com.app.rewardapp.restApi.ApiClient;
import com.app.rewardapp.restApi.ApiInterface;
import com.app.rewardapp.restApi.WebApi;
import com.app.rewardapp.adsManager.AdImp;
import com.app.rewardapp.adsManager.AdManager;
import com.app.rewardapp.util.Fun;
import com.app.rewardapp.util.Session;
import com.bumptech.glide.Glide;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WithdrawActivity extends AppCompatActivity implements OnItemClickListener, AdImp {
    ActivityWithdrawBinding binding;
    Activity activity;
    List<RedeemResponse.DataItem> list;
    Session session;
    private AlertDialog bonus_dialog,alertDialog;
    RedeemAdapter adapter;
    AdManager adManager;
    RewardAds.Builder adNetwork;
    BottomSheetDialog bottomSheetDialog;
    RedeemdialogBinding redeemBind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityWithdrawBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        activity=WithdrawActivity.this;
        alertDialog= Fun.Alert(activity);
        adManager=new AdManager(activity);
        adManager.loadBannerAd(null);
        loadReward();

        bonus_dialog = new AlertDialog.Builder(activity).setView(LayoutInflater.from(activity).inflate(R.layout.layout_collect_bonus, null)).create();
        bonus_dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        bonus_dialog.getWindow().setWindowAnimations(R.style.Dialoganimation);
        bonus_dialog.setCanceledOnTouchOutside(false);

        session=new Session(this);
        binding.toolbar.setText(getString(R.string.activity_reward));

        list= new ArrayList();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RedeemAdapter(WithdrawActivity.this,list);
        adapter.setClickListener(this);
        binding.recyclerView.setAdapter(adapter);

        if (isConnected(this)){
            getdata();
        }else {
            showAlert(getString(R.string.no_internet));
        }

        binding.back.setOnClickListener(v->{
            onBackPressed();
        });

    }

    void showAlert(String msg){
        alertDialog.show();
        TextView tv=alertDialog.findViewById(R.id.txt);
        tv.setText(msg);
        alertDialog.findViewById(R.id.close).setOnClickListener(v -> {
            alertDialog.dismiss();
        });
    }

    void showbonus(String msg, String type){
        bonus_dialog.show();
        TextView tv,congrats;
        tv=bonus_dialog.findViewById(R.id.txt);
        congrats=bonus_dialog.findViewById(R.id.congrts);
        tv.setText(msg);
        if(type.equals("error")){
            congrats.setText(getString(R.string.oops));
            congrats.setTextColor(getResources().getColor(R.color.red));
        }
        bonus_dialog.findViewById(R.id.closebtn).setOnClickListener(view -> {bonus_dialog.dismiss();});
    }

    private void getdata(){
        Call<RedeemResponse> call = ApiClient.getClient(this).create(ApiInterface.class).getRedeem();
        call.enqueue(new Callback<RedeemResponse>() {
            @Override
            public void onResponse(Call<RedeemResponse> call, Response<RedeemResponse> response) {
                if(response.isSuccessful() && response.body().getData()!=null){
                    list.addAll(response.body().getData());
                    adapter.notifyDataSetChanged();
                    binding.shimmerViewContainer.setVisibility(View.GONE);
                    binding.recyclerView.setVisibility(View.VISIBLE);
                }else {
                    binding.shimmerViewContainer.setVisibility(View.GONE);
                    binding.noResult.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<RedeemResponse> call, Throwable t) {
                binding.shimmerViewContainer.setVisibility(View.GONE);
                binding.noResult.setVisibility(View.VISIBLE);
            }
        });
    }

    private void loadReward() {
        adNetwork= new RewardAds.Builder(activity);
        adNetwork.setAdcolony_Appid(adcolonyAppID);
        adNetwork.setAdcolonyZone(adcolonyZoneID);
        adNetwork.setAdmob_au(admobRewardID);
        adNetwork.setMax_au(applovinRewardID);
        adNetwork.setUnity_au(unityRewardID);
        adNetwork.setAdmob_reward(ADMOB_REWARD);
        adNetwork.setAdcolony_reward(ADCOLONY_REWARD);
        adNetwork.setApplovin_reward(APPLOVIN_REWARD);
        adNetwork.setStatartapp_reward(STARTAPP_REWARD);
        adNetwork.setUnity_reward(UNITY_REWARD);
        adNetwork.setRewardMode(false);
        adNetwork.buildAd();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onClick(View view, int position) {
        prepareReward(position);
    }

    private void prepareReward(int pos) {
        bottomSheetDialog = new BottomSheetDialog(this);
        redeemBind= RedeemdialogBinding.inflate(getLayoutInflater());
        bottomSheetDialog.setContentView(redeemBind.getRoot());
        bottomSheetDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        bottomSheetDialog.show();

        Glide.with(this).load(WebApi.Api.IMAGES  + list.get(pos).getImage()).placeholder(R.drawable.placeholder).into(redeemBind.imageView);

        redeemBind.name.setText(getString(R.string.fill)+" "+list.get(pos).getTitle()+" "+getString(R.string.detail));
        redeemBind.email.setHint(getString(R.string.fill)+list.get(pos).getTitle()+getString(R.string.detail));

        redeemBind.send.setOnClickListener(v->{
            if(redeemBind.email.getText().toString().isEmpty()){
                showAlert(getString(R.string.fill_required_detail));
            }else{
                ApiClient.getClient(WithdrawActivity.this).create(ApiInterface.class).Api(Fun.data(
                        list.get(pos).getTitle(),
                        list.get(pos).getPoints(),
                        list.get(pos).getPointvalue(),
                        list.get(pos).getDescription(),
                        redeemBind.email.getText().toString().trim(),"",2,2,session.Auth())).enqueue(new Callback<CallbackResp>() {
                    @Override
                    public void onResponse(Call<CallbackResp> call, Response<CallbackResp> response) {
                        bottomSheetDialog.dismiss();
                        adNetwork.showReward();
                        if(response.isSuccessful() && response.body().getCode()==201){
                            session.setIntData(session.WALLET,response.body().getBalance());
                            showbonus(response.body().getMsg(),"");
                        }else {
                            showbonus(response.body().getMsg(),"error");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallbackResp> call, Throwable t) {
                        bottomSheetDialog.dismiss();
                        Toast.makeText(activity, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
