package com.azerion.bluestack.react.common;

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

import com.azerion.bluestack.RequestOptions;
import com.azerion.bluestack.react.utils.BlueStackLogger;
import com.azerion.bluestack.util.Gender;

import org.json.JSONObject;

public class BluestackAdsCommon {

    private static final String TAG = "BluestackAdsCommon";

    public static RequestOptions getRequestOptions(String jsonPreference, Context context) {

        int _adChoicePosition = 0;
        int _age = -1;
        Location _location = null;
        int _consentFlag = 0;
        String _language = null;
        String _keyword = null;
        String _contentUrl = null;
        Gender _gender = Gender.GenderUnknown;

        RequestOptions requestOptions = null;

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

            if (preferencejsonObject.has("age") && !preferencejsonObject.isNull("age")) {
                Integer age = (Integer) preferencejsonObject.get("age");
                if (age != null) {
                    _age = age;
                }
            }

            if (preferencejsonObject.has("gender") && !preferencejsonObject.isNull("gender")) {
                String gender = (String) preferencejsonObject.get("gender");
                if (gender != null) {
                    switch (gender) {
                        case "Female":
                            _gender = Gender.GenderFemale;
                            break;
                        case "Male":
                            _gender = Gender.GenderMale;
                            break;
                        default:
                            _gender = Gender.GenderUnknown;
                    }
                }
            }

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

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

            if (preferencejsonObject.has("keyword") && !preferencejsonObject.isNull("keyword")) {
                String keyword = (String) preferencejsonObject.get("keyword");
                if (keyword != null) {
                    _keyword = keyword;
                }
            }

            Integer consentFlag = 0;
            if (preferencejsonObject.has("consentFlag") && !preferencejsonObject.isNull("consentFlag")) {
                consentFlag = (Integer) preferencejsonObject.get("consentFlag");
                _consentFlag = consentFlag;
            }

            if (preferencejsonObject.has("location") && !preferencejsonObject.isNull("location")) {
                JSONObject locationJsonObject = preferencejsonObject.getJSONObject("location");
                Location location = new Location(locationJsonObject.get("provider").toString());
                location.setLatitude((Double) locationJsonObject.get("latitude"));
                location.setLongitude((Double) locationJsonObject.get("longitude"));
                _location = location;
            }
        } catch (Exception e) {
            BlueStackLogger.error(TAG, "Error saving json to preferencejsonObject: " + e.getMessage());
            e.printStackTrace();
        }

        if (requestOptions == null) {
            requestOptions = new RequestOptions(
                    context,
                    _adChoicePosition,
                    _age,
                    _consentFlag,
                    _location,
                    _language,
                    _keyword,
                    _contentUrl,
                    _gender);
        }
        printRequestOptions(requestOptions);
        return requestOptions;
    }

    public static void printRequestOptions(RequestOptions requestOptions) {
        if (requestOptions != null) {
            System.out.println("RequestOptions:");
            System.out.println("  Age: " + requestOptions.toPreference().getAge());
            System.out.println("  Consent Flag: " + requestOptions.toPreference().getConsentFlag());
            System.out.println("  Location: " + requestOptions.toPreference().getLocation());
            System.out.println("  Language: " + requestOptions.toPreference().getLanguage());
            System.out.println("  Keyword: " + requestOptions.toPreference().getKeyword());
            System.out.println("  Content URL: " + requestOptions.toPreference().getContentUrl());
            System.out.println("  Gender: " + requestOptions.toPreference().getGender());
        } else {
            System.out.println("RequestOptions: Not created yet.");
        }
    }
}
