import { actionAddins } from ".";
import { IAction, IActionResult, IActionNode, IActionProperty, IField ,IContext, IVarName} from "../types/ActionTypes";
/**
 * アクションの属性定義
 */
interface IDatetimeGetterProps {
    /**変数の名前 */
    verName:IVarName;
}
/**
 * 現在日時を取得するアクション
 */
export class DatetimeGetterAction implements IAction {
    name: string;
    actionProps: IActionProperty[];
    props: IDatetimeGetterProps;
    constructor() {
        this.name = "現在日時";
        this.actionProps = [];
        this.props = {
            verName:{name:''}
        }
        this.register();
    }
    /**
     * アクションの実行を呼び出す
     * @param actionNode 
     * @param event 
     * @returns 
     */
    async process(actionNode: IActionNode, event: any,context:IContext): Promise<IActionResult> {
        let result = {
            canNext: true,
            result: false
        };

        try {
            //属性設定を取得する
            this.actionProps = actionNode.actionProps;

            if (!('verName' in actionNode.ActionValue) ) {
                return result
            }
            this.props = actionNode.ActionValue as IDatetimeGetterProps;
            
            let today = new Date();
          
            if(this.props.verName && this.props.verName.name!==''){
                context.variables[this.props.verName.name]=today.toISOString();
            }

            return result;

        } catch (error) {
            context.errors.handleError(error,actionNode);
            result.canNext = false;
            return result;
        }
    };
    register(): void {
        actionAddins[this.name] = this;
    }

}
new DatetimeGetterAction();
