package com.friendvilla.bucksbuddy;

import static android.util.Base64.DEFAULT;
import static android.util.Base64.decode;
import static android.util.Base64.encode;
import static com.friendvilla.bucksbuddy.MainActivity.USER;
import static com.friendvilla.bucksbuddy.Method.isConnected;
import static com.friendvilla.bucksbuddy.Method.processTxn;
import static com.friendvilla.bucksbuddy.controls.Constant.BONUS;
import static com.friendvilla.bucksbuddy.controls.Constant.GET_SETTING;
import static com.friendvilla.bucksbuddy.controls.Constant.GET_USER;
import static com.friendvilla.bucksbuddy.controls.Constant.LINK;
import static com.friendvilla.bucksbuddy.controls.Constant.REDEEM;
import static com.friendvilla.bucksbuddy.controls.Constant.REFER;
import static com.friendvilla.bucksbuddy.controls.Constant.Tag;
import static com.friendvilla.bucksbuddy.controls.Constant.USER_ID;
import static com.friendvilla.bucksbuddy.controls.Constant.VERSION;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.friendvilla.bucksbuddy.controls.MessageBox;
import com.friendvilla.bucksbuddy.model.User;

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

import java.util.Random;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends AppCompatActivity {

    @SuppressLint("HardwareIds")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        USER_ID = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
        if(isConnected(this)) {
            String BASE = new String(decode(BuildConfig.BASE_URL,DEFAULT));
            String URL = BASE + GET_SETTING;
            Volley.newRequestQueue(this).add(new JsonObjectRequest(URL,response -> {
                try {
                    BONUS = response.getInt("bonus");
                    REFER = response.getInt("refer");
                    REDEEM = response.getInt("redeem");
                    LINK = response.getString("link");
                    VERSION = response.getInt("version");
                    getUserData();
                } catch (JSONException e) {
                    MessageBox msg = new MessageBox(e.getMessage());
                    msg.show(getSupportFragmentManager(),Tag);
                }
            }, error -> {
                MessageBox msg = new MessageBox(error.getMessage());
                msg.show(getSupportFragmentManager(),Tag);
            }));
        } else {
            MessageBox message = new MessageBox("No internet connection found");
            message.show(getSupportFragmentManager(),Tag);
        }
    }

    private void getUserData() {
        String BASE = new String(decode(BuildConfig.BASE_URL,DEFAULT));
        String HASH = new String(encode(USER_ID.getBytes(),DEFAULT));
        String URL = BASE + GET_USER + HASH;
        Volley.newRequestQueue(this).add(new JsonObjectRequest(URL,response -> {
            try {
                JSONArray res = response.getJSONArray("result");
                JSONObject obj = res.getJSONObject(0);
                if(obj.getString("success").equals("0")) {
                    char[] TEMP_ID = USER_ID.toCharArray();
                    StringBuilder sb = new StringBuilder();
                    for(int o=0; o<8; o++) {
                        sb.append(TEMP_ID[new Random().nextInt(TEMP_ID.length)]);
                    }
                    String txn = "Joinning Bonus," + BONUS + ",BucksBuddy Pvt. Ltd.";
                    processTxn(this,1,txn);
                    USER.setValue(new User(String.valueOf(BONUS),"0",sb.toString(),"",""));
                } else {
                    JSONObject pro = obj.getJSONObject("profile");
                    String deposit = pro.getString("deposit");
                    String withdraw = pro.getString("withdraw");
                    String refercode = pro.getString("refercode");
                    String checkin = pro.getString("mobile");
                    String referby = pro.getString("referby");
                    USER.setValue(new User(deposit,withdraw,refercode,checkin,referby));
                }
                startActivity(new Intent(this,MainActivity.class));
                finish();
            } catch (JSONException e) {
                MessageBox msg = new MessageBox(e.getMessage());
                msg.show(getSupportFragmentManager(),Tag);
            }
        }, error -> {
            MessageBox msg = new MessageBox(error.getMessage());
            msg.show(getSupportFragmentManager(),Tag);
        }));
    }
}