/*
 * Copyright 2012 Kulikov Dmitriy
 * Copyright 2015-2016 Nickolay Savchenko
 * Copyright 2017-2018 Nikita Shakarun
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package javax.microedition.lcdui;

import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.util.ArrayList;

import javax.microedition.lcdui.event.CommandActionEvent;
import javax.microedition.lcdui.event.Event;
import javax.microedition.lcdui.event.EventQueue;
import javax.microedition.shell.MicroActivity;
import javax.microedition.util.ContextHolder;

public abstract class Displayable {
	private MicroActivity parent;
	private String title;

	private ArrayList<Command> commands;
	private CommandListener listener;

	private static EventQueue queue;

	static {
		queue = new EventQueue();
		queue.startProcessing();
	}

	public Displayable() {
		commands = new ArrayList<>();
		listener = null;
	}

	public void setParentActivity(MicroActivity activity) {
		parent = activity;

		clearDisplayableView();
	}

	public AppCompatActivity getParentActivity() {
		if (parent == null) {
			return ContextHolder.getCurrentActivity();
		}
		return parent;
	}

	public void setTitle(String title) {
		this.title = title;

		if (parent != null) {
			parent.runOnUiThread(() -> parent.getSupportActionBar().setTitle(Displayable.this.title));
		}
	}

	public String getTitle() {
		return title;
	}

	public boolean isShown() {
		if (parent != null) {
			return parent.isVisible() && parent.getCurrent() == this;
		}

		return false;
	}

	public abstract View getDisplayableView();

	public abstract void clearDisplayableView();

	public void addCommand(Command cmd) {
		commands.add(cmd);
	}

	public void removeCommand(Command cmd) {
		commands.remove(cmd);
	}

	public void removeAllCommands() {
		commands.clear();
	}

	public int countCommands() {
		return commands.size();
	}

	public Command[] getCommands() {
		return commands.toArray(new Command[0]);
	}

	public CommandListener getCommandListener() {
		return listener;
	}

	public void setCommandListener(CommandListener listener) {
		this.listener = listener;
	}

	public void fireCommandAction(Command c, Displayable d) {
		if (listener != null) {
			queue.postEvent(CommandActionEvent.getInstance(listener, c, d));
		}
	}

	public EventQueue getEventQueue() {
		return queue;
	}

	public void postEvent(Event event) {
		queue.postEvent(event);
	}

	public int getWidth() {
		return ContextHolder.getDisplayWidth();
	}

	public int getHeight() {
		return ContextHolder.getDisplayHeight();
	}
}

@@ -783,16 +783,45 @@
 ew.View;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -1253,16 +1253,40 @@
 istener;
+%0A%09private Ticker ticker;
 %0A%0A%09priva
@@ -3103,11 +3103,201 @@
 ht();%0A%09%7D
+%0A%0A%09public void setTicker(Ticker ticker) %7B%0A%09%09this.ticker = ticker;%0A%09%09Toast.makeText(parent, ticker.getString(), Toast.LENGTH_LONG).show();%0A%09%7D%0A%0A%09public Ticker getTicker() %7B%0A%09%09return ticker;%0A%09%7D
 %0A%7D%0A

Implement Ticker in Displayable