module ApplicationHelper
  def body_css_class
    @body_css_classes ||= []
    # view_css_class = [controller_path.split('/'), action_name, 'view'].flatten.join('-')
    view_css_class = [controller_path.split('/'), action_name, 'view'].flatten[0] + "-view"
    @body_css_classes.unshift(view_css_class).join(' ')
  end

  def currency_balance(principal, currency_symbol, dfx_id = nil)
    canisters = {
      ICYPEES: "es3he-kyaaa-aaaah-abzna-cai",
      EVL: "7wqow-haaaa-aaaao-ahbpq-cai",
      "SNS-1": "zfcdd-tqaaa-aaaaq-aaaga-cai",
      TENDY: "72uqs-pqaaa-aaaak-aes7a-cai",
      YC: "5gxp5-jyaaa-aaaag-qarma-cai",
      CZECH: "fkhzl-saaaa-aaaan-qd26a-cai",
      ICL: "5573k-xaaaa-aaaak-aacnq-cai",
      OT: "imeri-bqaaa-aaaai-qnpla-cai",
      "01": "6ohjj-4qaaa-aaaai-aapua-cai",
      BABYAROFdip20: "iy6hh-xaaaa-aaaan-qauza-cai"}
    multipliers = {
        TENDY: 100000000,
      ICYPEES: 100000000,
        CZECH: 100000000,
           OT: 100000000,
          EVL: 100000000,
      "SNS-1": 100000000,
          ICL: 100000000,
         "01": 100000,
      BABYAROFDIP20: 100000000
    }
    begin
      # if currency_symbol == "ICL"
      # binding.pry
      # end
      if ["SNS-1"].any?{ |s| s.casecmp(currency_symbol)==0 }
        wallet_currency_balance = `dfx canister --network ic call #{canisters[:"#{currency_symbol}"]} #{currency_symbol == "SNS-1" ? "icrc1_balance_of" : "balanceOf"} '(record { owner = principal "#{principal}"})'`.gsub("_","").match(/\d+/)[0].to_f/(multipliers[:"#{currency_symbol}"])
      elsif ["BTC"].any?{ |s| s.casecmp(currency_symbol)==0 }
        puts "BTC"
        `bitcoin-core.cli loadwallet #{dfx_id}`
        wallet_currency_balance = `bitcoin-core.cli getbalance`.strip
        if wallet_currency_balance.to_s.empty?
          `bitcoin-core.cli createwallet "#{dfx_id}"`
          `bitcoin-core.cli loadwallet #{dfx_id}`
        end
        wallet_currency_balance = `bitcoin-core.cli getbalance`.strip
        `bitcoin-core.cli unloadwallet #{dfx_id}`
      elsif ["testBTC"].any?{ |s| s.casecmp(currency_symbol)==0 }
        puts "testBTC"
        `bitcoin-core.cli loadwallet #{dfx_id}`
        wallet_currency_balance = `bitcoin-core.cli -testnet getbalance`.strip
        if wallet_currency_balance.to_s.empty?
          `bitcoin-core.cli -testnet createwallet "#{dfx_id}"`
        end
        `bitcoin-core.cli -testnet loadwallet #{dfx_id}`
        wallet_currency_balance = `bitcoin-core.cli -testnet getbalance`.strip
        `bitcoin-core.cli -testnet unloadwallet #{dfx_id}`
      elsif ["ICP"].any?{ |s| s.casecmp(currency_symbol)==0 }
        wallet_currency_balance = icp_balance(dfx_id)
      elsif ["ETH"].any?{ |s| s.casecmp(currency_symbol)==0 }
        wallet_currency_balance = `eth address:balance -n mainnet #{dfx_id}`.strip
        if wallet_currency_balance.empty?
          new_address = JSON.parse `eth address:random`
          `eth address:add #{dfx_id} #{new_address["privateKey"]}`
          wallet_currency_balance = `eth address:balance -n mainnet #{dfx_id}`.strip
        end
      elsif ["goerliBaseETH"].any?{ |s| s.casecmp(currency_symbol)==0 }
        wallet_currency_balance = `eth address:balance -n goerliBase #{dfx_id}`.strip
        if wallet_currency_balance.empty?
          new_address = JSON.parse `eth address:random`
          `eth address:add #{dfx_id} #{new_address["privateKey"]}`
          wallet_currency_balance = `eth address:balance -n goerliBase #{dfx_id}`.strip
        end
      elsif ["goerliETH"].any?{ |s| s.casecmp(currency_symbol)==0 }
        wallet_currency_balance = `eth address:balance -n goerli #{dfx_id}`.strip
        if wallet_currency_balance.empty?
          new_address = JSON.parse `eth address:random`
          `eth address:add #{dfx_id} #{new_address["privateKey"]}`
          wallet_currency_balance = `eth address:balance -n goerli #{dfx_id}`.strip
        end
      elsif ["sepETH"].any?{ |s| s.casecmp(currency_symbol)==0 }
        wallet_currency_balance = `eth address:balance -n sep #{dfx_id}`.strip
        if wallet_currency_balance.empty?
          new_address = JSON.parse `eth address:random`
          `eth address:add #{dfx_id} #{new_address["privateKey"]}`
          wallet_currency_balance = `eth address:balance -n sep #{dfx_id}`.strip
        end
      elsif canisters.keys.map{|k| k.to_s}.any?{ |s| s.casecmp(currency_symbol)==0 }
        # binding.pry
        wallet_currency_balance = `dfx canister --network ic call #{canisters[:"#{currency_symbol.upcase}"]} balanceOf '(principal "#{principal}")'`.gsub("_","").match(/\d+/)[0].to_f/(multipliers[:"#{currency_symbol.upcase}"])
      else
        return "Currency not found"
      end
    rescue
      return "Error fetching balance"
    end
    if wallet_currency_balance.to_s.empty?
      return "Error fetching "
    else
      return wallet_currency_balance 
    end
  end



end
