class Kumogata::Client
  def initialize(options)
    @options = options
    @options = Hashie::Mash.new(@options) unless @options.kind_of?(Hashie::Mash)
    @cloud_formation = AWS::CloudFormation.new
    @post_processing = Kumogata::PostProcessing.new(@options)
  end

  def create(path_or_url, stack_name = nil)
    validate_stack_name(stack_name)

    @options.delete_stack = false if stack_name
    template = open_template(path_or_url)
    update_deletion_policy(template)
    add_encryption_password(template)

    outputs = create_stack(template, stack_name)
    @post_processing.run(:create, outputs)

    nil
  end

  def validate(path_or_url)
    template = open_template(path_or_url)
    add_encryption_password_for_validation(template)
    validate_template(template)
    nil
  end

  def convert(path_or_url)
    template = open_template(path_or_url)

    if ruby_template?(path_or_url)
      JSON.pretty_generate(template).colorize_as(:json)
    else
      devaluate_template(template).chomp.colorize_as(:ruby)
    end
  end

  def update(path_or_url, stack_name)
    validate_stack_name(stack_name)

    template = open(path_or_url) do |f|
      evaluate_template(f)
    end

    add_encryption_password(template)

    outputs = update_stack(template, stack_name)
    @post_processing.run(:update, outputs)

    nil
  end

  def delete(stack_name)
    validate_stack_name(stack_name)

    if @options.force? or agree("Are you sure you want to delete `#{stack_name}`? ".yellow)
      delete_stack(stack_name)
    end

    nil
  end

  def list(stack_name = nil)
    validate_stack_name(stack_name)

    stacks = describe_stacks(stack_name)
    JSON.pretty_generate(stacks).colorize_as(:json)
  end

  def export(stack_name)
    validate_stack_name(stack_name)

    template = export_template(stack_name)
    devaluate_template(template).chomp.colorize_as(:ruby)
  end

  def show_events(stack_name)
    validate_stack_name(stack_name)

    events = describe_events(stack_name)
    JSON.pretty_generate(events).colorize_as(:json)
  end

  def show_outputs(stack_name)
    validate_stack_name(stack_name)

    outputs = describe_outputs(stack_name)
    JSON.pretty_generate(outputs).colorize_as(:json)
  end

  def show_resources(stack_name)
    validate_stack_name(stack_name)

    resources = describe_resources(stack_name)
    JSON.pretty_generate(resources).colorize_as(:json)
  end

  def diff(path_or_url1, path_or_url2)
    templates = [path_or_url1, path_or_url2].map do |path_or_url|
      template = open_template(path_or_url)
      JSON.pretty_generate(template)
    end

    diff_opts = @options.ignore_all_space? ? '-uw' : '-u'
    opts = {:include_diff_info => true, :diff => diff_opts}
    diff = Diffy::Diff.new(*templates, opts).to_s

    diff.sub(/^(\e\[\d+m)?\-\-\-(\s+)(\S+)/m) { "#{$1}---#{$2}#{path_or_url1}"}
        .sub(/^(\e\[\d+m)?\+\+\+(\s+)(\S+)/m) { "#{$1}+++#{$2}#{path_or_url2}"}
  end

  private ###########################################################

  def open_template(path_or_url)
    format = @options.format || (ruby_template?(path_or_url) ? :ruby : :json)

    open(path_or_url) do |f|
      case format
      when :ruby
        evaluate_template(f)
      when :json
        JSON.parse(f.read)
      else
        raise "Unknown format: #{format}"
      end
    end
  end

  def ruby_template?(path_or_url)
    File.extname(path_or_url) == '.rb'
  end

  def evaluate_template(template)
    key_converter = proc do |key|
      key = key.to_s
      key.gsub!('__', '::') if @options.replace_underscore?
      key
    end

    value_converter = proc do |v|
      case v
      when Hash, Array
        v
      else
        v.to_s
      end
    end

    template = Dslh.eval(template.read, {
      :key_conv   => key_converter,
      :value_conv => value_converter,
      :scope_hook => proc {|scope|
        define_template_func(scope, template.path)
      },
      :filename   => template.path,
    })

    @post_processing.fetch!(template)

    return template
  end

  def evaluate_after_trigger(template)
    triggers = template.delete('_after')
    return {} unless triggers

  end

  def devaluate_template(template)
    exclude_key = proc do |k|
      k = k.to_s.gsub('::', '__')
      k !~ /\A[_a-z]\w+\Z/i and k !~ %r|\A/\S*\Z|
    end

    key_conv = proc do |k|
      k = k.to_s

      if k =~ %r|\A/\S*\Z|
        proc do |v, nested|
          if nested
            "_path(#{k.inspect}) #{v}"
          else
            "_path #{k.inspect}, #{v}"
          end
        end
      else
        k.gsub('::', '__')
      end
    end

    value_conv = proc do |v|
      if v.kind_of?(String) and v =~ /\A(?:0|[1-9]\d*)\Z/
        v.to_i
      else
        v
      end
    end

    Dslh.deval(template, :key_conv => key_conv, :value_conv => value_conv, :exclude_key => exclude_key)
  end

  def define_template_func(scope, path_or_url)
    scope.instance_eval(<<-EOS)
      def _include(file)
        path = file.dup

        unless path =~ %r|\\A/| or path =~ %r|\\A\\w+://|
          path = File.expand_path(File.join(File.dirname(#{path_or_url.inspect}), path))
        end

        open(path) {|f| instance_eval(f.read) }
      end

      def _path(path, value = nil, &block)
        if block
          value = Dslh::ScopeBlock.nest(binding, 'block')
        end

        @__hash__[path] = value
      end

      def _post(options = {}, &block)
        commands = Dslh::ScopeBlock.nest(binding, 'block')

        @__hash__[:_post] = {
          :options  => options,
          :commands => commands,
        }
      end
    EOS
  end

  def create_stack(template, stack_name)
    unless stack_name
      user_host = Kumogata::Utils.get_user_host

      stack_name = ['kumogata']
      stack_name << user_host if user_host
      stack_name << UUIDTools::UUID.timestamp_create

      stack_name = stack_name.join('-')
      stack_name.gsub!(/[^-a-zA-Z0-9]+/, '-')
    end

    Kumogata.logger.info("Creating stack: #{stack_name}".cyan)
    stack = @cloud_formation.stacks.create(stack_name, template.to_json, build_create_options)
    event_log = {}

    unless while_in_progress(stack, 'CREATE_COMPLETE', event_log)
      errmsgs = ['Create failed']
      errmsgs << stack_name
      errmsgs << stack.status_reason if stack.status_reason
      raise errmsgs.join(': ')
    end

    outputs = outputs_for(stack)
    summaries = resource_summaries_for(stack)

    if @options.delete_stack?
      delete_stack(stack_name)
    end

    output_result(stack_name, outputs, summaries)

    return outputs
  end

  def update_stack(template, stack_name)
    stack = @cloud_formation.stacks[stack_name]
    stack.status

    Kumogata.logger.info("Updating stack: #{stack_name}".green)
    event_log = create_event_log(stack)
    stack.update(build_update_options(template.to_json))

    unless while_in_progress(stack, 'UPDATE_COMPLETE', event_log)
      errmsgs = ['Update failed']
      errmsgs << stack_name
      errmsgs << stack.status_reason if stack.status_reason
      raise errmsgs.join(': ')
    end

    outputs = outputs_for(stack)
    summaries = resource_summaries_for(stack)
    output_result(stack_name, outputs, summaries)

    return outputs
  end

  def delete_stack(stack_name)
    stack = @cloud_formation.stacks[stack_name]
    stack.status

    Kumogata.logger.info("Deleting stack: #{stack_name}".red)
    event_log = create_event_log(stack)
    stack.delete

    completed = false

    begin
      completed = while_in_progress(stack, 'DELETE_COMPLETE', event_log)
    rescue AWS::CloudFormation::Errors::ValidationError
      # Handle `Stack does not exist`
      completed = true
      Kumogata.logger.info('Success')
    end

    unless completed
      errmsgs = ['Delete failed']
      errmsgs << stack_name
      errmsgs << stack.status_reason if stack.status_reason
      raise errmsgs.join(': ')
    end
  end

  def describe_stacks(stack_name)
    AWS.memoize do
      stacks = @cloud_formation.stacks
      stacks = stacks.select {|i| i.name == stack_name } if stack_name

      stacks.map do |stack|
        {
          'StackName'    => stack.name,
          'CreationTime' => stack.creation_time,
          'StackStatus'  => stack.status,
          'Description'  => stack.description,
        }
      end
    end
  end

  def export_template(stack_name)
    stack = @cloud_formation.stacks[stack_name]
    stack.status
    JSON.parse(stack.template)
  end

  def describe_events(stack_name)
    AWS.memoize do
      stack = @cloud_formation.stacks[stack_name]
      stack.status
      events_for(stack)
    end
  end

  def describe_outputs(stack_name)
    AWS.memoize do
      stack = @cloud_formation.stacks[stack_name]
      stack.status
      outputs_for(stack)
    end
  end

  def describe_resources(stack_name)
    AWS.memoize do
      stack = @cloud_formation.stacks[stack_name]
      stack.status
      resource_summaries_for(stack)
    end
  end

  def while_in_progress(stack, complete_status, event_log)
    # XXX: Status does not change if you have been memoized.
    #      Should be forcibly disabled memoization?
    while stack.status =~ /_IN_PROGRESS\Z/
      print_event_log(stack, event_log)
      sleep 1
    end

    print_event_log(stack, event_log)
    completed = (stack.status == complete_status)
    Kumogata.logger.info(completed ? 'Success' : 'Failure')
    return completed
  end

  def print_event_log(stack, event_log)
    events_for(stack).sort_by {|i| i['Timestamp'] }.each do |event|
      event_id = event['EventId']

      unless event_log[event_id]
        event_log[event_id] = event

        timestamp = event['Timestamp']
        summary = {}

        ['LogicalResourceId', 'ResourceStatus', 'ResourceStatusReason'].map do |k|
          summary[k] = event[k]
        end

        puts [
          timestamp.getlocal.strftime('%Y/%m/%d %H:%M:%S %Z'),
          summary.to_json.colorize_as(:json),
        ].join(': ')
      end
    end
  end

  def create_event_log(stack)
    event_log = {}

    events_for(stack).sort_by {|i| i['Timestamp'] }.each do |event|
      event_id = event['EventId']
      event_log[event_id] = event
    end

    return event_log
  end

  def build_create_options
    opts = {}
    add_parameters(opts)

    [:capabilities, :disable_rollback, :notify, :timeout].each do |k|
      opts[k] = @options[k] if @options[k]
    end

    return opts
  end

  def build_update_options(template)
    opts = {:template => template}
    add_parameters(opts)
    return opts
  end

  def add_parameters(hash)
    if @options.parameters? and not @options.parameters.empty?
      parameters = {}

      enc_params = @options.encrypt_parameters
      passwd = @options.encryption_password || Kumogata::Crypt.mkpasswd(16)

      @options.parameters.each do |key, value|
        if enc_params and (enc_params.include?('*') or enc_params.include?(key))
          value = Kumogata::Crypt.encrypt(passwd, value)
        end

        parameters[key] = value
      end

      if @options.encrypt_parameters? and not @options.skip_send_password?
        parameters[Kumogata::ENCRYPTION_PASSWORD] = passwd.encode64
      end

      hash[:parameters] = parameters
    end
  end

  def update_deletion_policy(template)
    if @options.delete_stack? or @options.deletion_policy_retain?
      template['Resources'].each do |k, v|
        v['DeletionPolicy'] ||= 'Retain'
      end
    end
  end

  def add_encryption_password(template)
    if @options.encrypt_parameters? and not @options.skip_send_password?
      template['Parameters'] ||= {}

      template['Parameters'][Kumogata::ENCRYPTION_PASSWORD] = {
        'Type'   => 'String',
        'NoEcho' => 'true',
      }
    end
  end

  def add_encryption_password_for_validation(template)
    template['Parameters'] ||= {}

    template['Parameters'][Kumogata::ENCRYPTION_PASSWORD] ||= {
      'Type' => 'String',
      'Default' => "(#{Kumogata::ENCRYPTION_PASSWORD})",
    }
  end

  def validate_template(template)
    result = @cloud_formation.validate_template(template.to_json)

    if result[:code]
      raise result.values_at(:code, :message).join(': ')
    end

    Kumogata.logger.info('Template validated successfully'.green)
  end

  def events_for(stack)
    stack.events.map do |event|
      event_hash = {}

      [
        :event_id,
        :logical_resource_id,
        :physical_resource_id,
        :resource_properties,
        :resource_status,
        :resource_status_reason,
        :resource_type,
        :stack_id,
        :stack_name,
        :timestamp,
      ].each do |k|
        event_hash[Kumogata::Utils.camelize(k)] = event.send(k)
      end

      event_hash
    end
  end

  def outputs_for(stack)
    outputs_hash = {}

    stack.outputs.each do |output|
      outputs_hash[output.key] = output.value
    end

    return outputs_hash
  end

  def resource_summaries_for(stack)
    stack.resource_summaries.map do |summary|
      summary_hash = {}

      [
        :logical_resource_id,
        :physical_resource_id,
        :resource_type,
        :resource_status,
        :resource_status_reason,
        :last_updated_timestamp
      ].each do |k|
        summary_hash[Kumogata::Utils.camelize(k)] = summary[k]
      end

      summary_hash
    end
  end

  def output_result(stack_name, outputs, summaries)
    puts <<-EOS

Stack Resource Summaries:
#{JSON.pretty_generate(summaries).colorize_as(:json)}

Outputs:
#{JSON.pretty_generate(outputs).colorize_as(:json)}

(Save to `#{@options.result_log}`)
    EOS

    open(@options.result_log, 'wb') do |f|
      f.puts JSON.pretty_generate({
        'StackName' => stack_name,
        'StackResourceSummaries' => summaries,
        'Outputs' => outputs,
      })
    end
  end

  def validate_stack_name(stack_name)
    return unless stack_name

    unless /\A[a-zA-Z][-a-zA-Z0-9]*\Z/i =~ stack_name
      raise "1 validation error detected: Value '#{stack_name}' at 'stackName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*"
    end
  end
end