package com.app.rewardapp.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import com.app.rewardapp.R;
import com.app.rewardapp.Responsemodel.CallbackResp;
import com.app.rewardapp.adsManager.RewardAds;
import com.app.rewardapp.databinding.ActivityMathQuizBinding;
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.Fun;
import com.app.rewardapp.util.Session;

import java.util.Random;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MathQuiz extends AppCompatActivity implements AdImp {
    ActivityMathQuizBinding bind;
    Session session;
    Activity activity;
    AdManager adManager;
    RewardAds.Builder adNetwork;
    private static int quiz;
    int num1, num2,total;
    boolean complete=false;
    ProgressDialog pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind=ActivityMathQuizBinding.inflate(getLayoutInflater());
        setContentView(bind.getRoot());
        activity=this;
        bind.toolbar.setText(getString(R.string.activity_quiz));

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

        pb = new ProgressDialog(activity);
        pb.setMessage(getString(R.string.loading));
        pb.setCancelable(false);

        Que();
        loadReward();
        checklimit();

        bind.play.setOnClickListener(v -> {
            if(bind.ans.getText().toString().isEmpty()){
                Toast.makeText(activity, getString(R.string.enter_valid_answer), Toast.LENGTH_SHORT).show();
            }else{
                if(total==Integer.parseInt(bind.ans.getText().toString().trim())){
                    complete=true;
                    showads();
                    Toast.makeText(this, "Right Answer", Toast.LENGTH_SHORT).show();
                    bind.play.setEnabled(false);
                    bind.play.setAlpha(0.1f);
                }else{
                    showads();
                    Toast.makeText(this, "Wrong Answer", Toast.LENGTH_SHORT).show();
                    Que();
                }
            }
        });

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

    private void showads() {
        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);
        }
    }


    void Que(){
        Random random = new Random();
        num1=random.nextInt(60);
        num2=random.nextInt(99);
        bind.que.setText("What is Result of "+num1+" + "+num2);
        bind.ans.setText(null);
        total=num1+num2;
        bind.play.setAlpha(1f);
        bind.play.setEnabled(true);

    }

    private void checklimit() {
        ApiClient.getClient(this).create(ApiInterface.class).Api(Fun.data("2","","","","","",4,2,session.Auth())).enqueue(new Callback<CallbackResp>() {
            @Override
            public void onResponse(Call<CallbackResp> call, Response<CallbackResp> response) {
                if(response.isSuccessful() && response.body().getCode()==201){
                    int spinlimt=response.body().getSpinlimit();
                    int count=response.body().getCount();
                    if(count==0){
                        bind.limit.setText(String.valueOf(spinlimt));
                    }else {
                        int tot=spinlimt-count;
                        quiz=tot;
                        bind.limit.setText(String.valueOf(tot));
                    }
                }
            }

            @Override
            public void onFailure(Call<CallbackResp> call, Throwable t) {
            }
        });
    }

    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();
    }

    private void credit() {
        ApiClient.getClient(this).create(ApiInterface.class).Api(Fun.data("2","","","","","",8,0,session.Auth())).enqueue(new Callback<CallbackResp>() {
            @Override
            public void onResponse(Call<CallbackResp> call, Response<CallbackResp> response) {
                if(response.isSuccessful() && response.body().getCode()==201){
                    session.setIntData(session.WALLET,response.body().getBalance());
                    complete=false;
                    Toast.makeText(activity, ""+response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    if(quiz>0){
                        quiz=quiz-1;
                        bind.limit.setText(""+quiz);
                        Que();
                    }else {
                        bind.limit.setText(""+quiz);
                    }
                }else {
                    Toast.makeText(activity, ""+response.body().getMsg(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CallbackResp> call, Throwable t) {
            }
        });
    }

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

    @Override
    protected void onResume() {
        if(adNetwork.isCompleted()){
            adNetwork.setCompleted(false);
            adNetwork.buildAd();
            loadReward();
            if(complete){credit();}
        }else{
            loadReward();
        }
        super.onResume();
    }

}