require_relative './mixin/colorize'
require_relative './replace_properties'

module Convection
  module Model
    ##
    # Difference between an item in two templates
    ##
    class Diff
      extend Mixin::Colorize

      attr_reader :key
      attr_accessor :action
      attr_reader :ours
      attr_reader :theirs
      colorize :action, :green => [:create], :yellow => [:update, :retain], :red => [:delete, :replace]

      def initialize(key, ours, theirs)
        @key = key
        @ours = ours
        @theirs = theirs

        @action = if ours && theirs
                    property_name = key[/AWS::[A-Za-z0-9:]+\.[A-Za-z0-9]+/]
                    if REPLACE_PROPERTIES.include?(property_name)
                      :replace
                    else
                      :update
                    end
                  elsif ours
                    :create
                  else
                    :delete
                  end
      end

      def to_thor
        message = case action
                  when :create then "#{ key }: #{ ours }"
                  when :update then "#{ key }: #{ theirs } => #{ ours }"
                  when :replace then "#{ key }: #{ theirs } => #{ ours }"
                  when :delete then key
                  when :retain then key
                  end

        [action, message, color]
      end

      def ==(other)
        @key == other.key &&
          @ours == other.ours &&
          @theirs == other.theirs &&
          @action == other.action
      end
    end
  end
end

require_relative './mixin/colorize'
require_relative './replace_properties'

module Convection
  module Model
    ##
    # Difference between an item in two templates
    ##
    class Diff
      include Comparable
      extend Mixin::Colorize

      attr_reader :key
      attr_accessor :action
      attr_reader :ours
      attr_reader :theirs
      colorize :action, :green => [:create], :yellow => [:update, :retain], :red => [:delete, :replace]

      def initialize(key, ours, theirs)
        @key = key
        @ours = ours
        @theirs = theirs

        @action = if ours && theirs
                    property_name = key[/AWS::[A-Za-z0-9:]+\.[A-Za-z0-9]+/]
                    if REPLACE_PROPERTIES.include?(property_name)
                      :replace
                    else
                      :update
                    end
                  elsif ours
                    :create
                  else
                    :delete
                  end
      end

      def to_thor
        message = case action
                  when :create then "#{ key }: #{ ours }"
                  when :update then "#{ key }: #{ theirs } => #{ ours }"
                  when :replace then "#{ key }: #{ theirs } => #{ ours }"
                  when :delete then key
                  when :retain then key
                  end

        [action, message, color]
      end

      def <=>(other)
        value = @key <=> other.key
        return value if value != 0

        value = @ours <=> other.ours
        return value if value != 0

        value = @theirs <=> other.theirs
        return value if value != 0

        @action <=> other.action
      end
    end
  end
end

Allow Diff objects to be sorted in addition to checking for equality.
Allow Diff objects to be sorted in addition to checking for equality.
