package com.secspace.lib.common.utils;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ProviderCall {

    public static Bundle call(Context context, String authority, String method, String arg, Bundle bundle) {
        try {
            Uri uri = Uri.parse("content://" + authority);
            return context.getContentResolver().call(uri, method, arg, bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Bundle();
    }

    public static final class Builder {

        private Context context;

        private Bundle bundle = new Bundle();

        private String method;
        private String auth;
        private String arg;

        public Builder(Context context, String auth) {
            this.context = context;
            this.auth = auth;
        }

        public Builder methodName(String name) {
            this.method = name;
            return this;
        }

        public Builder arg(String arg) {
            this.arg = arg;
            return this;
        }

        public Builder addArgs(String key, Object value) {
            if (value != null) {
                if (value instanceof Boolean) {
                    bundle.putBoolean(key, (Boolean) value);
                } else if (value instanceof Integer) {
                    bundle.putInt(key, (Integer) value);
                } else if (value instanceof Long) {
                    bundle.putLong(key, (Long) value);
                } else if (value instanceof String) {
                    bundle.putString(key, (String) value);
                } else if (value instanceof CharSequence) {
                    bundle.putCharSequence(key, (CharSequence) value);
                } else if (value instanceof Serializable) {
                    bundle.putSerializable(key, (Serializable) value);
                } else if (value instanceof Bundle) {
                    bundle.putBundle(key, (Bundle) value);
                } else if (value instanceof Parcelable) {
                    bundle.putParcelable(key, (Parcelable) value);
                } else if (value instanceof int[]) {
                    bundle.putIntArray(key, (int[]) value);
                } else if (value instanceof Byte[]) {
                    bundle.putByteArray(key, (byte[]) value);
                } else if (value instanceof String[]) {
                    bundle.putStringArray(key, (String[]) value);
                } else if (value instanceof Parcelable[]) {
                    bundle.putParcelableArray(key, (Parcelable[]) value);
                } else if (value instanceof List) {
                    if (((List) value).get(0) instanceof Integer) {
                        bundle.putIntegerArrayList(key, (ArrayList<Integer>) value);
                    } else if (((List) value).get(0) instanceof String) {
                        bundle.putStringArrayList(key, (ArrayList<String>) value);
                    } else if (((List) value).get(0) instanceof Parcelable) {
                        bundle.putParcelableArrayList(key, (ArrayList<? extends Parcelable>) value);
                    }
                } else {
                    throw new IllegalArgumentException("Unknown type " + value.getClass() + " in Bundle.");
                }
            }
            return this;
        }

        public Bundle call() {
            return ProviderCall.call(context, auth, method, arg, bundle);
        }

    }

}
