/**
 * TIME JUMP CONTROLLER - ISOLATED SYSTEM
 * 
 * This is a completely separate system that does NOT interfere with existing algorithms.
 * It works by filtering JSON data BEFORE it reaches the main chart algorithm.
 * 
 * Core Principle: Change the input, not the engine.
 */

class TimeJumpController {
    constructor() {
        this.originalData = null;
        this.filteredData = null;
        this.isActive = false;
        this.jumpTime = null;
        this.dataTimeRange = { start: null, end: null };
        this.currentUsername = null;
        
        this.initializeElements();
        this.bindEvents();
        this.validateTimeFormat = this.validateTimeFormat.bind(this);
        this.parseTimeToMinutes = this.parseTimeToMinutes.bind(this);
        this.formatMinutesToTime = this.formatMinutesToTime.bind(this);
        this.initializeUserSession();
    }

    /**
     * Initialize user session
     */
    async initializeUserSession() {
        try {
            const response = await fetch('./php/session_manager.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ action: 'getCurrentUser' })
            });
            const sessionData = await response.json();
            this.currentUsername = sessionData.username;
        } catch (error) {
        }
    }

    /**
     * Initialize DOM elements
     */
    initializeElements() {
        this.elements = {
            input: document.getElementById('time-jump-input'),
            button: document.getElementById('time-jump-button'),
            status: document.getElementById('time-jump-status'),
            rangeDisplay: document.getElementById('time-range-display')
        };

        // Validate all elements exist
        Object.keys(this.elements).forEach(key => {
            if (!this.elements[key]) {
            }
        });
    }

    /**
     * Bind event listeners
     */
    bindEvents() {
        if (!this.elements.input || !this.elements.button) return;

        // Input validation and formatting
        this.elements.input.addEventListener('input', (e) => {
            this.handleTimeInput(e.target.value);
        });

        // Jump button click
        this.elements.button.addEventListener('click', () => {
            this.executeTimeJump();
        });

        // Enter key support
        this.elements.input.addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && !this.elements.button.disabled) {
                this.executeTimeJump();
            }
        });
    }

    /**
     * Handle time input formatting and validation
     */
    handleTimeInput(value) {
        // Auto-format time input (add colon)
        let formatted = value.replace(/[^\d]/g, '');
        if (formatted.length >= 3) {
            formatted = formatted.substring(0, 2) + ':' + formatted.substring(2, 4);
        }
        
        if (formatted !== value) {
            this.elements.input.value = formatted;
        }

        this.validateTimeInput(formatted);
    }

    /**
     * Validate time input and update UI state
     */
    validateTimeInput(timeStr) {
        const isValid = this.validateTimeFormat(timeStr);
        const isInRange = isValid && this.isTimeInDataRange(timeStr);
        
        // Update input styling
        this.elements.input.classList.remove('error', 'success');
        if (timeStr.length > 0) {
            this.elements.input.classList.add(isValid && isInRange ? 'success' : 'error');
        }

        // Update button state
        this.elements.button.disabled = !(isValid && isInRange);

        // Update status
        this.updateStatus(timeStr, isValid, isInRange);
    }

    /**
     * Update status message
     */
    updateStatus(timeStr, isValid, isInRange) {
        if (!this.elements.status) return;

        this.elements.status.className = 'time-jump-status';
        
        if (timeStr.length === 0) {
            this.elements.status.style.display = 'none';
            return;
        }

        if (!isValid) {
            this.elements.status.className += ' error';
            this.elements.status.textContent = 'Invalid time format. Use HH:MM (24-hour format)';
            this.elements.status.style.display = 'block';
        } else if (!isInRange) {
            this.elements.status.className += ' warning';
            this.elements.status.textContent = 'Time is outside available data range';
            this.elements.status.style.display = 'block';
        } else {
            this.elements.status.className += ' success';
            this.elements.status.textContent = 'Ready to jump to ' + timeStr;
            this.elements.status.style.display = 'block';
        }
    }

    /**
     * Validate time format (HH:MM)
     */
    validateTimeFormat(timeStr) {
        const timeRegex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/;
        return timeRegex.test(timeStr);
    }

    /**
     * Convert time string to minutes since midnight
     */
    parseTimeToMinutes(timeStr) {
        const [hours, minutes] = timeStr.split(':').map(Number);
        return hours * 60 + minutes;
    }

    /**
     * Convert minutes since midnight to time string
     */
    formatMinutesToTime(minutes) {
        const hours = Math.floor(minutes / 60);
        const mins = minutes % 60;
        return `${hours.toString().padStart(2, '0')}:${mins.toString().padStart(2, '0')}`;
    }

    /**
     * Check if time is within data range
     */
    isTimeInDataRange(timeStr) {
        if (!this.dataTimeRange.start || !this.dataTimeRange.end) return false;
        
        const targetMinutes = this.parseTimeToMinutes(timeStr);
        const startMinutes = this.parseTimeToMinutes(this.dataTimeRange.start);
        const endMinutes = this.parseTimeToMinutes(this.dataTimeRange.end);
        
        // Handle midnight crossing (e.g., 21:58 to 00:07)
        if (endMinutes < startMinutes) {
            // Range crosses midnight: accept times >= start OR <= end
            return targetMinutes >= startMinutes || targetMinutes <= endMinutes;
        }
        
        // Normal range (same day)
        return targetMinutes >= startMinutes && targetMinutes <= endMinutes;
    }
    
    /**
     * Convert HH:MM time back to timestamp for filtering
     */
    timeToTimestamp(timeStr) {
        if (!this.originalData || this.originalData.length === 0) return null;
        
        const targetMinutes = this.parseTimeToMinutes(timeStr);
        const firstTimestamp = this.originalData[0].time;
        
        return firstTimestamp + (targetMinutes * 60);
    }

    /**
     * Set original data and analyze time range
     * This is called when new JSON data is loaded
     */
    setOriginalData(jsonData, datasetPath = null) {
        try {
            this.originalData = jsonData;
            
            // Store dataset path if provided
            if (datasetPath && window.chart) {
                window.chart.currentDatasetPath = datasetPath;
            }
            
            this.analyzeDataTimeRange();
            this.updateRangeDisplay();
            this.resetController();
            
        } catch (error) {
            this.showError('Failed to analyze data time range');
        }
    }

    /**
     * Analyze time range from JSON data
     */
    analyzeDataTimeRange() {
        if (!this.originalData || this.originalData.length === 0) {
            this.dataTimeRange = { start: null, end: null };
            return;
        }

        // Extract time from first and last candle
        const firstCandle = this.originalData[0];
        const lastCandle = this.originalData[this.originalData.length - 1];
        
        // Assume time is in 'time' field or 'timestamp' field
        const startTime = this.extractTimeFromCandle(firstCandle);
        const endTime = this.extractTimeFromCandle(lastCandle);
        
        this.dataTimeRange = {
            start: startTime,
            end: endTime
        };
    }

    /**
     * Extract time string from candle data
     */
    extractTimeFromCandle(candle) {
        // Try different possible time field names
        const timeFields = ['time', 'timestamp', 'datetime', 't'];
        
        for (const field of timeFields) {
            if (candle[field]) {
                return this.parseTimeFromValue(candle[field]);
            }
        }
        

        return null;
    }

    /**
     * Parse time from various formats
     */
    parseTimeFromValue(timeValue) {
        if (typeof timeValue === 'string') {
            // If it's already in HH:MM format
            if (/^\d{1,2}:\d{2}$/.test(timeValue)) {
                return timeValue;
            }
            
            // If it's a full datetime string, extract time part
            const timeMatch = timeValue.match(/(\d{1,2}):(\d{2})/);
            if (timeMatch) {
                return `${timeMatch[1].padStart(2, '0')}:${timeMatch[2]}`;
            }
        }
        
        // For numeric timestamps, convert to actual time of day
        if (typeof timeValue === 'number') {
            const date = new Date(timeValue * 1000);
            const hours = date.getHours();
            const minutes = date.getMinutes();
            return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
        }
        
        return null;
    }

    /**
     * Update range display
     */
    updateRangeDisplay() {
        if (!this.elements.rangeDisplay) return;
        
        if (this.dataTimeRange && this.dataTimeRange.start && this.dataTimeRange.end) {
            this.elements.rangeDisplay.textContent = `Available: ${this.dataTimeRange.start} to ${this.dataTimeRange.end}`;
        } else {
            this.elements.rangeDisplay.textContent = 'No data loaded';
        }
    }

    /**
     * Execute time jump
     */
    async executeTimeJump() {
        const timeStr = this.elements.input.value;
        
        if (!this.validateTimeFormat(timeStr)) {
            this.showError('Invalid time format. Use HH:MM');
            return;
        }





        
        // Use direct chart approach (your system uses single chart)
        if (window.chart && window.chart.candleData && window.chart.candleData.length > 0) {
            try {
                const success = await this.executeDirectTimeJump(timeStr);
                
                if (success) {
                    this.jumpTime = timeStr;
                    this.isActive = true;
                    this.showSuccess(`Jumped to ${timeStr}. Chart will build from this time.`);
                } else {
                    this.showError('Time jump failed. Check if time is within data range.');
                }
                
            } catch (error) {
                this.showError('Failed to execute time jump');
            }
        } else {
            this.showError('Chart data not available');
        }
    }

    /**
     * Filter data from specified time
     */
    filterDataFromTime(timeStr) {
        if (!this.originalData) {
            throw new Error('No original data available');
        }

        const targetTimestamp = this.timeToTimestamp(timeStr);
        if (!targetTimestamp) {
            throw new Error('Invalid time conversion');
        }
        
        this.filteredData = this.originalData.filter(candle => {
            return candle.time >= targetTimestamp;
        });

        if (this.filteredData.length === 0) {
            throw new Error('No data found from specified time');
        }
    }

    /**
     * Get data for chart (filtered or original)
     */
    getDataForChart() {
        return this.isActive && this.filteredData ? this.filteredData : this.originalData;
    }

    /**
     * Reset controller to use original data
     */
    resetController() {
        this.isActive = false;
        this.jumpTime = null;
        this.filteredData = null;
        this.elements.input.value = '';
        this.elements.input.classList.remove('error', 'success');
        this.elements.button.disabled = true;
        this.elements.status.style.display = 'none';
    }

    /**
     * Trigger chart rebuild with current data
     */
    triggerChartRebuild() {
        // This method should be called by the main chart system
        // to rebuild the chart with the filtered data
        
        // If chart exists, load the filtered data directly
        if (window.chart && this.filteredData) {
    
            window.chart.loadDataset(this.filteredData, []);
        }
        
        // Dispatch custom event that main chart system can listen to
        const event = new CustomEvent('timeJumpExecuted', {
            detail: {
                jumpTime: this.jumpTime,
                filteredData: this.filteredData,
                isActive: this.isActive
            }
        });
        
        document.dispatchEvent(event);
    }

    /**
     * Show success message
     */
    showSuccess(message) {
        if (!this.elements.status) return;
        
        this.elements.status.className = 'time-jump-status success';
        this.elements.status.textContent = message;
        this.elements.status.style.display = 'block';
    }

    /**
     * Show error message
     */
    showError(message) {
        if (!this.elements.status) return;
        
        this.elements.status.className = 'time-jump-status error';
        this.elements.status.textContent = message;
        this.elements.status.style.display = 'block';
    }

    /**
     * Check if controller is active
     */
    isControllerActive() {
        return this.isActive;
    }

    /**
     * Get current jump time
     */
    getCurrentJumpTime() {
        return this.jumpTime;
    }

    /**
     * Get active dataset path from chart instance
     */
    getActiveDatasetPath(activeAssetName) {
        // Method 1: Get from active chart instance (most reliable)
        if (window.chart && window.chart.currentDatasetPath) {
            return window.chart.currentDatasetPath;
        }
        
        // Method 2: Get from multi-chart manager
        if (window.multiChartManager && window.multiChartManager.getChart && window.multiChartManager.getChart(activeAssetName)) {
            const chart = window.multiChartManager.getChart(activeAssetName);
            if (chart && chart.currentDatasetPath) {
                return chart.currentDatasetPath;
            }
        }
        
        return null;
    }

    /**
     * Detect dataset path from active chart data (emergency fallback)
     */
    detectDatasetFromChart(activeAssetName) {
        const chart = window.chart || (window.multiChartManager && window.multiChartManager.getChart(activeAssetName));
        if (!chart || !chart.candleData || chart.candleData.length === 0) return null;
        
        // Try to reconstruct path from chart data timestamps
        const firstCandle = chart.candleData[0];
        if (firstCandle && firstCandle.time && this.currentUsername) {
            const simplifiedAssetName = activeAssetName.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
            return `./data/${this.currentUsername}/${simplifiedAssetName}/dataset_*/candle_data.json`;
        }
        
        return null;
    }

    /**
     * Update time range from active asset's full dataset
     */
    async updateTimeRange() {
        try {
            const activeAssetName = document.querySelector('.asset-name')?.textContent;
            if (!activeAssetName) return;
            
            // Method 1: Try to get dataset path and fetch full data
            let datasetPath = this.getActiveDatasetPath(activeAssetName);
            if (datasetPath) {
                const response = await fetch(datasetPath);
                const fullData = await response.json();
                
                if (fullData && fullData.length > 0) {
                    this.updateTimeRangeFromData(fullData);
                    return;
                }
            }
            
            // Method 2: Extract time range directly from active chart data
            const chart = window.chart || (window.multiChartManager && window.multiChartManager.getChart(activeAssetName));
            if (chart && chart.candleData && chart.candleData.length > 0) {
                
                // Get raw data from chart if available
                if (chart.rawData && chart.rawData.length > 0) {
                    this.updateTimeRangeFromData(chart.rawData);
                    return;
                }
                
                // Fallback: use processed candle data
                const firstCandle = chart.candleData[0];
                const lastCandle = chart.candleData[chart.candleData.length - 1];
                
                if (firstCandle.timestamp && lastCandle.timestamp) {
                    const timeRange = {
                        start: new Date(firstCandle.timestamp).toLocaleTimeString([], {hour: '2-digit', minute: '2-digit', hour12: false}),
                        end: new Date(lastCandle.timestamp).toLocaleTimeString([], {hour: '2-digit', minute: '2-digit', hour12: false})
                    };
                    
                    this.dataTimeRange = timeRange;
                    this.updateRangeDisplay();
                    return;
                }
            }
            
            // Method 3: Try auto-detection as last resort
            datasetPath = await this.autoDetectAssetPath(activeAssetName);
            if (datasetPath) {
                const response = await fetch(datasetPath);
                const fullData = await response.json();
                
                if (fullData && fullData.length > 0) {
                    this.updateTimeRangeFromData(fullData);
                    return;
                }
            }
            
        } catch (error) {
        }
    }
    
    /**
     * Update time range from data array
     */
    updateTimeRangeFromData(data) {
        const firstCandle = data[0];
        const lastCandle = data[data.length - 1];
        
        const timeRange = {
            start: this.parseTimeFromValue(firstCandle.time),
            end: this.parseTimeFromValue(lastCandle.time)
        };
        
        // Only update if range actually changed
        const currentRange = this.dataTimeRange;
        if (!currentRange || currentRange.start !== timeRange.start || currentRange.end !== timeRange.end) {
            this.dataTimeRange = timeRange;
            this.updateRangeDisplay();
        }
    }



    /**
     * Convert HH:MM to Unix timestamp within dataset's time range
     */
    convertTimeToTimestamp(timeStr, dataArray) {
        const [inputHours, inputMinutes] = timeStr.split(':').map(Number);
        
        // Get data's actual date from first timestamp
        const firstTimestamp = dataArray[0].time;
        const dataDate = new Date(firstTimestamp * 1000);
        
        // Create target timestamp for same date
        const targetDate = new Date(dataDate.getFullYear(), dataDate.getMonth(), dataDate.getDate(), inputHours, inputMinutes, 0, 0);
        
        return Math.floor(targetDate.getTime() / 1000);
    }

    /**
     * Find closest candle to target timestamp
     */
    findClosestCandle(targetTimestamp, dataArray) {
        // Binary search for efficiency with large datasets
        let left = 0;
        let right = dataArray.length - 1;
        let closest = dataArray[0];
        let minDiff = Math.abs(dataArray[0].time - targetTimestamp);
        
        while (left <= right) {
            const mid = Math.floor((left + right) / 2);
            const diff = Math.abs(dataArray[mid].time - targetTimestamp);
            
            if (diff < minDiff) {
                minDiff = diff;
                closest = dataArray[mid];
            }
            
            if (dataArray[mid].time < targetTimestamp) {
                left = mid + 1;
            } else if (dataArray[mid].time > targetTimestamp) {
                right = mid - 1;
            } else {
                // Exact match found
                return dataArray[mid];
            }
        }
        
        return closest;
    }

    /**
     * Execute time jump directly on the chart
     */
    async executeDirectTimeJump(timeStr) {

        
        const activeAssetName = document.querySelector('.asset-name')?.textContent;
        
        if (!activeAssetName) {
            return false;
        }
        
        // Get the specific chart instance for the active asset
        let targetChart = null;
        
        // Check if multi-chart manager is available and has the asset
        if (window.multiChartManager && window.multiChartManager.hasChart && window.multiChartManager.hasChart(activeAssetName)) {
            targetChart = window.multiChartManager.getChart(activeAssetName);
        } else if (window.chart && window.chart.candleData) {
            targetChart = window.chart;
        }
        
        if (!targetChart || !targetChart.candleData) {
            return false;
        }
        
        try {
            let datasetPath = this.getActiveDatasetPath(activeAssetName);
            if (!datasetPath) {
                datasetPath = await this.autoDetectAssetPath(activeAssetName);
                if (!datasetPath) {
                    return false;
                }
            }
            

            const response = await fetch(datasetPath);
            const fullData = await response.json();
            
            const targetTimestamp = this.convertTimeToTimestamp(timeStr, fullData);
            const closestCandle = this.findClosestCandle(targetTimestamp, fullData);
            
            if (!closestCandle) {
                return false;
            }
            
            // Find target candle index in raw data
            const targetRawIndex = fullData.findIndex(candle => candle.time === closestCandle.time);
            

            if (targetChart.isPlaying) {
                targetChart.stopAnimation();
            }
            
            // Load full dataset into the specific chart instance
            targetChart.loadDataset(fullData, []);
            
            // Calculate target candle index in processed candle data (60 seconds per candle)
            const targetCandleIndex = Math.floor(targetRawIndex / 60);
            const targetCandleElapsed = targetRawIndex % 60;
            
            // Set chart position to target time
            setTimeout(() => {
                // Stop any current animation
                if (targetChart.isPlaying) {
                    targetChart.stopAnimation();
                }
                
                // Set position to target candle and elapsed time
                targetChart.currentCandleIndex = Math.min(targetCandleIndex, targetChart.candleData.length - 1);
                targetChart.currentCandleElapsed = targetCandleElapsed;
                
                // Mark candles before target as complete
                targetChart.candleData.forEach((candle, index) => {
                    candle.isComplete = index < targetCandleIndex;
                });
                
                // Set current price from target position
                const targetCandle = targetChart.candleData[targetCandleIndex];
                if (targetCandle && targetCandle.secondData && targetCandle.secondData[targetCandleElapsed]) {
                    targetChart.currentPrice = targetCandle.secondData[targetCandleElapsed].price;
                    targetChart.targetPrice = targetChart.currentPrice;
                } else if (targetCandle) {
                    targetChart.currentPrice = targetCandle.open;
                    targetChart.targetPrice = targetCandle.open;
                }
                
                // Initialize running extremes for current candle
                if (targetChart._initRunningExtremes) {
                    targetChart._initRunningExtremes();
                }
                
                // Reset pan offset to keep chart centered on current candle
                const a = targetChart.getChartArea();
                const twoCm = 100;
                const i = targetChart.currentCandleIndex;
                const isOddCandle = (i % 2) === 0;
                const gridIndex = Math.floor(i / 2);
                const targetCandleX = isOddCandle ?
                    gridIndex * twoCm :
                    gridIndex * twoCm + twoCm / 2;
                const chartCenter = a.width / 2;
                targetChart.panOffset = chartCenter - targetCandleX - twoCm;
                
                // Update displays
                if (targetChart.updateDisplays) {
                    targetChart.updateDisplays();
                }
                
                targetChart.startAnimation();
            }, 1000);
            
            return true;
            
        } catch (error) {
            return false;
        }
    }

    /**
     * Auto-detect dataset path for new assets
     */
    async autoDetectAssetPath(assetName) {
        // Use the existing getUserSpecificDatasetPath function from app.js
        if (window.getUserSpecificDatasetPath) {
            return await window.getUserSpecificDatasetPath(assetName);
        }
        
        // Fallback implementation
        let simplifiedAssetName = assetName.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
        
        // Only add 'otc' suffix if asset name contains '(OTC)'
        if (assetName.includes('(OTC)') && !simplifiedAssetName.endsWith('otc')) {
            simplifiedAssetName += 'otc';
        }
        
        try {
            const response = await fetch(`./php/list_datasets.php?asset=${encodeURIComponent(simplifiedAssetName)}`);
            const datasets = await response.json();
            
            if (datasets.length > 0) {
                const latestDataset = datasets[0];
                
                if (!this.currentUsername) {
                    await this.initializeUserSession();
                }
                
                if (this.currentUsername) {
                    const path = `./data/${this.currentUsername}/${simplifiedAssetName}/${latestDataset}/candle_data.json`;
                    return path;
                }
            }
        } catch (error) {
        }
        
        return null;
    }

    /**
     * Get data statistics
     */
    getDataStats() {
        return {
            originalLength: this.originalData ? this.originalData.length : 0,
            filteredLength: this.filteredData ? this.filteredData.length : 0,
            isActive: this.isActive,
            jumpTime: this.jumpTime,
            timeRange: this.dataTimeRange
        };
    }
}

// Initialize Time Jump Controller when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
    
    // Only initialize if we're in the signals panel
    if (document.getElementById('time-jump-input')) {
        window.timeJumpController = new TimeJumpController();
        
        // Periodic checks for chart readiness
        let checkCount = 0;
        const maxChecks = 15;
        const checkInterval = setInterval(() => {
            checkCount++;
            
            if (window.chart && window.chart.candleData && window.chart.candleData.length > 0) {
                window.timeJumpController.updateTimeRange();
                clearInterval(checkInterval);
            } else if (checkCount >= maxChecks) {
                clearInterval(checkInterval);
            }
        }, 2000);
        
        // Monitor for asset name changes with debouncing
        let lastAssetName = null;
        let updateTimeout = null;
        setInterval(() => {
            const currentAssetName = document.querySelector('.asset-name')?.textContent;
            if (currentAssetName && currentAssetName !== lastAssetName) {
                lastAssetName = currentAssetName;
                // Clear previous timeout to prevent multiple updates
                if (updateTimeout) clearTimeout(updateTimeout);
                updateTimeout = setTimeout(() => {
                    window.timeJumpController.updateTimeRange();
                    updateTimeout = null;
                }, 2000);
            }
        }, 1000);
    } else {
    }
});

// Listen for asset changes to update time range
let assetClickTimeout = null;
document.addEventListener('click', (e) => {
    const assetCard = e.target.closest('.asset-card-tab');
    if (assetCard && window.timeJumpController) {
        // Clear previous timeout to prevent multiple updates
        if (assetClickTimeout) clearTimeout(assetClickTimeout);
        assetClickTimeout = setTimeout(() => {
            window.timeJumpController.updateTimeRange();
            assetClickTimeout = null;
        }, 3000);
    }
});

// Listen for chart control button clicks
document.addEventListener('click', (e) => {
    if (e.target.id === 'chart-play-btn' || e.target.closest('#chart-play-btn')) {
        setTimeout(() => {
            if (window.timeJumpController) {
                window.timeJumpController.updateTimeRange();
            }
        }, 2000);
    }
});

// Export for use in other modules
if (typeof module !== 'undefined' && module.exports) {
    module.exports = TimeJumpController;
}