class Vic < ActiveRecord::Base
  validates :number_letter, length: { maximum: 10 }
  validates :number, presence: true,
                     numericality: { only_integer: true,
                                     greater_than_or_equal_to: 1,
                                     less_than_or_equal_to: 9999 }
  validate :touroku_date_cannot_a_future_date,
           :zero_year_cannot_a_future_date
  validates :car_type, length: { maximum: 4 }
  validates :use, length: { maximum: 2 }
  validates :personal_or_business, length: { maximum: 3 }
  validates :body_shape, length: { maximum: 10 }
  validates :car_name, length: { maximum: 24 }
  validates :riding_capacity,  numericality: { less_than_or_equal_to: 100 }, allow_nil: true
  validates :maximum_loading_capacity, numericality: { less_than_or_equal_to: 100000 },
                                       allow_nil: true
  validates :weight, numericality: { less_than_or_equal_to: 100000 }, allow_nil: true
  validates :total_weight, numericality: { less_than_or_equal_to: 100000 }, allow_nil: true
  validates :chassis_number, length: { maximum: 128 }, format: { with: /[\x20-\x7E]+/ }
  validates :length, numericality: { only_integer: true,
                                     greater_than_or_equal_to: 1,
                                     less_than_or_equal_to: 999 }, allow_nil: true
  validates :width, numericality:  { only_integer: true,
                                     greater_than_or_equal_to: 1,
                                     less_than_or_equal_to: 500 }, allow_nil: true
  validates :height, numericality:  { only_integer: true,
                                      greater_than_or_equal_to: 1,
                                      less_than_or_equal_to: 500 }, allow_nil: true
  validates :ff_load, numericality:  { only_integer: true,
                                       greater_than_or_equal_to: 1,
                                       less_than_or_equal_to: 50000 }, allow_nil: true
  validates :fr_load, numericality:  { only_integer: true,
                                       greater_than_or_equal_to: 1,
                                       less_than_or_equal_to: 50000 }, allow_nil: true
  validates :rf_load, numericality:  { only_integer: true,
                                       greater_than_or_equal_to: 1,
                                       less_than_or_equal_to: 50000 }, allow_nil: true
  validates :rr_load, numericality:  { only_integer: true,
                                       greater_than_or_equal_to: 1,
                                       less_than_or_equal_to: 50000 }, allow_nil: true
  validates :model, length: { maximum: 24 }
  validates :motor, length: { maximum: 16 }
  validates :output, numericality:  { greater_than_or_equal_to: 1, less_than: 11 },
                                    allow_nil: true
  validates :fuel, length: { maximum: 16 }
  validates :katashiki, length: { maximum: 16 }
  validates :ruibetsu, length: { maximum: 16 }
  validates :user_name, length: { maximum: 24 }
  validates :user_address, length: { maximum: 48 }
  validates :owner_name, length: { maximum: 24 }
  validates :owner_address, length: { maximum: 48 }
  validates :headquaters, length: { maximum: 48 }
  validate :limit_must_be_date, :limit2_must_be_date
  validates :note, length: { maximum: 512 }

# 和暦用仮想attributes
  attr_reader :touroku_date_wareki_s, :zero_year_wareki_s, :limit_wareki_s, :limit2_wareki_s


### methods..
  def conv_wareki
    @touroku_date_wareki_s = wareki_kanji_format(touroku_date.jisx0301) if !touroku_date.blank?
    if !zero_year.blank? then
      wareki = zero_year.jisx0301
      /^[a-zA-Z]([0-9]{2})\.([0-9]{2})\.([0-9]{2})/.match(wareki)
      @zero_year_wareki_s = "平成%d年%d月"%[$1, $2] if wareki[0] == 'H'
      @zero_year_wareki_s = "昭和%d年%d月"%[$1, $2] if wareki[0] == 'S'
    end
    @limit_wareki_s = wareki_kanji_format(limit.jisx0301) if !limit.blank?
    @limit2_wareki_s = wareki_kanji_format(limit2.jisx0301) if !limit2.blank?
  end

private
    def touroku_date_cannot_a_future_date
      return if touroku_date.blank?
      # touroku_date(日付)に日付以外を入れると nil になるので、ここの実装はまぁ動かないだろう。。
      if !touroku_date.kind_of?(Date) then
        errors.add :touroku_date, "type is not a date."
        return
      end
      if (Date.today <=> touroku_date) < 0 then
        errors.add :touroku_date, "can't be in the future."
      end
    end
    def zero_year_cannot_a_future_date
      return if zero_year.blank?
      # zero_year(日付)に日付以外を入れると nil になるので、ここの実装はまぁ動かないだろう。。
      if !zero_year.kind_of?(Date) then
        errors.add :zero_year, "type is not a date."
        return
      end
      if (Date.today <=> zero_year) < 0 then
        errors.add :zero_year, "can't be in the future."
      end
    end

    def limit_must_be_date
      return if limit.blank?
      # limit(日付)に日付以外を入れると nil になるので、ここの実装にはまぁこない。。
      errors.add :limit, "limit must be a Date type." if !limit.kind_of?(Date)
    end
    def limit2_must_be_date
      return if limit2.blank?
      # limit2(日付)に日付以外を入れると nil になるので、ここの実装にはまぁこない。。
      errors.add :limit2, "limit2 must be a Date type." if !limit2.kind_of?(Date)
    end

    def wareki_kanji_format(str)
      /^[a-zA-Z]([0-9]{2})\.([0-9]{2})\.([0-9]{2})/.match(str)
      return "平成%d年%d月%d日"%[$1, $2, $3] if str[0] == 'H'
      return "昭和%d年%d月%d日"%[$1, $2, $3] if str[0] == 'S'
    end
end
