# frozen_string_literal: true

module Cryptum
  # Cryptum::UI Module used for Presenting the
  # Cryptum Curses Interface
  module UI
    # Cryptum::UI::Order Namespace
    module Order
      # Update the ExecuteDetails Section of the UI (When Active)
      module ExecuteDetails
        # Supported Method Parameters::
        # Cryptum::UI::Order::ExecuteDetails.refresh(
        # )

        public_class_method def self.refresh(opts = {})
          event_history = opts[:event_history]
          order_execute_details_win = opts[:order_execute_details_win]

          order_meta_data = event_history.order_execute_selected_data

          order_color = order_meta_data[:color].to_sym
          order_id = case order_color
                     when :cyan, :red
                       order_meta_data[:buy_order_id]
                     when :green, :magenta, :yellow
                       order_meta_data[:sell_order_id]
                     when :white
                       'Expired'
                     else
                       'N/A'
                     end

          order_id_details = "- ID: #{order_id}"

          order_history_arr = event_history.order_book[:order_history].select do |order|
            order if order[:id] == order_id
          end

          if order_history_arr.empty? && order_color == :yellow
            order_meta_data.delete(:sell_order_id)
            order_id = order_meta_data[:buy_order_id]
            order_meta_data[:color] = :cyan
            order_history_arr = event_history.order_book[:order_history].select do |order|
              order if order[:id] == order_id
            end
          end

          order_type_ln = '- Type: N/A'
          order_status_ln = '- Status: N/A'
          fill_fees = 'N/A'
          fill_size = 'N/A'
          executed_value = 'N/A'
          market_type = 'N/A'
          settled = 'N/A'
          unless order_history_arr.empty?
            order_history = order_history_arr.first
            order_type = order_history[:type].capitalize
            time_in_force = order_history[:time_in_force].upcase
            order_status = order_history[:status].upcase
            created_at = order_history[:created_at]
            fill_fees = order_history[:fill_fees]
            fill_size = order_history[:filled_size]
            executed_value = order_history[:executed_value]
            market_type = order_history[:market_type]
            settled = order_history[:settled]

            order_side = order_history[:side].capitalize
            case order_side.to_sym
            when :Buy
              expire_time = order_history[:expire_time]
              done_at = order_history[:done_at]
              order_meta_data[:color] = :cyan if done_at
            when :Sell
              done_at = order_history[:done_at]
            end

            order_type_ln = "- Type: #{order_type} #{order_side} #{time_in_force}"
            order_type_ln = "- Type: #{order_type} #{order_side} #{time_in_force} 1m" if time_in_force == 'GTT'
            order_status_ln = "- Status: #{order_status}"
          end

          order_hist_created_finished_ln = if created_at && !expire_time && !done_at
                                             "- Creation Date: #{created_at} | Finished Date: N/A"
                                           elsif created_at && expire_time && !done_at
                                             "- Creation Date: #{created_at} | Expiring Date: #{expire_time}"
                                           elsif created_at && done_at
                                             "- Creation Date: #{created_at} | Finished Date: #{done_at}"
                                           else
                                             '- Creation Date: N/A | Finished Date: N/A'
                                           end

          invest_out = Cryptum.beautify_large_number(
            value: order_meta_data[:invest]
          )
          price_out = Cryptum.beautify_large_number(
            value: order_meta_data[:price]
          )
          size_out = Cryptum.beautify_large_number(
            value: order_meta_data[:size]
          )
          target_price_out = Cryptum.beautify_large_number(
            value: order_meta_data[:target_price]
          )

          invest = "$#{invest_out} @ "
          tick = "$#{price_out} = "
          size = "*#{size_out} + "
          tpm_out = "#{order_meta_data[:tpm]}% = "
          targ_tick = "$#{target_price_out}"
          profit = " | Profit: $#{order_meta_data[:profit]}"

          details_ln1 = "- Slice Plan: #{invest}#{tick}#{size}#{tpm_out}#{targ_tick}#{profit}"
          details_ln2 = "- Fees: $#{fill_fees} | Fill Size: *#{fill_size}"
          details_ln3 = "- Executed Value: $#{executed_value} | Market Type: #{market_type} | Settled: #{settled}"

          # UI
          col_just1 = (Curses.cols - Cryptum::UI.col_first) - 1

          # ROW 1
          out_line_no = 0
          line_color = order_color
          header_color = order_color
          header_style = :bold
          style = :bold
          header_style = :reverse if event_history.order_execute_details_win_active

          Cryptum::UI.line(
            ui_win: order_execute_details_win,
            out_line_no: out_line_no,
            color: line_color
          )

          # ROW 2
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol
          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: header_color,
            style: header_style,
            string: ''.ljust(col_just1, ' ')
          )

          header_str = "- ORDER ##{order_meta_data[:plan_no]} DETAILS -"
          order_execute_details_win.setpos(
            out_line_no,
            Cryptum::UI.col_center(str: header_str)
          )

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: header_style,
            string: header_str
          )

          # ROW 3
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: style,
            string: order_id_details.ljust(col_just1, ' ')
          )

          # ROW 4
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: style,
            string: order_type_ln.ljust(col_just1, ' ')
          )

          # ROW 5
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: style,
            string: order_status_ln.ljust(col_just1, ' ')
          )

          # ROW 6
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: style,
            string: order_hist_created_finished_ln.ljust(col_just1, ' ')
          )

          # ROW 7
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: style,
            string: details_ln1.ljust(col_just1, ' ')
          )

          # ROW 8
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: style,
            string: details_ln2.ljust(col_just1, ' ')
          )

          # ROW 9
          out_line_no += 1
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: style,
            string: details_ln3.ljust(col_just1, ' ')
          )

          # Clear to OK ROW
          ok_row = 9
          out_line_no += 1
          to_ok_row = ok_row - 1
          (out_line_no..to_ok_row).each do |clr_line|
            order_execute_details_win.setpos(clr_line, Cryptum::UI.col_first)
            Cryptum::UI.colorize(
              ui_win: order_execute_details_win,
              color: order_color,
              style: :normal,
              string: ''.ljust(col_just1, ' ')
            )
          end

          # OK ROW
          out_line_no = ok_row
          order_execute_details_win.setpos(out_line_no, Cryptum::UI.col_first)
          order_execute_details_win.clrtoeol
          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            string: ''.ljust(col_just1, ' ')
          )

          header_str = '- OK -'
          order_execute_details_win.setpos(
            out_line_no,
            Cryptum::UI.col_center(str: header_str)
          )

          Cryptum::UI.colorize(
            ui_win: order_execute_details_win,
            color: order_color,
            style: :reverse,
            string: header_str
          )

          order_execute_details_win.refresh

          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}.refresh(
           )
          "
        end
      end
    end
  end
end