package aji;

import java.io.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
//import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;


/*
 * InsertWindow.java レシピ登録画面
 * 作成日: 2005/11/04
 *
 * TODO この生成されたファイルのテンプレートを変更するには次へジャンプ:
 * ウィンドウ - 設定 - Java - コード・スタイル - コード・テンプレート
 */

/**z
 * @author yui
 *
 * TODO この生成された型コメントのテンプレートを変更するには次へジャンプ:
 * ウィンドウ - 設定 - Java - コード・スタイル - コード・テンプレート
 */

//材料検索
public class SearchMateTest{
		Singleton instance = Singleton.getInstance();
		private Composite c1;
		private Text[] HiraText = new Text[21];
		private Text[] SuuText = new Text[24];
		private Combo cm1 ;
		private Combo cm2 ;
		private Combo cm3 ;
		private List list1;
		private int getComboBig,getComboMiddle,i = 0;
		private String Bigitem[] = {"野菜類","肉類","魚類","きのこ類","果物類","その他"};
		private String Miditem[][] = new String[6][100];
		//1:ぶた　2:ぎゅう　3:とり　4:ひつじ
		private String Smlitem[][] = { {"ろーす","ばら","もも","ひれ","ひきにく","ればー","とんそく"},
										{"さーろいん","ろーす","ひれ","もも","らんぷ","ひきにく","たん","はつ","ればー","まめ","みの","ひも","てーる"},
										{"もも","むね","かわ","ささみ","ひきにく","はつ","ればー","すなぎも"},
										{"かた","ろーす","もも"},
										{" "},
										{" "}};
	
		

		public SearchMateTest(){
			try {
				
				File f = new File("niku.txt");
				System.out.println(f.exists());
				
				FileInputStream fileYasai = new FileInputStream("yasai.txt");
			    InputStreamReader inputYasai = new InputStreamReader(fileYasai);
			    BufferedReader br = new BufferedReader(inputYasai);
				
			    FileInputStream fileNiku = new FileInputStream("niku.txt");
			    InputStreamReader inputNiku = new InputStreamReader(fileNiku);
			    BufferedReader br2 = new BufferedReader(inputNiku);
			    
			    FileInputStream fileSakana = new FileInputStream("sakana.txt");
			    InputStreamReader inputSakana = new InputStreamReader(fileSakana);
			    BufferedReader br3 = new BufferedReader(inputSakana);
			    
			    FileInputStream fileKinoko = new FileInputStream("kinoko.txt");
			    InputStreamReader inputKinoko = new InputStreamReader(fileKinoko);
			    BufferedReader br4 = new BufferedReader(inputKinoko);
			    
			    FileInputStream fileKudamono = new FileInputStream("kudamono.txt");
			    InputStreamReader inputKudamono = new InputStreamReader(fileKudamono);
			    BufferedReader br5 = new BufferedReader(inputKudamono);
			    
			    FileInputStream fileSonota = new FileInputStream("sonota.txt");
			    InputStreamReader inputSonota = new InputStreamReader(fileSonota);
			    BufferedReader br6 = new BufferedReader(inputSonota);
			    BufferedReader brr[] ={br,br2,br3,br4,br5,br6};
			    for(int k = 0; k < 6; k++){
			    	i = 0;
			    	while((Miditem[k][i] = brr[k].readLine()) != null){
					    if(k == 0){
			    			System.out.println(k+" "+i+" "+Miditem[k][i]);
			    		}else if(k == 1){
			    			System.out.println(k+" "+i+" "+Miditem[k][i]);
			    		}else if(k == 2){
			    			System.out.println(k+" "+i+" "+Miditem[k][i]);
			    		}else if(k == 3){
			    			System.out.println(k+" "+i+" "+Miditem[k][i]);
			    		}else if(k == 4){
			    			System.out.println(k+" "+i+" "+Miditem[k][i]);
			    		}else if(k == 5){
			    			System.out.println(k+" "+i+" "+Miditem[k][i]);
			    		}
			    		i++;
			    	}
			    }
			}catch(Exception e){
				e.printStackTrace();
			}

//####ここのレイアウト
		    c1 = new Composite(instance.getShell(), SWT.NULL); // shellが親
		    GridData gridDataText = new GridData(GridData.FILL);
		    gridDataText.widthHint = 3000; // 横幅を最低300確保
		    c1.setLayoutData(gridDataText);
		    c1.setSize(800,536);
		     
//####コンボ		    
			cm1 = new Combo(c1,SWT.DROP_DOWN);
			cm2 = new Combo(c1,SWT.DROP_DOWN);
			cm3 = new Combo(c1,SWT.DROP_DOWN);
			//cm2.setItems(Miditem[1]);
		    makeComboBig();
		    makeComboMiddle();
		    makeComboSmall();
		    
//####リスト		    
		    
		    list1 = new List(c1,SWT.MULTI|SWT.BORDER|SWT.V_SCROLL);
		    list1.setBounds(300,0,100,100);
		    
//####適応
		    //コンボボックスで選んだ材料をリストに送る
		    Button accept = new Button(c1,SWT.PUSH);
		    accept.setBounds(100,0,100,30);
		    accept.setText("追加");
		    accept.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	  	
		    	  	if(cm1.getText() != ""){
		    	  		if(cm2.getText() != ""){
		    	  			if(cm3.getText() != "") list1.add(cm2.getText() + cm3.getText());
		    	  			else list1.add(cm2.getText());
		    	  			
		    	  		}
		    	  	}
		    	  	for(int i=0;i<list1.getItemCount();i++)
	    	  		System.out.println("###"+list1.getItems()[i]);
		    	  	
		    	  }
		    });
		    
		    //リストに表示されている材料を消す（選択されている材料）
		    Button accept2 = new Button(c1,SWT.PUSH);
		    accept2.setBounds(100,36,100,30);
		    accept2.setText("削除");
		    accept2.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	    list1.remove(list1.getSelectionIndices());
		    	  }
		    }); 
		    
		    
		    //リストに表示されている材料をサーバーに送る
		    Button accept3 = new Button(c1,SWT.PUSH);
		    accept3.setBounds(300,300,30,30);
		    accept3.setText("送信");
		    accept3.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	  	if(list1.getItemCount() != 0){
		    	  		String [] submit = list1.getItems();
		    	  		for(int cnt=0;cnt<submit.length;cnt++)
		    	  			System.out.println(submit[cnt]);
		    	  		visiWin();
		    	  	new SearchMateList().make_SearchMateList();
		    	  	}
		    	  }
		    });
		    c1.setVisible(true);
	}

		int a = 3,b = 4;

		public void makeComboBig(){
			
			
			//System.out.println(Miditem[0][0]);
		//	cm1 = new Combo(c1,SWT.DROP_DOWN);
			cm1.setBounds(0,0,100,30);
			cm1.setItems(Bigitem);
		    cm1.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	    
		    	  	System.out.println("1 - " + cm1.getText());
		    	  	a = cm1.getSelectionIndex();
		    	  	try{
		    	  		System.out.println(Miditem[1][0]);
		    	  	if(Miditem[a] != null)cm2.setItems(Miditem[a]);
		    	  		//if(Miditem[1][0] != null)cm2.setItems(Miditem[a]);
		    	  	}catch(Exception ee){
		    	  		//System.exit(0);
		    	  		ee.printStackTrace();
		    	  	}
		    	  	cm3.setItems(Smlitem[2]);
		    	  }
				  
		    });
			
		    
		}
		
		public void makeComboMiddle(){
			
		//	cm2 = new Combo(c1,SWT.DROP_DOWN);
			cm2.setBounds(0,20,100,30);
		//	cm2.setItems(Miditem[a]);
			cm2.addSelectionListener(new SelectionAdapter(){
				    public void widgetSelected(SelectionEvent e){
				    	  
				    	System.out.println("2 - " + cm2.getText());
				    	b = cm2.getSelectionIndex();
				    	//0:豚　1:牛 2:鳥 3:羊
				    	if(b == 1 && a == 1){
				    		cm3.setItems(Smlitem[1]);
				    	}else if(b == 0 && a == 1){
				    		cm3.setItems(Smlitem[0]);
				    	}else if(b == 2 && a == 1){
				    		cm3.setItems(Smlitem[2]);
				    	}else if(b == 3 && a == 1){
				    		cm3.setItems(Smlitem[3]);
				    	}
				    	else{
				    		cm3.setItems(Smlitem[4]);
				    	}
				    	  	
				    }
			});
			
		}
		
		public void makeComboSmall(){
			
			///cm3 = new Combo(c1,SWT.DROP_DOWN);
			cm3.setBounds(0,40,100,30);
	//		cm3.setItems(Smlitem[b]);
			cm3.addSelectionListener(new SelectionAdapter(){
				    public void widgetSelected(SelectionEvent e){
				    	if(a == 1 && b == 1){
				    		System.out.println("3 - " + cm3.getText());
				    	}else if(a == 1 && b == 0){
				    		System.out.println("3 - " + cm3.getText());
				    	}else if(a == 1 && b == 2){
				    		System.out.println("3 - " + cm3.getText());
				    	}
				    	else{
				    		e.doit = false;
				    		if(e.doit) System.out.println("3 - " + cm3.getText());
				    	}
				    	  	
				    }
			});
					
			
		}
		
		public void visiWin(){
			c1.setVisible(false);
		}
}
