import com.nttdocomo.ui.*;
import com.nttdocomo.io.*;

import javax.microedition.io.*;

import java.io.*;
import java.util.*;

public class Bunseki extends IApplication
				implements ComponentListener {
	
	Button btnCalc,btnCls,btnRe,btnRe2,btnNext,btnNext2,btnPre;
	TextBox [] txt;
	Panel pnlIn;
	int suti[] = new int[30];
	ListBox list_1,list_2;
	String hyojun[] = new String[25];
	int haba;
	int hikaku = 0; //0:比較しない 1:比較する
	int gyokai = 1;
	boolean tushin; //true 成功　false 失敗
	Font pFont=Font.getFont(Font.SIZE_SMALL);
	
	
	/*
	 0:現預金 1:受手形　2:売掛金　3:棚卸　4:他流動　5:有形固定　6:他資産　7:総資産
	 10:流動負債　11:短期借入　12:長期借入　13:資本金　
	 20:売上　21:総利益　22:営業利益　23:受取利息　24:支払利息　25:経常利益　26:従業員数
	*/
	
	public void start() {
		// TODO 自動生成されたメソッド・スタブ
		dataIn();
	}

	private void dataIn() {
		// TODO 自動生成されたメソッド・スタブ
		pnlIn = new Panel();
		btnCalc = new Button("計　算");
		btnCls = new Button("クリア");
		pnlIn.setTitle("簡易経営分析");

		Label [] lbl = new Label[30];
		String [] strLbl = new String[30];
		
		strLbl[0] = "現預金";
		strLbl[1] = "受取手形";
		strLbl[2] = "売掛金";
		strLbl[3] = "棚卸資産";
		strLbl[4] = "他流動資";
		strLbl[5] = "有固資産";
		strLbl[6] = "無固資産";
		strLbl[7] = "資産計";
		strLbl[8] = "";
		strLbl[9] = "";
		strLbl[10] = "支払手形";
		strLbl[11] = "買掛金";
		strLbl[12] = "短期借入";
		strLbl[13] = "他流負債";
		strLbl[14] = "長期借入";
		strLbl[15] = "他固負債";
		strLbl[16] = "資本金";
		strLbl[17] = "";
		strLbl[18] = "";
		strLbl[19] = "";
		strLbl[20] = "売上高";
		strLbl[21] = "総利益";
		strLbl[22] = "営業利益";
		strLbl[23] = "受取利息";
		strLbl[24] = "支払利息";
		strLbl[25] = "経常利益";
		strLbl[26] = "従業員数";
		strLbl[27] = "人件費計";
		strLbl[28] = "";
		strLbl[29] = "";
		
		Label lblDummy;
		txt = new TextBox[30];
		
		for (int i=0;i<30;i++){
			if (suti[i] !=0){
				txt[i] = new TextBox(Integer.toString((int)suti[i]),8,1,TextBox.NUMBER);
			} else {
				txt[i] = new TextBox("",8,1,TextBox.NUMBER);
			}
			
			txt[i].setInputMode(TextBox.NUMBER);
			
			//資産合計、資本金は編集不可
			if (i == 7 || i == 16){
				txt[i].setEditable(false);
				txt[i].setBackground(Graphics.getColorOfName(Graphics.AQUA));
			}
		}
		
		haba = pnlIn.getWidth()-txt[0].getWidth()-5;
		
		pnlIn.add(btnCls);
		lblDummy = new Label("",Label.RIGHT);
		lblDummy.setSize(haba*3/4,15);
		pnlIn.add(lblDummy);
		
		for (int i=0;i<30;i++){
			if (i == 8 || i == 9 || (i>=17 && i<=19) || i>=28){
				continue;
			}
			
			lbl[i] = new Label(strLbl[i],Label.CENTER);
			lbl[i].setSize(haba-20,20);
			if (i <10){
			  lbl[i].setBackground(Graphics.getColorOfName(Graphics.LIME));
			} else if (i<20){
				  lbl[i].setBackground(Graphics.getColorOfName(Graphics.SILVER));			
			} else {
				  lbl[i].setBackground(Graphics.getColorOfName(Graphics.YELLOW));			
			}
			
			lbl[i].setFont(pFont);
			txt[i].setFont(pFont);
			pnlIn.add(lbl[i]);
			pnlIn.add(txt[i]);
		}
		
		pnlIn.add(btnCalc);
		pnlIn.setComponentListener(this);
		
		Display.setCurrent(pnlIn);
	}
	
	private void gyokaiP() {
		// TODO 自動生成されたメソッド・スタブ
		pnlIn = new Panel();
		btnNext = new Button("計　算");
		btnNext2 = new Button("計　算");
		btnPre = new Button("戻　る");
		pnlIn.setTitle("業界指定");
		
		list_1 = new ListBox(ListBox.RADIO_BUTTON);  //比較の有無
		list_2 = new ListBox(ListBox.RADIO_BUTTON);	 //業界	
		
		list_1.append("業界値なし");
		list_1.append("業界値あり");
		list_1.select(hikaku);
		
		list_2.append("1:建設業");
		list_2.append("2:製造業");
		list_2.append("3:情報通信業");
		list_2.append("4:運輸業");
		list_2.append("5:卸売業");
		list_2.append("6:小売業");
		list_2.append("7:不動産業");
		list_2.append("8:飲食宿泊業");
		list_2.append("9:サービス業");
		list_2.setSize(pnlIn.getWidth(),155);
		list_2.select(gyokai-1);
		
		if (hikaku == 0){
			list_2.setVisible(false);
			btnNext2.setVisible(false);
			btnNext.setVisible(true);
		} else {
			list_2.setVisible(true);
			btnNext2.setVisible(true);
			btnNext.setVisible(false);			
	    }
			
		pnlIn.add(btnPre);
		pnlIn.add(list_1);
		pnlIn.add(btnNext);
		pnlIn.add(list_2);
		pnlIn.add(btnNext2);
		
		pnlIn.setComponentListener(this);
		
		Display.setCurrent(pnlIn);
	}	
	
	private void dataOut() {
		// TODO 自動生成されたメソッド・スタブ
		pnlIn = new Panel();
		btnRe = new Button("戻る");
		btnRe2 = new Button("戻る");
		pnlIn.setTitle("分析数値");
		
		Label [] lbl = new Label[25];
		Label [] hyoLbl = new Label[25];//業界標準用
		String [] strLbl = new String[25];
		txt = new TextBox[25];
		long [] lngSu = new long[25];

		/*
		 0:総利/売上 1:営利/売上　2:経利/売上　3:営利/総資　4:経利/総資　5:総資本回　6:売債回転　7:棚卸回転　8:固資回転
		 11:自己資本比　12:流動比率　13:当座比率 14:固定比率　15:固長適合　16:手元現預　17:借入依存　18:支利息/売上　
		 21:売上/人　22:経利益/人　23:固定資/人
		*/
		strLbl[0] = "総利/売上";
		strLbl[1] = "営利/売上";
		strLbl[2] = "経利/売上";
		strLbl[3] = "営利/総資";
		strLbl[4] = "経利/総資";
		strLbl[5] = "総資本回転";
		strLbl[6] = "固資産回転";
		strLbl[7] = "売債回転";
		strLbl[8] = "棚卸回転";
		strLbl[9] = "";
		strLbl[10] = "";
		strLbl[11] = "自己資本率";
		strLbl[12] = "流動比率";
		strLbl[13] = "当座比率";
		strLbl[14] = "固定比率";
		strLbl[15] = "固長適合";
		strLbl[16] = "手元現預率";
		strLbl[17] = "借入依存率";
		strLbl[18] = "支利息/売上";
		strLbl[19] = "";
		strLbl[20] = "";
		strLbl[21] = "売上/人";
		strLbl[22] = "経利益/人";
		strLbl[23] = "人件費/人";
		strLbl[24] = "労働分配率";
		
		//計算処理
		for (int i=0;i<25;i++){
			lngSu[i]=0;
		}
		
		try {
			if (suti[20] != 0){
				lngSu[0] = (suti[21]*10000/suti[20]+5)/10; //売上高総利益率
				lngSu[1] = (suti[22]*10000/suti[20]+5)/10; //売上高営業利益率
				lngSu[2] = (suti[25]*10000/suti[20]+5)/10; //売上高経常利益率

				lngSu[16] = (suti[0]*10000/suti[20]+5)/10; //手元現金預金比率			
				lngSu[18] = (suti[24]*10000/suti[20]+5)/10; //売上高支払利息割引料率
			}
			if (suti[7] != 0){
				lngSu[3] = (suti[22]*10000/suti[7]+5)/10; //総資本営業利益率
				lngSu[4] = (suti[25]*10000/suti[7]+5)/10; //総資本経常利益率
				lngSu[5] = (suti[20]*100/suti[7]+5)/10; //総資本回転率(回)

				lngSu[11] = ((suti[16]*10000)/suti[7]+5)/10; //自己資本比率	
			
				lngSu[17] = ((suti[12]+suti[14])*10000/suti[7]+5)/10; //借入金依存度
			}
			if ((suti[5]+suti[6]) !=0){
				lngSu[6] = (suti[20]*100/(suti[5]+suti[6])+5)/10; //固定資産回転率(回)
			}

			if ((suti[20] !=0) && ((suti[1]+suti[2]) != 0)){
				lngSu[7] = (((suti[1]+suti[2])*365*100)/suti[20]+5)/10; //売上債権回転日数(日)				
			}

			if ((suti[20] !=0) && ((suti[3]) != 0)){
				lngSu[8] = ((suti[3]*365*100)/suti[20]+5)/10; //棚卸資産回転日数(日)
			}			
			
			if ((suti[10]+suti[11]+suti[12]+suti[13]) != 0){
				lngSu[12] = (((suti[0]+suti[1]+suti[2]+suti[3]+suti[4])*10000)/(suti[10]+suti[11]+suti[12]+suti[13])+5)/10; //流動比率
				lngSu[13] = (((suti[0]+suti[1]+suti[2])*10000)/(suti[10]+suti[11]+suti[12]+suti[13])+5)/10; //当座比率
			}
			
			if (suti[16] != 0){
				lngSu[14] = ((suti[5]+suti[6])*10000/suti[16]+5)/10; //固定比率
			}
			if ((suti[14]+suti[15]+suti[16]) != 0){
				lngSu[15] = ((suti[5]+suti[6])*10000/(suti[14]+suti[15]+suti[16])+5)/10; //固定長期適合率
			}
			
			if (suti[26] != 0){
				lngSu[21] = (suti[20]/suti[26]+5)/10; //一人当たり売上高
				lngSu[22] = (suti[25]/suti[26]+5)/10; //一人当たり経常利益
				lngSu[23] = (suti[27]/suti[26]+5)/10; //一人当たり人件費
			}
			if (suti[21] != 0){
				lngSu[24] = (suti[27]*10000/suti[21]+5)/10; //労働分配率
			}
		} catch (Exception e){
		}
		
		TextBox dummy = new TextBox("",4,1,TextBox.DISPLAY_ANY);
		dummy.setEditable(false);
		dummy.setText("結果");
		pnlIn.add(dummy);
		
		pnlIn.add(btnRe);
		Label lblDummy;
		Ticker lblDum;
		if (tushin == false && hikaku == 1){
			//標準データの取得に失敗したとき
			lblDum = new Ticker("業界ﾃﾞｰﾀ取得失敗");
			lblDum.setForeground(Graphics.getColorOfName(Graphics.RED));
			hikaku = 0;		
			lblDum.setSize(haba-10,15);
			pnlIn.add(lblDum);
		} else {
			lblDummy = new Label("",Label.RIGHT);	
			lblDummy.setSize(haba-10,15);
			pnlIn.add(lblDummy);
		}


		
		//パネルに追加
		for (int i=0;i<25;i++){
			if (i == 9 || i == 10 || (i>=19 && i<=20)){
				continue;
			}
			
			if (lngSu[i] !=0){
				if (i == 5 || i == 6){
					txt[i] = new TextBox(syoTen(Long.toString(lngSu[i]),2),8,1,TextBox.DISPLAY_ANY);
				} else if (i >= 7 && i <= 8){
					txt[i] = new TextBox(syoTen(Long.toString(lngSu[i]),3),8,1,TextBox.DISPLAY_ANY);
				} else if (i >= 21 && i<=23){
					txt[i] = new TextBox(inComma(Long.toString(lngSu[i])),8,1,TextBox.DISPLAY_ANY);
				} else {
					txt[i] = new TextBox(syoTen(Long.toString(lngSu[i]),1),8,1,TextBox.DISPLAY_ANY);
				}
		
			} else {
				txt[i] = new TextBox("---",8,1,TextBox.NUMBER);
			}
			
			txt[i].setEditable(false);			
			lbl[i] = new Label(strLbl[i],Label.CENTER);				
			lbl[i].setSize(haba-20,20);
			
			lbl[i].setFont(pFont);
			txt[i].setFont(pFont);			
			pnlIn.add(lbl[i]);
			pnlIn.add(txt[i]);
			
			if (hikaku == 1){
				hyoLbl[i] = new Label(hyojun[i],Label.RIGHT);				
				hyoLbl[i].setSize(pnlIn.getWidth()-10,15);
				pnlIn.add(hyoLbl[i]);
			}
			
		}
		
		pnlIn.add(btnRe2);
		pnlIn.setComponentListener(this);
		
		Display.setCurrent(pnlIn);
	}
	
	public void componentAction(Component c, int act, int prm) {
		// TODO 自動生成されたメソッド・スタブ
		if (c == btnCalc){
			for (int i=0;i<30;i++){
				suti[i]=0;
				try {
					suti[i] = Integer.parseInt(txt[i].getText());
				} catch (Exception e){
				}
			}
			gyokaiP();
		} else if (c == btnCls){
			for (int i=0;i<30;i++){
				suti[i]=0;
			}
			dataIn();
		} else if (c == btnRe || c == btnRe2){
			gyokaiP();
		
		} else if (c == btnNext || c == btnNext2){
			if (hikaku == 1){
				//業界標準を取得
				httpTushin(gyokai);
			}
			
			dataOut();
		
		} else if (c == btnPre){
			dataIn();
		
			//資産合計
		} else if (c == txt[0] || c == txt[1] || c == txt[2] || c == txt[3]
		          || c == txt[4] || c == txt[5] || c == txt[6]){
			if (act == ComponentListener.TEXT_CHANGED){
				int intTmp=0;
				for (int i=0;i<7;i++){
					try {
						intTmp += Integer.parseInt(txt[i].getText());
					} catch (Exception e){
					}
				}
				
				txt[7].setText(Integer.toString(intTmp));
				
				for (int i=10;i<16;i++){
					try {
						intTmp -= Integer.parseInt(txt[i].getText());
					} catch (Exception e){
					}
				}			
				txt[16].setText(Integer.toString(intTmp));
				
			}
			//資本金
		} else if (c == txt[10] || c == txt[11] || c == txt[12] || 
				c == txt[13] || c == txt[14] || c == txt[15]){
			if (act == ComponentListener.TEXT_CHANGED){
				int intTmp = 0;
				
				try {
					intTmp=Integer.parseInt(txt[7].getText());
				} catch (Exception e){
				}
				
				for (int i=10;i<16;i++){
					try {
						intTmp -= Integer.parseInt(txt[i].getText());
					} catch (Exception e){
					}
				}
			
				txt[16].setText(Integer.toString(intTmp));
			}
			
		} else if (c == list_1){
			if (act == ComponentListener.SELECTION_CHANGED){
				hikaku = list_1.getSelectedIndex();
				if (hikaku == 0){
					list_2.setVisible(false);
					btnNext2.setVisible(false);
					btnNext.setVisible(true);
				} else {
					list_2.setVisible(true);
					btnNext2.setVisible(true);
					btnNext.setVisible(false);				}
			}
		} else if (c == list_2){
			if (act == ComponentListener.SELECTION_CHANGED){
				gyokai = list_2.getSelectedIndex()+1;
			}			
		}
	}
	
	//3桁カンマ
	String inComma(String num) {
		
		StringBuffer sb = new StringBuffer (num);
		int len = num.length ();
		int comma_num = (int)((len - 1) / 3);  // カンマの個数
		int comma_pos = (int)(len % 3);        // カンマの最左端位置
		if (comma_pos == 0) {
		  comma_pos = 3;
		}
		if (comma_num == 0) {
		  comma_pos = 0;
		}
		for (int i=0; i < comma_num; i++) {
		  sb.insert (comma_pos + 3*i + i, ",");
		}
		String out = sb.toString ();
		return out;
	}

	//小数点1桁
	String syoTen(String num,int syu) {
		//syu 1:% 2:回 3:日
		String out;
		int len = num.length ();
		
		if (num.equals("0") == true){
			return num;
		} else if (len == 1){
			num = "0" + num;
		}
		
		StringBuffer sb = new StringBuffer (num);
		
		len = num.length ();
		sb.insert(len-1,".");
		out = sb.toString();
				
		switch (syu){
			case 1:
				out = out + "%";
				break;
			case 2:
				out = out + "回";
				break;
			case 3:
				out = out + "日";
				break;
		}

		return out;
	}
	
	private void httpTushin(int gyosyu){
		try {
			
			//String url = "http://localhost:66/hyojun" + gyosyu + ".csv";
			String url = "http://jiroh.com:80/iap/hyojun" + gyosyu + ".csv";
			
			//System.out.println(url);
			HttpConnection http = (HttpConnection)Connector.open(
					url,Connector.READ,true);
		
			http.setRequestMethod(HttpConnection.GET);
			http.connect();
			InputStreamReader in = 
				new InputStreamReader(http.openInputStream());
			
			StringBuffer dat = new StringBuffer();
			//標準データの格納
			if (in != null){
				char word[] = new char[1];
				while (in.read(word) != -1){
					dat.append(word);
				}
			}
			
			try {
				http.close();
			} catch (Exception e){
			}
			
			//標準データを配列へ
			for (int i=0;i<25;i++){
				hyojun[i]="";
			}
			expStr(dat, hyojun);
			
			tushin = true;
		} catch (IOException e) {
			tushin = false;
		}		
	}
	
	//標準データを配列へ格納
	private void expStr(StringBuffer strB, String[] str) {
		String strDat = strB.toString();
		int j = 0;
		int flg = 0;
		for (int i=0;i<strDat.length();i++){
			
			String n = strDat.substring(i,i+1);
			
			if (n.equals("\n") == true || n.equals("\r") == true || n.equals("\r\n") == true){
				flg++;	
			} else {
				if (flg !=0){
					flg = 0;
					j++;
					str[j]= str[j] + n;
				} else {
					str[j]= str[j] + n;
				}
			}				
		}
	}
}
