package com.reactnativebluestackmodule.common;

import com.mngads.util.MNGPreference;
import com.mngads.util.MNGGender;

import android.location.Location;
import android.util.Log;
import android.content.Context;

import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactApplicationContext;

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

public class BluestackAdsCommon {

    private static final String TAG = "BluestackAdsCommon";

    public static MNGPreference getMNGPreference(String jsonPreference, Context context) {
        MNGPreference mMNGPreference;
        mMNGPreference = new MNGPreference();

        try {
            Log.d(TAG, "jsonPreference : " + jsonPreference);
            JSONObject preferencejsonObject = new JSONObject(jsonPreference);

            if (preferencejsonObject.has("age") && !preferencejsonObject.isNull("age")) {
                Integer age = (Integer) preferencejsonObject.get("age");
                Log.d(TAG, "preferencejsonObject age: " + age);
                if (age != null) {
                    mMNGPreference.setAge(age);
                }
            }

            if (preferencejsonObject.has("gender") && !preferencejsonObject.isNull("gender")) {
                String gender = (String) preferencejsonObject.get("gender");
                if (gender != null) {
                    switch (gender) {
                        case "Female":
                            mMNGPreference.setGender(MNGGender.MNGGenderFemale);
                            break;
                        case "Male":
                            mMNGPreference.setGender(MNGGender.MNGGenderMale);
                            break;
                        default:
                            mMNGPreference.setGender(MNGGender.MNGGenderUnknown);
                    }
                }
            }

            if (preferencejsonObject.has("language") && !preferencejsonObject.isNull("language")) {
                String language = (String) preferencejsonObject.get("language");
                if (language != null) {
                    mMNGPreference.setLanguage(language);
                }
            }

            if (preferencejsonObject.has("contentUrl") && !preferencejsonObject.isNull("contentUrl")) {
                String contentUrl = (String) preferencejsonObject.get("contentUrl");
                if (contentUrl != null) {
                    mMNGPreference.setContentUrl(contentUrl);
                }
            }

            if (preferencejsonObject.has("keyword") && !preferencejsonObject.isNull("keyword")) {
                String keyword = (String) preferencejsonObject.get("keyword");
                if (keyword != null) {
                    mMNGPreference.setKeyword(keyword);
                    Log.d(TAG, "preferencejsonObject keyword: " + keyword);
                }
            }

            Integer consentFlag = 0;
            if (preferencejsonObject.has("consentFlag") && !preferencejsonObject.isNull("consentFlag")) {
                consentFlag = (Integer) preferencejsonObject.get("consentFlag");
                Log.d(TAG, "preferencejsonObject consentFlag: " + consentFlag);
            }

            if (preferencejsonObject.has("location") && !preferencejsonObject.isNull("location")) {
                JSONObject locationJsonObject = preferencejsonObject.getJSONObject("location");
                Location mLocation = new Location(locationJsonObject.get("provider").toString());
                mLocation.setLatitude((Double) locationJsonObject.get("latitude"));
                mLocation.setLongitude((Double) locationJsonObject.get("longitude"));

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

                mMNGPreference.setLocation(mLocation, consentFlag, context);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error saving json to preferencejsonObject: " + e.getMessage());
            e.printStackTrace();
        }

        if (mMNGPreference != null) {
            String mMNGPreferenceJSON = mMNGPreference.getJson(context).toString();
            Log.d(TAG, "mMNGPreference.getJson : " + mMNGPreferenceJSON);
        }

        return mMNGPreference;
    }
}
