# frozen_string_literal: true

module Cryptum
  # Cryptum::Event Namespace
  module Event
    # This Module is used to parse message received by the Web Socket
    module Parse
      # Supported Method Parameters::
      # Cryptum::Event::Parse.websocket_msg(
      # )
      public_class_method def self.websocket_msg(opts = {})
        env = opts[:env]
        terminal_win = opts[:terminal_win]
        option_choice = opts[:option_choice]
        event_history = opts[:event_history]
        indicator_status = opts[:indicator_status]
        bot_conf = opts[:bot_conf]

        this_product = event_history.order_book[:this_product]
        fiat = this_product[:quote_currency]
        fiat_portfolio_file = "#{option_choice.session_root}/order_books/#{fiat}_PORTFOLIO.json"

        # Determine if Summary UI needs updated data
        event_history = Cryptum::Portfolio::Balance.refresh(
          env: env,
          option_choice: option_choice,
          terminal_win: terminal_win,
          event_history: event_history,
          fiat_portfolio_file: fiat_portfolio_file
        )

        # If the Terminal Window has been Resized, Resize the UI
        if Curses.cols != terminal_win.cols
          terminal_win.cols = Curses.cols
          terminal_win.ticker_ui_resize = true
          terminal_win.market_trend_ui_resize = true
        end

        event_history = Cryptum::UI::Portfolio.refresh(
          option_choice: option_choice,
          portfolio_win: terminal_win.portfolio_section,
          event_history: event_history,
          key_press_event: terminal_win.key_press_event,
          indicator_status: indicator_status,
          bot_conf: bot_conf,
          fiat_portfolio_file: fiat_portfolio_file
        )

        if event_history.event_type == :ticker ||
           terminal_win.ticker_ui_resize

          ticker_event = event_history.ticker_event = event_history.event if event_history.event_type == :ticker
          ticker_event = event_history.ticker_event if terminal_win.ticker_ui_resize
          event_history = Cryptum::UI::Ticker.refresh(
            option_choice: option_choice,
            start_time: event_history.start_time,
            ticker_win: terminal_win.ticker_section,
            key_press_event: terminal_win.key_press_event,
            event_history: event_history,
            event: ticker_event
          )
        end

        Cryptum::UI::Order::Timer.refresh(
          option_choice: option_choice,
          event_history: event_history,
          order_timer_win: terminal_win.order_timer_section,
          indicator_status: indicator_status,
          key_press_event: terminal_win.key_press_event
        )

        if event_history.event_type == :l2update ||
           terminal_win.market_trend_ui_resize

          market_trend_event = event_history.market_trend_event = event_history.event if event_history.event_type == :l2update
          market_trend_event = event_history.market_trend_event if terminal_win.market_trend_ui_resize
          event_history = Cryptum::UI::MarketTrend.refresh(
            option_choice: option_choice,
            market_trend_win: terminal_win.market_trend_section,
            event_history: event_history,
            key_press_event: terminal_win.key_press_event,
            event: market_trend_event,
            indicator_status: indicator_status,
            bot_conf: bot_conf
          )
        end

        indicator_status = Cryptum::UI::SignalEngine.refresh(
          option_choice: option_choice,
          signal_engine_win: terminal_win.signal_engine_section,
          event_history: event_history,
          key_press_event: terminal_win.key_press_event,
          indicator_status: indicator_status,
          bot_conf: bot_conf,
          fiat_portfolio_file: fiat_portfolio_file
        )

        unless event_history.order_plan_details_win_active
          event_history = Cryptum::UI::Order::Plan.refresh(
            option_choice: option_choice,
            order_plan_win: terminal_win.order_plan_section,
            env: env,
            event_history: event_history,
            key_press_event: terminal_win.key_press_event,
            indicator_status: indicator_status,
            bot_conf: bot_conf,
            fiat_portfolio_file: fiat_portfolio_file
          )
          recalc_op = event_history.recalculate_order_plan
          order_plan = event_history.order_book[:order_plan]
          terminal_win.key_press_event.key_w = true if (event_history.order_ready || recalc_op || order_plan.empty?) &&
                                                       !event_history.red_pill
          event_history.recalculate_order_plan = false
        end

        if event_history.order_plan_details_win_active
          event_history = Cryptum::UI::Order::PlanDetails.refresh(
            option_choice: option_choice,
            order_plan_details_win: terminal_win.order_plan_details_section,
            event_history: event_history,
            key_press_event: terminal_win.key_press_event
          )
        end

        unless event_history.order_execute_details_win_active
          event_history = Cryptum::UI::Order::Execute.refresh(
            option_choice: option_choice,
            order_execute_win: terminal_win.order_execute_section,
            env: env,
            event_history: event_history,
            key_press_event: terminal_win.key_press_event,
            indicator_status: indicator_status,
            bot_conf: bot_conf,
            fiat_portfolio_file: fiat_portfolio_file
          )
        end

        if event_history.order_execute_details_win_active
          event_history = Cryptum::UI::Order::ExecuteDetails.refresh(
            option_choice: option_choice,
            order_execute_details_win: terminal_win.order_execute_details_section,
            event_history: event_history,
            key_press_event: terminal_win.key_press_event
          )
        end

        # Refresh Command Section for Cryptum Session Usage
        Cryptum::UI::Command.refresh(
          command_win: terminal_win.command_section,
          key_press_event: terminal_win.key_press_event
        )

        event_history
      rescue Interrupt, StandardError => e
        Cryptum::Log.append(level: :error, msg: e, which_self: self, event_history: event_history)
      end

      # Display Usage for this Module
      public_class_method def self.help
        puts "USAGE:
          #{self}.websocket_msg(
            env: env,
            terminal_win: terminal_win,
            option_choice: option_choice,
            event_history: event_history,
            indicator_status: indicator_status,
            bot_conf: bot_conf,
            ai_enabled: ai_enabled
          )
        "
      end
    end
  end
end