package godau.fynn.dsbdirect;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import godau.fynn.dsbdirect.manager.DownloadManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public class NewsQuery implements Runnable {

    /*
     * News structure:
     * {
     *  "statusMessage": "Message for the user",
     *  "statusMessageDE": "Nachricht an Nutzer",
     *  "queryBodyBaseJson": "optional: das neue queryBodyBaseJson",
     *  "querySendDate": boolean, optional
     *  "querySendLastDate": boolean, optional
     *  "queryHeaders": ["optional", "Array mit headers"]
     *  "queryEndpoint": 0 (mobile) / 1 (web) / 2 (ihkmobile) / 3 (appihkbb)
     * }
     */

    Activity context;
    DownloadManager downloadManager;
    Utility u;

    public NewsQuery(Activity context, DownloadManager downloadManager) {
        this.context = context;
        this.downloadManager = downloadManager;
        u = new Utility(context);
    }

    @Override
    public void run() {
        try {
            JSONObject news = downloadManager.downloadNews();

            // Get message from news

            String message;

            // German or not german (hardcoded and uncool)
            if (Locale.getDefault().toString().contains("de")) {
                message = news.getString("statusMessageDE");
            } else {
                message = news.getString("statusMessage");
            }

            // Get new query body base json
            if (news.has("queryBodyBaseJson")) {
                u.getSharedPreferences()
                        .edit()
                        .putString("queryBodyBaseJson", news.getString("queryBodyBaseJson"))
                        .apply();
            }

            // Get new date bool
            if (news.has("querySendDate")) {
                u.getSharedPreferences()
                        .edit()
                        .putBoolean("querySendDate", news.getBoolean("querySendDate"))
                        .apply();
            }

            // Get new query send last date bool
            if (news.has("querySendLastDate")) {
                u.getSharedPreferences()
                        .edit()
                        .putBoolean("querySendLastDate", news.getBoolean("querySendLastDate"))
                        .apply();
            }

            // Get new headers
            if (news.has("queryHeaders")) {
                JSONArray headers = news.getJSONArray("queryHeaders");

                Set<String> headerSet = new HashSet<String>();

                for (int i = 0; i < headers.length(); i++) {
                    String header = headers.getString(i);
                    headerSet.add(header);
                }

                u.getSharedPreferences()
                        .edit()
                        .putStringSet("queryHeaders", headerSet)
                        .apply();
            }

            // Get new endpoint
            if (news.has("queryEndpoint")) {
                int endpoint = news.getInt("queryEndpoint");

                u.getSharedPreferences()
                        .edit()
                        .putInt("queryEndpoint", endpoint)
                        .apply();
            }


            // Show news
            popup(message);

        } catch (JSONException | DownloadManager.UnexpectedResponseException ex) {
            popup(context.getString(R.string.news_network_invalid_response));
            ex.printStackTrace();
        } catch (IOException ex) {
            popup(context.getString(R.string.news_network_error_generic));
            ex.printStackTrace();
        }
    }

    private void popup(final String message) {
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new AlertDialog.Builder(context)
                        .setMessage(message)
                        .show();
            }
        });

    }

    public static void wipeNews(Context context) {
        new Utility(context).getSharedPreferences().edit()
                .remove("queryBodyBaseJson")
                .remove("querySendDate")
                .remove("querySendLastDate")
                .remove("queryHeaders")
                .remove("queryEndpoint")
                .apply();
    }
}
