package com.s1025.mikoto.builder;

import java.util.ArrayList;
import java.util.List;

import com.s1025.mikoto.model.button.ButtonBase;
import com.s1025.mikoto.model.button.ButtonClick;
import com.s1025.mikoto.model.button.ButtonMedia;
import com.s1025.mikoto.model.button.ButtonScancodePush;
import com.s1025.mikoto.model.button.ButtonScancodeWaitmsg;
import com.s1025.mikoto.model.button.ButtonSub;
import com.s1025.mikoto.model.button.ButtonType;
import com.s1025.mikoto.model.button.ButtonView;

public class MenuBuilder {
	private List<ButtonBase> menu = new ArrayList<ButtonBase>();
	
	public List<ButtonBase> getMenu() {
		return menu;
	}

	public void setMenu(List<ButtonBase> menu) {
		this.menu = menu;
	}

	public void add(ButtonBase button){
		menu.add(button);
	}
	
	public void add(ButtonBase button, int index){
		if(index>=menu.size()) menu.add(button);
		else if(index<menu.size()){
			ButtonBase buttonBase = menu.get(index);
			if(buttonBase instanceof ButtonSub){
				ButtonSub sub = (ButtonSub) buttonBase;
				sub.add(button);
			} else {
				menu.remove(index);
				menu.add(index,button);
			}
		}
	}
	
	public void addClick(String name, String key, int index){
		ButtonClick button = new ButtonClick();
		button.setType(ButtonType.CLICK);
		button.setName(name);
		button.setKey(key);
		this.add(button, index);
	}
	
	public void addView(String name, String url, int index){
		ButtonView button = new ButtonView();
		button.setType(ButtonType.VIEW);
		button.setName(name);
		button.setUrl(url);
		this.add(button, index);
	}
	
	public void addSub(String name, int index){
		ButtonSub button = new ButtonSub();
		button.setName(name);
		this.add(button, index);
	}
	
	public void addMedia(String name, String mediaId, int index){
		ButtonMedia button = new ButtonMedia();
		button.setName(name);
		button.setType(ButtonType.MEDIAID);
		button.setMediaId(mediaId);
		this.add(button, index);
	}
	
	public void addScancodePush(String name, String key, int index){
		ButtonScancodePush button = new ButtonScancodePush();
		button.setName(name);
		button.setType(ButtonType.SCANCODEPUSH);
		button.setKey(key);
		this.add(button, index);
	}
	
	public void addScancodeWaitmsg(String name, String key, int index){
		ButtonScancodeWaitmsg button = new ButtonScancodeWaitmsg();
		button.setName(name);
		button.setType(ButtonType.SCANCODEWAITMSG);
		button.setKey(key);
		this.add(button, index);
	}

	public String toJson(){
		String sub = "";
		for(int i =0; i<menu.size(); i++){
			ButtonBase button = menu.get(i);
			sub += button.toJson();
			if(i!=menu.size()-1){
				sub += ",";
			}
		}
		return "{\"button\":["+sub+"]}";
	}
}

@@ -735,36 +735,43 @@
 %0D%0A%09%7D%0D%0A%0D%0A%09public 
-void
+MenuBuilder
  add(ButtonBase 
@@ -797,24 +797,40 @@
 d(button);%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09publ
@@ -828,28 +828,35 @@
 %0A%09%0D%0A%09public 
-void
+MenuBuilder
  add(ButtonB
@@ -1186,16 +1186,32 @@
 %7D%0D%0A%09%09%7D%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09
@@ -1213,28 +1213,35 @@
 %0A%09%0D%0A%09public 
-void
+MenuBuilder
  addClick(St
@@ -1424,32 +1424,48 @@
 utton, index);%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09public v
@@ -1459,28 +1459,35 @@
 %0A%09%0D%0A%09public 
-void
+MenuBuilder
  addView(Str
@@ -1666,32 +1666,48 @@
 utton, index);%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09public v
@@ -1701,28 +1701,35 @@
 %0A%09%0D%0A%09public 
-void
+MenuBuilder
  addSub(Stri
@@ -1834,32 +1834,48 @@
 utton, index);%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09public v
@@ -1869,28 +1869,35 @@
 %0A%09%0D%0A%09public 
-void
+MenuBuilder
  addMedia(St
@@ -2094,32 +2094,48 @@
 utton, index);%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09public v
@@ -2125,36 +2125,43 @@
 %0A%09%7D%0D%0A%09%0D%0A%09public 
-void
+MenuBuilder
  addScancodePush
@@ -2372,24 +2372,40 @@
 n, index);%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09publ
@@ -2407,20 +2407,27 @@
 %09public 
-void
+MenuBuilder
  addScan
@@ -2662,16 +2662,32 @@
 ndex);%0D%0A
+%09%09return this;%0D%0A
 %09%7D%0D%0A%0D%0A%09p

change menubuilder can series add button