package space.ibrahim.library;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;


public class CheckAppUpdate {
    private static final String TAG = "CheckAppUpdateService";
    private final Activity activity;
    private
    @ColorInt
    int color = -1;
    private long cache = 43200;
    @StringRes
    private int message = -1;
    @StringRes
    private int buttonText = -1;
    private FirebaseRemoteConfig remoteConfig;

    public CheckAppUpdate(Activity activity) {
        this.activity = activity;
    }

    private void setColor(@ColorInt int color) {
        this.color = color;
    }

    public int getColor() {
        return color;
    }

    public long getCache() {
        return cache;
    }

    private void setCache(long cache) {
        this.cache = cache;
    }

    private void setMessage(@StringRes int message) {
        this.message = message;
    }

    public int getMessage() {
        return message;
    }

    private void setButtonText(@StringRes int buttonText) {
        this.buttonText = buttonText;
    }

    public int getButtonText() {
        return buttonText;
    }

    public static class Builder {

        private CheckAppUpdate instance;

        public Builder(Activity activity) {
            instance = new CheckAppUpdate(activity);
        }

        /**
         * Sets the color of the dialog background
         *
         * @param color resource int
         * @return Builder
         */
        public CheckAppUpdate.Builder setColor(@ColorInt int color) {
            instance.setColor(color);
            return this;
        }

        /**
         * Sets the length of time the service should read from cache
         *
         * @param cache time in seconds
         * @return
         */
        public CheckAppUpdate.Builder setCache(long cache) {
            this.instance.setCache(cache);
            return this;
        }


        /**
         * Sets the content message of the dialog
         *
         * @param message String resource
         * @return
         */
        public CheckAppUpdate.Builder setMessage(@StringRes int message) {
            instance.setMessage(message);
            return this;
        }

        /**
         * Sets the button text of the dialog
         *
         * @param buttonText String resource
         * @return
         */
        public CheckAppUpdate.Builder setButtonText(@StringRes int buttonText) {
            this.instance.setButtonText(buttonText);
            return this;
        }

        public CheckAppUpdate build() {
            return this.instance;
        }

    }

    public void checkForUpdate() {
        remoteConfig = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setDeveloperModeEnabled(true)
                .build();

        remoteConfig.setConfigSettings(configSettings);

        remoteConfig.fetch(cache)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        remoteConfig.activateFetched();
                        String url = remoteConfig.getString("android");
                        boolean isOptional = remoteConfig.getBoolean("android_optional");

                        Config config = new Config(isOptional, url, color, cache, message, buttonText);

                        if (!url.isEmpty()) {
                            Intent appUpdateIntent = new Intent(activity, UpdateAppActivity.class);
                            appUpdateIntent.putExtra("data", config);
                            if (!activity.isDestroyed())
                                activity.startActivity(appUpdateIntent);
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        e.printStackTrace();
                    }
                });

    }
}