package com.app.rewardapp.ui.activity;

import static com.ads.androidsdk.sdk.util.Tool.isConnected;
import static com.app.rewardapp.util.Constant_Api.Pos;
import static com.app.rewardapp.util.Constant_Api.REMOVE;
import static com.app.rewardapp.util.Fun.data;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.app.rewardapp.R;
import com.app.rewardapp.Responsemodel.CallbackResp;
import com.app.rewardapp.Responsemodel.TaskResp;
import com.app.rewardapp.adapters.TaskAdapter;
import com.app.rewardapp.adsManager.RewardAds;
import com.app.rewardapp.databinding.ActivityWeburlBinding;
import com.app.rewardapp.listener.OnItemClickListener;
import com.app.rewardapp.restApi.ApiClient;
import com.app.rewardapp.restApi.ApiInterface;
import com.app.rewardapp.adsManager.AdImp;
import com.app.rewardapp.adsManager.AdManager;
import com.app.rewardapp.util.Constant_Api;
import com.app.rewardapp.util.Fun;
import com.app.rewardapp.util.Session;
import com.applovin.mediation.ads.MaxRewardedAd;

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

public class WeburlActivity extends AppCompatActivity implements AdImp, OnItemClickListener {
    ActivityWeburlBinding binding;
    Activity activity;
    AdManager adManager;
    Session session;
    TaskAdapter adapter;
    List<TaskResp> list;
    int item,pos;
    String id;
    CountDownTimer countDownTimer;
    boolean isTimerFinish,isTimerRunning;
    private AlertDialog dialog,bonus_dialog;
    RewardAds.Builder adNetwork;
    ProgressDialog pb;
    private static final int REQUEST_CODE = 100;
    public static final  String TAG="WebUrlActivity : ";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityWeburlBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        activity=WeburlActivity.this;
        session = new Session(activity);
        adManager=new AdManager(activity);
        adManager.loadBannerAd(null);
        adManager.loadInterstitalAd();

        dialog= Fun.loading(activity);
        pb = new ProgressDialog(activity);
        pb.setMessage(getString(R.string.loading));
        pb.setCancelable(false);
        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);

        binding.toolbar.setText(getString(R.string.activity_article));
        list=new ArrayList<>();
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new TaskAdapter(list,activity);
        binding.recyclerview.setAdapter(adapter);
        adapter.setClickListener(this);

        if (isConnected(this)){
            getdata();
        }else {
            Fun.Error(activity,getString(R.string.no_internet));
        }

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


    private void getdata(){
        ApiClient.getClient(activity).create(ApiInterface.class).ApiTask(Fun.data("","","","","","",7,2,session.Auth())).enqueue(new Callback<List<TaskResp>>() {
           @Override
           public void onResponse(Call<List<TaskResp>> call, Response<List<TaskResp>> response) {
               if(response.isSuccessful() && response.body().size()>0){
                   showItem(true);
                   displayData(response);
               }else {
                   showItem(false);
               }
           }

           @Override
           public void onFailure(Call<List<TaskResp>> call, Throwable t) {
              showItem(false);
           }
       });
    }

    private void showItem(boolean item) {
        if(item){
            binding.shimmerViewContainer.setVisibility(View.GONE);
            binding.recyclerview.setVisibility(View.VISIBLE);
        }else {
            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(true);
        adNetwork.setInstantReload(true);
        adNetwork.buildAd();
    }


    @Override
    protected void onResume() {
        if(REMOVE){
            removeItem(Pos);
        }
        super.onResume();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == REQUEST_CODE) {
            Log.e(TAG, "onActivityResult: " );
            if(adNetwork.isAdLoaded()){
                Log.e(TAG, "isAdLoaded: " );
                adNetwork.showReward();
            }else{
                Log.e(TAG, "Not Loaded: " );
                Toast.makeText(activity, "Not Loaded", Toast.LENGTH_SHORT).show();
            }

            if(isTimerFinish){
                isTimerFinish=false;
                if (adNetwork.isAdLoaded()) {
                    adNetwork.showReward();
                } else {
                    pb.show();
                    loadReward();
                    new Handler().postDelayed(() -> {
                        pb.dismiss();
                        if (adNetwork.isAdLoaded()) {
                            adNetwork.showReward();
                        }else{
                            Toast.makeText(activity, "Try Again No ads Available", Toast.LENGTH_SHORT).show();
                        }
                    }, 5000);
                }
            }else{
                countDownTimer.cancel();
                countDownTimer=null;
                isTimerRunning=false;
                if (adNetwork.isAdLoaded()) {
                    adNetwork.showReward();
                } else {
                    pb.show();
                    loadReward();
                    new Handler().postDelayed(() -> {
                        pb.dismiss();
                        if (adNetwork.isAdLoaded()) {
                            adNetwork.showReward();
                        }
                    }, 5000);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void displayData(Response<List<TaskResp>> response) {
        for (int i = 0; i < response.body().size(); i++) {
            list.add(response.body().get(i));
            item++;
            if (item == nativeItem) {
                item = 0;
                if (nativeType.equals(FB)) {
                    list.add(new TaskResp().setViewType(3));
                } else if (nativeType.equals(ADMOB)) {
                    list.add(new TaskResp().setViewType(4));
                } else if (nativeType.equals(STARTAPP))
                    list.add(new TaskResp().setViewType(5));
            }
        }
    }

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

    public void back(View view) {
        onBackPressed();
    }

    private void removeItem(int posi){
        list.remove(posi);
        adapter.notifyDataSetChanged();
        Pos=0;
        REMOVE=false;
        if(list.size()<5){
            list.clear();
            getdata();
        }
    }

    @Override
    public void onClick(View view, int position) {
        id=list.get(position).getId();
        this.pos=position;
        Pos=position;
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.setUrlBarHidingEnabled(true);
        builder.setToolbarColor(getResources().getColor(R.color.colorPrimaryDark));
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.intent.setData(Uri.parse(list.get(position).getUrl()));
        startActivityForResult(customTabsIntent.intent,REQUEST_CODE);
        startTime();
    }

    void startTime(){
        countDownTimer = new CountDownTimer((Integer.parseInt(list.get(pos).getTimer())* 60L)* 1000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                isTimerRunning=true;
                Log.e(TAG, "onTick: "+(millisUntilFinished/1000) );
                Toast.makeText(activity, ""+(millisUntilFinished/1000), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                credit();
                isTimerRunning=false;
                isTimerFinish=true;
            }
        }.start();
    }

    private void credit() {
        showDialog();
        ApiClient.getClient(this).create(ApiInterface.class).Api(data("","","","","","",9,Integer.parseInt(id),session.Auth())).enqueue(new Callback<CallbackResp>() {
            @Override
            public void onResponse(Call<CallbackResp> call, Response<CallbackResp> response) {
                dismissDialog();
                if(response.isSuccessful() && response.body().getCode()==201){
                    Constant_Api.REMOVE=true;
                    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) {
                dismissDialog();
            }
        });
    }

    private void showDialog() {
        dialog.show();
    }

    private void dismissDialog() {
        if(dialog.isShowing()){
            dialog.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();});
    }

}
