# rubocop:disable Metrics/AbcSize
require 'fastlane/erb_template_helper'
require 'ostruct'
require 'rexml/document'
require 'zip'

module Fastlane
 module Actions
  module SharedValues
   MANIFEST_VERSION_NUM = :MANIFEST_VERSION_NUM
   MANIFEST_VERSION_NAME = :MANIFEST_VERSION_NAME
   end

   MANIFEST_FILE_MAP = {
      manifest_file: '-f',
   }

   class ManifestAnalyzerAction < Action
      def self.run(params)
         # Calling fetch on config so that default values will be used            
         manifest_path = params[:manifest_file]
         
         manifest_xml = self.fetch_manifest_file(manifest_path)
         Actions.lane_context[SharedValues::MANIFEST_VERSION_NUM] = manifest_xml.attributes['android:versionName']
         Actions.lane_context[SharedValues::MANIFEST_VERSION_NAME] = manifest_xml.attributes['android:versionCode']
         return true
      end

      def self.fetch_manifest_file(file)
        content = File.open(file)
        xml = REXML::Document.new(content)
        
        return xml.elements['manifest']
      end

      def self.available_options
        [
          ['manifest_file', 'This is the manifest_file path']
        ]
      end

      def self.output
      [
        ['MANIFEST_VERSION_NUM', 'The apk version number'],
        ['MANIFEST_VERSION_NAME', 'The apk version name']
      ]
      end

      def self.author
       "pconno3"
      end

      def self.is_supported?(platform)
       platform == :android
      end
   end
end
end
