class TokensController < ApplicationController
  before_action :set_token, only: %i[ show edit update destroy refresh ]

  # GET /tokens or /tokens.json
  def index
   authorize @tokens = Token.all
  end

  # GET /tokens/1 or /tokens/1.json
  def show
    authorize @token
  end

  # GET /tokens/new
  def new
    authorize @token = Token.new
  end

  # GET /tokens/1/edit
  def edit
    authorize @token
  end

  def refresh
    authorize @token
    # if call icrc1_metadata #icrc1
    metadata = `dfx canister --network ic call #{@token.canister_id} icrc1_metadata 2>&1`
    if metadata.scan(/no update method/).present?
      # TODO: Use getTokenInfo instead of getMetadata
      metadata = `dfx canister --network ic call #{@token.canister_id} getMetadata 2>&1`
      if metadata.scan(/no update method/).present?
        @token.standard = :unknown
      else
        @token.standard = :dip20

        # TODO: Use getTokenInfo instead of getMetadata
        tokenInfo = `dfx canister --network ic call #{@token.canister_id} getTokenInfo 2>&1`
        @token.holder_number = tokenInfo.lines[2].split[2]

        @token.transfer_fee = metadata.lines[2].split[2].to_i
        @token.decimals = metadata.lines[3].split[2]
        # @token.owner = metadata.lines[4].split[3]
        # @token.logo_url = metadata.lines[5].split("\"")[1]
        @token.logo_url = tokenInfo.lines[8].split[2].split('"')[1]
        @token.name = metadata.lines[6].split("\"")[1]
        @token.total_supply = metadata.lines[7].split[2].to_i/(10.0**@token.decimals)
        @token.symbol = metadata.lines[8].split("\"")[1]
      end
    else
        @token.standard = :icrc1
        @token.transfer_fee = metadata.lines[2].split[7].to_i
        @token.decimals = metadata.lines[5].split[7].to_i
        begin
          @token.logo_url = metadata.lines[9].split("\"")[1]
        rescue
        end
        @token.name = metadata.lines[3].split("\"")[3]
        total_supply = `dfx canister --network ic call #{@token.canister_id} icrc1_total_supply 2>&1`
        @token.total_supply = total_supply.split[0][1..-1].to_i/(10.0**@token.decimals)
        @token.symbol = metadata.lines[4].split("\"")[3]
    end
    # elsif call getMetadata #dip20
    respond_to do |format|
      if @token.save
        format.html { redirect_to token_url(@token), notice: "Token was successfully refreshed." }
        format.json { render :show, status: :created, location: @token }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @token.errors, status: :unprocessable_entity }
      end
    end
  end

  # POST /tokens or /tokens.json
  def create
    authorize @token = Token.new(token_params)

    respond_to do |format|
      if @token.save
        format.html { redirect_to refresh_token_url(@token), notice: "Token was successfully created." }
        format.json { render :show, status: :created, location: @token }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @token.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tokens/1 or /tokens/1.json
  def update
    authorize @token
    respond_to do |format|
      if @token.update(token_params)
        format.html { redirect_to token_url(@token), notice: "Token was successfully updated." }
        format.json { render :show, status: :ok, location: @token }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @token.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tokens/1 or /tokens/1.json
  def destroy
    authorize @token
    @token.destroy

    respond_to do |format|
      format.html { redirect_to tokens_url, notice: "Token was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_token
      @token = Token.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def token_params
      params.require(:token).permit(:canister_id, :standard, :symbol, :name, :total_supply, :decimals, :transfer_fee, :logo_url)
    end
end
