package org.postgresforest.tool.commandline.action;

import java.util.ArrayList;
import java.util.Iterator;

import org.postgresforest.tool.action.CheckAction;
import org.postgresforest.tool.action.GscAction;
import org.postgresforest.tool.bean.GSCInfo;
import org.postgresforest.tool.bean.LoginInfo;
import org.postgresforest.tool.db.GSCdata;
import org.postgresforest.tool.util.CommandParser;
import org.postgresforest.tool.util.MessagesCommandLine;

/**
 * GSCへのコマンドラインツール用のアクションを提供するクラス
 */
public class GscActions {
	
	/**
	 * GSCの新規作成
	 * @param loginInfo ログイン情報
	 */
	public void make(LoginInfo loginInfo) {
		GscAction gscAction = new GscAction(null);
		String gscName = loginInfo.getDbname();
		try {
			if( gscAction.makeGsc(loginInfo)) {
			    System.out.println(MessagesCommandLine.getString("cui.message.gsc.make", new Object[] {gscName})); //$NON-NLS-1$
			    return;
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}		
	    System.out.println(MessagesCommandLine.getString("cui.error.gsc.make", new Object[] {gscName})); //$NON-NLS-1$
	}

	/**
	 * GSCの追加作成
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 */
	public void create(CommandParser cp, GSCdata gsc) {
		String gscName = (String)cp.getGsc().get(0);
		String serverid = (String)cp.getServer().get(0);		
		//グローバルシステムカタログ作成
		if(gscName != "" && serverid != ""){ //$NON-NLS-1$ //$NON-NLS-2$
			GscAction addGsc = new GscAction(gsc);
			if(	addGsc.addGsc(gscName, serverid) ){
				System.out.println(MessagesCommandLine.getString("cui.message.gsc.create", new Object[] {gscName, serverid})) ; //$NON-NLS-1$
				return;
			} 
		}
		System.out.println(MessagesCommandLine.getString("cui.error.gsc.create", new Object[] {gscName, serverid}) ); //$NON-NLS-1$
		
	}
	
	/**
	 * GSCの削除
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 */
	public void drop(CommandParser cp, GSCdata gsc) {
		//最後のGSCの削除の場合、確認メッセージを表示する
/*		if(view.isLastGsc()){
			Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
			if(!MessageDialog.openQuestion(shell, 
					Messages.getString("button.delete"), 
					Messages.getString("message.delgsc.confirm"))){
				return ;
			}
		}*/
		
		String gscName = (String)cp.getGsc().get(0);
		String serverid = (String)cp.getServer().get(0);		
		//グローバルシステムカタログ削除
		if(gscName != "" && serverid != ""){ //$NON-NLS-1$ //$NON-NLS-2$
			GscAction delGsc = new GscAction(gsc);
			if(	delGsc.delGsc(gscName, serverid) ){
				System.out.println(MessagesCommandLine.getString("cui.message.gsc.drop", new Object[] {gscName, serverid}) ); //$NON-NLS-1$
				return;
			}
		}		
		System.out.println(MessagesCommandLine.getString("cui.error.gsc.drop", new Object[] {gscName, serverid}) ); //$NON-NLS-1$
	}
	
	/**
	 * GSCの整合性チェック
	 * @param loginInfo ログイン情報
	 */
	public void check(LoginInfo loginInfo) {
		//グローバルシステムカタログ整合性チェック処理
		try {
			CheckAction chkGsc = new CheckAction();
			if (chkGsc.execute(loginInfo)) {
				System.out.println(MessagesCommandLine.getString("cui.message.gsc.check")) ;		 //$NON-NLS-1$
				return;
			}
        } catch (Exception e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }
		System.out.println(MessagesCommandLine.getString("cui.error.gsc.check") ); //$NON-NLS-1$
	}
	
	/**
	 * GSCの表示
	 * @param gsc GSCヘの接続情報
	 */
	public void show(GSCdata gsc) {
		//GSC情報取得
		ArrayList gscList = null;
		GscAction gscAct = new GscAction(gsc);
		try {
			gscList = gscAct.getGscList();
		} catch (Exception e) {
			System.out.println(MessagesCommandLine.getString("cui.error.gsc.show")) ; //$NON-NLS-1$
			System.err.println(e);
			return;
		}

		
		//GSC情報表示
		System.out.println(MessagesCommandLine.getString("cui.message.gsc.show")) ; //$NON-NLS-1$
		Iterator it = gscList.iterator();
		System.out.println(" DBNAME \t| HOST \t| PORT \t| SERVERID"); //$NON-NLS-1$
		System.out.println("----------------------------------------------"); //$NON-NLS-1$
		while (it.hasNext()) {
			GSCInfo gscInfo = (GSCInfo)it.next();
			System.out.print(gscInfo.getDb() + "\t| "); //$NON-NLS-1$
			System.out.print(gscInfo.getId() + "\t| "); //$NON-NLS-1$
			System.out.print(gscInfo.getHost() + "\t| "); //$NON-NLS-1$
			System.out.println(gscInfo.getPort());
		}
	}
}
