// control_blocks.js

/**
 * Control Blocks Category Definitions
 *
 * This file defines a set of control-related blocks for Blockly. Each block allows
 * users to manipulate and control the strategy's behavior, such as pausing, resuming,
 * exiting, setting various conditions, scheduling actions, and executing conditional statements.
 *
 * Ensure that this file is loaded **after** the `json_base_generator.js` and any other
 * necessary Blockly setup files to maintain proper loading order and functionality.
 */

export function defineControlBlocks() {
    /**
     * pause_strategy Block
     *
     * Description:
     * This block allows users to pause the current strategy. It can be placed
     * within a sequence of statements to temporarily halt the strategy's execution.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "pause_strategy",
        "message0": "Pause Strategy",
        "previousStatement": null,
        "nextStatement": null,
        "colour": 45,
        "tooltip": "Pause the strategy.",
        "helpUrl": ""
    }]);

    /**
     * strategy_resume Block
     *
     * Description:
     * This block enables users to resume the strategy if a certain condition is met.
     * It serves as a counterpart to the pause block, allowing the strategy to continue
     * its execution after being paused.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "strategy_resume",
        "message0": "Resume Strategy",
        "previousStatement": null,
        "nextStatement": null,
        "colour": 45,
        "tooltip": "Resume the strategy.",
        "helpUrl": ""
    }]);

    /**
     * strategy_exit Block
     *
     * Description:
     * This block allows users to exit the strategy and close trades based on specified conditions.
     * Users can choose to close all trades, only profitable trades, or only losing trades.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "strategy_exit",
        "message0": "Exit Strategy and close %1 trades.",
        "args0": [
            {
                "type": "field_dropdown",
                "name": "condition",
                "options": [
                    ['All', 'all'],
                    ['In Profit', 'in_profit'],
                    ['In Loss', 'in_loss']
                ]
            }
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 45,
        "tooltip": "Exit the strategy and close trades based on the condition.",
        "helpUrl": ""
    }]);

    /**
     * max_drawdown Block
     *
     * Description:
     * This block sets a maximum drawdown limit for the strategy. When the drawdown
     * exceeds the specified value, the strategy can either pause or exit based on user preference.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "max_drawdown",
        "message0": "Max Drawdown %1 Action: %2",
        "args0": [
            {
                "type": "input_value",
                "name": "DRAWDOWN_VALUE",
                "check": ["number", "dynamic_value"]
            },
            {
                "type": "field_dropdown",
                "name": "ACTION",
                "options": [
                    ["Pause", "PAUSE"],
                    ["Exit", "EXIT"]
                ]
            }
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 45,
        "tooltip": "Set a maximum drawdown limit to pause or exit the strategy.",
        "helpUrl": ""
    }]);

    /**
     * schedule_action Block
     *
     * Description:
     * This block schedules a specific action to be executed after a certain time interval.
     * Users can define what action to perform, after how long, and whether the action should repeat.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "schedule_action",
        "message0": "Schedule Action %1 After %2 Repeat %3",
        "args0": [
            {
                "type": "input_statement",
                "name": "DO",
                "check": null // Allows any block to be nested
            },
            {
                "type": "field_dropdown",
                "name": "TIME_INTERVAL",
                "options": [
                    ["1m", "1m"],
                    ["5m", "5m"],
                    ["15m", "15m"],
                    ["30m", "30m"],
                    ["1h", "1h"],
                    ["4h", "4h"],
                    ["6h", "6h"],
                    ["12h", "12h"],
                    ["1d", "1d"],
                    ["3d", "3d"],
                    ["1w", "1w"],
                    ["1M", "1M"]
                ]
            },
            {
                "type": "field_dropdown",
                "name": "REPEAT",
                "options": [
                    ["Once", "ONCE"],
                    ["Repeat", "REPEAT"]
                ]
            }
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 45,
        "tooltip": "Schedule a specific action to be executed after a certain time interval. Choose to execute once or repeat indefinitely.",
        "helpUrl": ""
    }]);

    /**
     * execute_if Block (New)
     *
     * Description:
     * This block executes a series of statements if a specified condition returns true.
     * It allows users to implement conditional logic within their strategy.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "execute_if",
        "message0": "If %1 then %2",
        "args0": [
            {
                "type": "input_value",
                "name": "CONDITION",
                "check": "Boolean"
            },
            {
                "type": "input_statement",
                "name": "DO",
                "check": null // Allows any blocks to be nested
            }
        ],
        "previousStatement": null,
        "nextStatement": null,
        "colour": 45,
        "tooltip": "Execute the enclosed statements if the condition is true.",
        "helpUrl": ""
    }]);
}
