package com.zenit.habclient;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.zenit.habclient.command.CommandAnalyzer;
import com.zenit.habclient.command.ICommandAnalyzer;
import com.zenit.habclient.rule.RuleOperationProvider;
import com.zenit.habclient.util.RegularExpression;

import java.util.HashMap;
import java.util.Locale;
import java.util.UUID;

/**
 * Created by Tony Alpskog in 2013.
 */
public class HABApplication extends Application {

    //TA: TODO - Replace all static usage of members and methods. Call HABApplication as (HABApplication)getActivity().getApplication() -> Create a method for this in all classes.

    private HashMap<UUID, HashMap<UUID, GraphicUnit>> roomUnitList = new HashMap<UUID, HashMap<UUID, GraphicUnit>>();
    private RoomProvider mRoomProvider = null;
    private UUID currentConfigRoom = null;
    private UUID currentFlipperRoom = null;
    private static RuleOperationProvider mRuleOperationProvider = null;
    private static RestCommunication mRestCommunication = null;
    private static ApplicationMode mAppMode = ApplicationMode.Unknown;
    private TextToSpeechProvider mTextToSpeechProvider = null;
    private static OpenHABWidgetProvider mOpenHABWidgetProvider = null;
    private static ICommandAnalyzer mSpeechResultAnalyzer = null;
    private static RegularExpression mRegularExpression = null;
    private static OpenHABWidgetControl mOpenHABWidgetControl = null;

    public ICommandAnalyzer getSpeechResultAnalyzer() {
        if(mSpeechResultAnalyzer == null)
            mSpeechResultAnalyzer = new CommandAnalyzer(getRoomProvider(), getOpenHABWidgetProvider(), getApplicationContext());

        mSpeechResultAnalyzer.setTextToSpeechProvider(getTextToSpeechProvider());
        return mSpeechResultAnalyzer;
    }

    public TextToSpeechProvider getTextToSpeechProvider() {
        if(mTextToSpeechProvider == null)
            mTextToSpeechProvider = new TextToSpeechProvider(getApplicationContext(), Locale.ENGLISH);
        return mTextToSpeechProvider;
    }

    public static ApplicationMode getAppMode() {
        return mAppMode;
    }

    public static void setAppMode(ApplicationMode appMode) {
        mAppMode = appMode;
    }

    public OpenHABWidgetProvider getOpenHABWidgetProvider() {
        return getOpenHABWidgetProvider2();
    }

    public static OpenHABWidgetProvider getOpenHABWidgetProvider2() {
        if(mOpenHABWidgetProvider == null)
            mOpenHABWidgetProvider = new OpenHABWidgetProvider();

        return mOpenHABWidgetProvider;
    }

    public static RestCommunication getRestCommunication() {
        if(mRestCommunication == null)
            mRestCommunication = new RestCommunication();

        return mRestCommunication;
    }

    private static OpenHABSetting mOpenHABSetting;
    public static OpenHABSetting getOpenHABSetting(Context context) {
        if(mOpenHABSetting == null)
            mOpenHABSetting = new OpenHABSetting(context);
        return mOpenHABSetting;
    }

    public static String getLogTag() {
        return getLogTag(1);//Actually gets index 0(zero) but this call adds one more level to the stacktrace.
    }

    public static String getLogTag(int relativeTraceIndex) {
        int traceIndex = 3 + relativeTraceIndex;
        StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[traceIndex];
        return stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "()";
    }

    public Room getConfigRoom() {
        if(currentConfigRoom == null)
            return null;

        return getRoom(currentConfigRoom);
//        Log.d(getLogTag(), String.format("Getting config room: name = '%s'  item name = '%s'  UUID = '%s'", room.getName(), room.getGroupWidgetId(), room.getId()));
    }

    public void setConfigRoom(Room room) {
        if(room != null) {
            Log.d(getLogTag(), String.format("Setting config room: name = '%s'  widget UUID = '%s' room UUID = '%s'", room.getName(), room.getRoomWidget() != null? room.getRoomWidget().getId() : "<Widget is NULL>", room.getId()));
            currentConfigRoom = room.getId();
        } else {
            Log.d(getLogTag(), "Setting config room: New Room");
            currentConfigRoom = null;
        }
    }

    public Room getFlipperRoom() {
        Room room = getRoom(currentFlipperRoom);
        currentFlipperRoom = room.getId();
        return room;
    }

    public void setFlipperRoom(Room room) {
        currentFlipperRoom = room.getId();
    }

    private Room getRoom(UUID roomId) {
        if(roomId == null)
            return getRoomProvider().getInitialRoom();
        else
            return getRoomProvider().get(roomId);
    }

    public RoomProvider getRoomProvider() {
        if(mRoomProvider == null)
            mRoomProvider = new RoomProvider(getApplicationContext());

        return mRoomProvider;
    }

    public RuleOperationProvider getRuleOperationProvider() {
        return getRuleOperationProvider(getApplicationContext());
    }

    public static RuleOperationProvider getRuleOperationProvider(Context context) {
        if(mRuleOperationProvider == null)
            mRuleOperationProvider = new RuleOperationProvider(context);

        return mRuleOperationProvider;
    }

    public static RegularExpression getRegularExpression() {
        if(mRegularExpression == null)
            mRegularExpression = new RegularExpression();

        return mRegularExpression;
    }

    public static OpenHABWidgetControl getOpenHABWidgetControl(Context context) {
        if(mOpenHABWidgetControl == null)
            mOpenHABWidgetControl = new OpenHABWidgetControl(context);

        return mOpenHABWidgetControl;
    }
}

@@ -329,34 +329,8 @@
 n;%0A%0A
-import java.util.HashMap;%0A
 impo
@@ -654,126 +654,8 @@
 s.%0A%0A
-    private HashMap%3CUUID, HashMap%3CUUID, GraphicUnit%3E%3E roomUnitList = new HashMap%3CUUID, HashMap%3CUUID, GraphicUnit%3E%3E();%0A
     

Remove unused code