package com.team.app.activities;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.team.app.Constant;
import com.team.app.MyApplication;
import com.team.app.R;

import org.json.JSONException;
import org.json.JSONObject;

import static com.team.app.Constant.APP_URL_MAIN;

public class Splash_Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        MyApplication myApplication = MyApplication.getInstance();
        loadAdCodes();
    }

    private void loadAdCodes() {
        String JSON_URL = APP_URL_MAIN + "api.php?ads=true&package=" + getPackageName();
        JsonArrayRequest request = new JsonArrayRequest(JSON_URL, response -> {
            if(response.length()>0) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject object = response.getJSONObject(i);
                        String id = object.getString("adshow");
                        Constant.adShown = id.equals("0");
                        Constant.BANNER_ADS_ID = object.getString("b_id").replace("\\","");
                        Constant.INTERTIAL_ADS_ID = object.getString("i_id").replace("\\","");
                        Constant.NATVE_NORMAL_ADS_ID = object.getString("n_id").replace("\\","");
                    } catch (JSONException exception) {
                        Toast.makeText(Splash_Activity.this, "Ads Fatching Error", Toast.LENGTH_SHORT).show();
                    }
                }
                startActivity(new Intent(Splash_Activity.this, Start_Activity.class));
                finish();
            } else {
                Toast.makeText(Splash_Activity.this, "No Ads Found", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Splash_Activity.this, Start_Activity.class));
                finish();
            }
        }, error -> {
            Toast.makeText(Splash_Activity.this, "No Valid Response Found", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(Splash_Activity.this, Start_Activity.class));
            finish();
        });
        RequestQueue requestQueue = Volley.newRequestQueue(Splash_Activity.this);
        requestQueue.add(request);
    }

}
