# encoding: UTF-8
module Axlsx
  # 3D attributes for a chart.
  class View3D

    # Validation for hPercent
    H_PERCENT_REGEX = /0*(([5-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)/

    # validation for depthPercent
    DEPTH_PERCENT_REGEX = /0*(([2-9][0-9])|([1-9][0-9][0-9])|(1[0-9][0-9][0-9])|2000)/

    # x rotation for the chart
    # must be between -90 and 90
    # @return [Integer]
    attr_reader :rot_x
    alias :rotX :rot_x
    
    # height of chart as % of chart width
    # must be between 5% and 500%
    # @return [String]
    attr_reader :h_percent
    alias :hPercent :h_percent

    # y rotation for the chart
    # must be between 0 and 360
    # @return [Integer]
    attr_reader :rot_y
    alias :rotY :rot_y
    
    # depth or chart as % of chart width
    # must be between 20% and 2000%
    # @return [String]
    attr_reader :depth_percent
    alias :depthPercent :depth_percent

    # Chart axis are at right angles
    # @return [Boolean]
    attr_reader :r_ang_ax
    alias :rAngAx :r_ang_ax

    # field of view angle
    # @return [Integer]
    attr_reader :perspective

    # Creates a new View3D for charts
    # @option options [Integer] rot_x
    # @option options [String] h_percent
    # @option options [Integer] rot_y
    # @option options [String] depth_percent
    # @option options [Boolean] r_ang_ax
    # @option options [Integer] perspective
    def initialize(options={})
      @rot_x, @h_percent, @rot_y, @depth_percent, @r_ang_ax, @perspective  = nil, nil, nil, nil, nil, nil
      options.each do |o|
        self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
      end
    end

    # @see rot_x
    def rot_x=(v) DataTypeValidator.validate "#{self.class}.rot_x", [Integer, Fixnum], v, lambda {|arg| arg >= -90 && arg <= 90 }; @rot_x = v; end
    alias :rotX= :rot_x=

    # @see h_percent
    def h_percent=(v)
      RegexValidator.validate "#{self.class}.h_percent", H_PERCENT_REGEX, v
      @h_percent = v
    end
    alias :hPercent= :h_percent=

    # @see rot_y
    def rot_y=(v) DataTypeValidator.validate "#{self.class}.rot_y", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 360 }; @rot_y = v; end
    alias :rotY= :rot_y=

    # @see depth_percent
    def depth_percent=(v) RegexValidator.validate "#{self.class}.depth_percent", DEPTH_PERCENT_REGEX, v; @depth_percent = v; end
    alias :depthPercent= :depth_percent=

    # @see r_ang_ax
    def r_ang_ax=(v) Axlsx::validate_boolean(v); @r_ang_ax = v; end
    alias :rAngAx= :r_ang_ax=
     
    # @see perspective
    def perspective=(v) DataTypeValidator.validate "#{self.class}.perspective", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 240 }; @perspective = v; end


    # Serializes the object
    # @param [String] str
    # @return [String]
    def to_xml_string(str = '')
      str << '<c:view3D>'
      str << '<c:rotX val="' << @rot_x.to_s << '"/>' unless @rot_x.nil?
      str << '<c:hPercent val="' << @h_percent.to_s << '"/>' unless @h_percent.nil?
      str << '<c:rotY val="' << @rot_y.to_s << '"/>' unless @rot_y.nil?
      str << '<c:depthPercent val="' << @depth_percent.to_s << '"/>' unless @depth_percent.nil?
      str << '<c:rAngAx val="' << @r_ang_ax.to_s << '"/>' unless @r_ang_ax.nil?
      str << '<c:perspective val="' << @perspective.to_s << '"/>' unless @perspective.nil?
      str << '</c:view3D>'
    end

  end
end
