package com.webileapps.excalibur;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import java.util.UUID;

public class Excalibur {

    public static final String TAG = "ExcaliburOBJECT";
    static final int EXCALIBUR_ID = 8375667;
    static final String IS_SHOWN = "IS_ACTIVITY_ACTIVE";
    static final String ONLY_BIND = "ONLY_BIND";
    static final String CONFIG_CLIENT_ID = "CLIENT_ID";
    static final String CONFIG_USER_ID = "USER_ID";
    static final String CONFIG_FCM_TOKEN = "FCM_TOKEN";
    static final String CONFIG_STAGING = "IS_STAGING";
    static final String CONFIG_FIELDS = "CUSTOM_FIELDS";
    static final String SHARED_PREFERENCE_FILE_NAME = "com.webileapps.excalibur";
    private Context context;
    private String userId;
    private String clientId;
    private String fcmToken;
    private String userName;
    private boolean isStaging;

    public Excalibur(Context context, ExcaliburConfig config) {
        this.context = context;
        this.userId = this.retrieveUserId(config);
        this.clientId = config.getClientId();
        this.fcmToken = config.getFcmToken();
        this.isStaging = config.getStaging();
        config.save(this.getPreferences());
        this.startService();
    }

    public void login(String userId,String userName) {
        this.login(userId, userName,(String)null);
    }

    public void login(String userId,String userName, String fcmToken) {
        this.stopService();
        this.userId = userId;
        this.fcmToken = fcmToken;
        this.userName = userName;
        SharedPreferences.Editor editor = this.getPreferences().edit();
        editor.putString("USER_ID", this.userId);
        editor.putString("FCM_TOKEN", this.fcmToken);
        editor.putString("USER_NAME",this.userName);
        editor.apply();
        this.startService();
    }



    public void logout() {
        this.stopService();
        if (this.fcmToken != null) {
            ExcaliburLogoutService.logout(this.context, this.clientId, this.userId, this.fcmToken, this.isStaging);
        }

        SharedPreferences.Editor editor = this.getPreferences().edit();
        editor.putString("USER_ID", (String)null);
        editor.putString("FCM_TOKEN", (String)null);
        editor.apply();
    }

    public void showChat() {
        this.startService();
        Intent i = new Intent(this.context, ExcaliburActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.context.startActivity(i);
    }

    private String retrieveUserId(ExcaliburConfig config) {
        if (config.getUserId() == null) {
            SharedPreferences preferences = this.getPreferences();
            this.userId = preferences.getString("USER_ID", (String)null);
            if (this.userId == null) {
                this.userId = UUID.randomUUID().toString();
                preferences.edit().putString("USER_ID", this.userId).apply();
            }

            config.setUserId(this.userId);
            return this.userId;
        } else {
            return this.userId = config.getUserId();
        }
    }

    private SharedPreferences getPreferences() {
        return this.context.getSharedPreferences("com.webileapps.excalibur", 0);
    }

    private void startService() {
        Intent intent = new Intent(this.context, ExcaliburService.class);
        this.context.startService(intent);
    }

    private void stopService() {
        Intent intent = new Intent(this.context, ExcaliburService.class);
        this.context.stopService(intent);
    }
}

