package com.webileapps.excalibur;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.JavascriptInterface;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

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

public class ExcaliburInterface {
    private ExcaliburFragment fragment;
    private Context context;

    ExcaliburInterface(Context context, ExcaliburFragment fragment) {
        this.fragment = fragment;
        this.context = context;
    }

    @JavascriptInterface
    public void clientInfo(String json) throws JSONException {
        Log.d("ExcaliburInterface ", "DDD clientInfo " + json);
        JSONObject jsonObject = new JSONObject(json);
        String title = jsonObject.getString("title");
        String bgColor = jsonObject.getString("bgColor");
        String textColor = jsonObject.getString("textColor");
        this.fragment.setTitle(title);
        this.fragment.setBgColor(bgColor);
        this.fragment.setTextColor(textColor);
        String action = this.context.getPackageName() + ".REFRESH_EXCALIBUR_INTERFACE";
        Intent intent = new Intent();
        intent.setAction(action);
        LocalBroadcastManager.getInstance(this.context).sendBroadcast(intent);
    }
}
