# frozen_string_literal: true

module Cryptum
  # Cryptum::UI Module used for Presenting the
  # Cryptum Curses Interface
  module UI
    # Update the Cryptum Command Section UI
    module Command
      # Supported Method Parameters::
      # Cryptum::UI::Command.refresh(
      #   action_command_status: 'required - Instatiated Cryptum::OrderBook::Probabilities Object'
      # )
      public_class_method def self.refresh(opts = {})
        command_win = opts[:command_win]
        key_press_event = opts[:key_press_event]

        key_press_event = Cryptum::UI.detect_key_press_in_ui(
          key_press_event: key_press_event,
          ui_win: command_win
        )

        if key_press_event.key_c
          key_c_color = :cyan
          key_c_style = :reverse
        else
          key_c_color = :white
          key_c_style = :bold
        end

        if key_press_event.key_g
          key_g_color = :cyan
          key_g_style = :reverse
        else
          key_g_color = :white
          key_g_style = :bold
        end

        if key_press_event.key_r
          key_r_color = :cyan
          key_r_style = :reverse
        else
          key_r_color = :white
          key_r_style = :bold
        end

        if key_press_event.key_u
          key_u_color = :cyan
          key_u_style = :reverse
        else
          key_u_color = :white
          key_u_style = :bold
        end

        if key_press_event.key_w
          key_w_color = :cyan
          key_w_style = :reverse
        else
          key_w_color = :white
          key_w_style = :bold
        end

        if key_press_event.key_x
          key_x_color = :cyan
          key_x_style = :reverse
        else
          key_x_color = :white
          key_x_style = :bold
        end

        command_str_r2_arr = [
          "* v#{Cryptum::VERSION} >>> ",
          '[',
          'C',
          ']ancel Open Orders',
          ' | ',
          '[',
          'G',
          ']TFO',
          ' | ',
          '[',
          'r',
          ']eload bot conf'
        ]

        command_str_r3_arr = [
          '[',
          'u',
          ']pdate summary',
          ' | ',
          '[',
          'w',
          ']rite order book',
          ' | ',
          'e[',
          'x',
          ']it'
        ]
        # ROW 1
        out_line_no = 0
        command_win.clrtoeol
        command_win.setpos(
          out_line_no,
          Cryptum::UI.col_center(str: command_str_r2_arr.join)
        )

        # * v#{Cryptum::VERSION} >>>
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :yellow,
          style: :bold,
          string: command_str_r2_arr.first
        )

        # [C]ancel Open Orders
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_c_color,
          style: key_c_style,
          string: command_str_r2_arr[1]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :yellow,
          style: key_c_style,
          string: command_str_r2_arr[2]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_c_color,
          style: key_c_style,
          string: command_str_r2_arr[3]
        )

        # |
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :white,
          string: command_str_r2_arr[4]
        )

        # [G]TFO
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_g_color,
          style: key_g_style,
          string: command_str_r2_arr[5]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :yellow,
          style: key_g_style,
          string: command_str_r2_arr[6]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_g_color,
          style: key_g_style,
          string: command_str_r2_arr[7]
        )

        # |
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :white,
          string: command_str_r2_arr[8]
        )

        # [r]eload bot conf
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_r_color,
          style: key_r_style,
          string: command_str_r2_arr[9]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :yellow,
          style: key_r_style,
          string: command_str_r2_arr[10]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_r_color,
          style: key_r_style,
          string: command_str_r2_arr.last
        )

        # ROW 3
        out_line_no += 1
        command_win.setpos(
          out_line_no,
          Cryptum::UI.col_center(str: command_str_r3_arr.join)
        )
        command_win.clrtoeol

        # [u]date summary
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_u_color,
          style: key_u_style,
          string: command_str_r3_arr.first
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :yellow,
          style: key_u_style,
          string: command_str_r3_arr[1]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_u_color,
          style: key_u_style,
          string: command_str_r3_arr[2]
        )

        # |
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :white,
          string: command_str_r3_arr[3]
        )

        # [w]rite order book
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_w_color,
          style: key_w_style,
          string: command_str_r3_arr[4]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :yellow,
          style: key_w_style,
          string: command_str_r3_arr[5]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_w_color,
          style: key_w_style,
          string: command_str_r3_arr[6]
        )

        # |
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :white,
          string: command_str_r3_arr[7]
        )

        # e[x]it
        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_x_color,
          style: key_x_style,
          string: command_str_r3_arr[8]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: :yellow,
          style: key_x_style,
          string: command_str_r3_arr[9]
        )

        Cryptum::UI.colorize(
          ui_win: command_win,
          color: key_x_color,
          style: key_x_style,
          string: command_str_r3_arr.last
        )

        # ROW 4
        out_line_no += 1
        Cryptum::UI.line(
          ui_win: command_win,
          out_line_no: out_line_no
        )

        command_win.refresh

        sleep 0.15 if key_press_event.key_g ||
                      key_press_event.key_r ||
                      key_press_event.key_u ||
                      key_press_event.key_w ||
                      key_press_event.key_x
      rescue Interrupt, StandardError => e
        Cryptum::Log.append(level: :error, msg: e, which_self: self)
      end

      # Display Usage for this Module

      public_class_method def self.help
        puts "USAGE:
         #{self}.refresh(
           command_status: 'required - Instatiated Cryptum::OrderBook::Probabilities Object'
         )
        "
      end
    end
  end
end