package com.reactnativebluestackmodule;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Promise;
import com.mngads.MNGAdsFactory;
import com.mngads.listener.MNGClickListener;
import com.mngads.listener.MNGInterstitialListener;
import com.mngads.util.MNGPreference;
import com.mngads.util.MNGGender;
import android.location.Location;

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

public class BluestackPreference extends ReactContextBaseJavaModule {

    private static final String TAG = "BluestackPreference Bridge";
    private static final String REACT_CLASS = "BluestackPreference";

    private MNGPreference preference;
    private MNGGender mGender = MNGGender.MNGGenderUnknown;

    public BluestackPreference(ReactApplicationContext reactContext) {
        super(reactContext);
        // reactContext.addLifecycleEventListener(this);

        // instantiate MNGPreference
        if (preference == null) {
            preference = new MNGPreference();
        }
    }

    @ReactMethod
    public void setAge(Integer age) {
        preference.setAge(age);
    }

    @ReactMethod
    public void setLocation(String location, Integer consentFlag) {

        try {
            JSONObject locationJsonObject = new JSONObject(location);
            Location mLocation = new Location(locationJsonObject.get("provider").toString());
            mLocation.setLatitude((Integer) locationJsonObject.get("latitude"));
            mLocation.setLongitude((Integer) locationJsonObject.get("longitude"));

            Log.d(TAG, "Preference Location: " + mLocation.getLatitude());

            ReactApplicationContext reactContext = this.getReactApplicationContext();
            preference.setLocation(mLocation, consentFlag, reactContext);

        } catch (Exception e) {
            Log.d(TAG, "Error converting json to locationJsonObject: " + e.getMessage());
        }
    }

    @ReactMethod
    public void setLanguage(String language) {
        preference.setLanguage(language);
    }

    @ReactMethod
    public void setGender(String gender) {
        switch (gender) {
            case "Female":
                preference.setGender(MNGGender.MNGGenderFemale);
                break;
            case "Male":
                preference.setGender(MNGGender.MNGGenderMale);
                break;
            default:
                preference.setGender(MNGGender.MNGGenderUnknown);
        }
    }

    public void setGender(MNGGender gender) {
        this.mGender = gender;
    }

    @ReactMethod
    public void setKeyword(String keyword) {
        preference.setKeyword(keyword);
    }

    @ReactMethod
    public void setContentUrl(String contentUrl) {
        preference.setContentUrl(contentUrl);
    }

    @ReactMethod(isBlockingSynchronousMethod = true)
    public String getMNGPreferenceJSONObj() {
        try {
            // Convert custom object to JSON string
            ReactApplicationContext reactContext = this.getReactApplicationContext();
            JSONObject prefJSONObj = preference.getJson(reactContext);

            Log.d(TAG, "Converting Preference to json: " + prefJSONObj.toString());
            return prefJSONObj.toString(); // Send JSON string as preference

        } catch (Exception e) {
            // Handle errors if any
            Log.d(TAG, "Error converting Preference to json: " + e.getMessage());
        }

        return "{}";
    }

    @ReactMethod
    public void getMNGPreference(Promise promise) {
        try {
            promise.resolve(preference);
        } catch (Exception e) {
            promise.reject("Error", e);
        }
    }

    @NonNull
    @Override
    public String getName() {
        return REACT_CLASS;
    }

    public void onDestroy() {
        if (preference != null) {
            preference = null;
        }
    }
}
