# encoding:utf-8
class ScaleUser < ActiveRecord::Base
  has_paper_trail
  self.table_name = "scale_users"
  belongs_to :wx_user
  has_many :scale_results, :foreign_key => :user_id, :dependent => :destroy
  attr_accessor :v_avatar

  #after_create :update_paied_time
  SEX_ENUM = [["男",0],["女",1]]

  # 国家选项
  COUNTRY_ENUM = [
    ["中国", "china"],
    ["美国", "usa"],
    ["日本", "japan"],
    ["韩国", "korea"],
    ["马来西亚", "malaysia"],
    ["其他", "other"]
  ]

  # 种族选项
  RACE_ENUM = [
    ["亚洲人", "asian"],
    ["白人", "caucasian"],
    ["黑人", "black"],
    ["拉丁裔", "latino"],
    ["其他", "other"]
  ]

  # 职业选项
  OCCUPATION_ENUM = [
    ["IT/程序员", "it_programmer"],
    ["教师", "teacher"],
    ["医生", "doctor"],
    ["学生", "student"],
    ["自由职业", "freelancer"],
    ["退休", "retired"],
    ["其他", "other"]
  ]

  def wx_user_contact
    return "-" if wx_user.blank?
    user = wx_user.user
    return "-" if user.blank?
    contact = user.tel
    contact = user.email if contact.blank?
    contact.blank? ? "-" : contact
  end

  def get_avatar_img
    return "https://x-encrpt-bucket.s3.ap-southeast-2.amazonaws.com/encript/1766455493300.jpeg" if self.avatar.blank?
    return self.avatar if self.avatar.to_s.start_with?('http')
    url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.avatar}"
    return url
  end

  # 获取国家显示名称
  def country_label
    COUNTRY_ENUM.find { |label, value| value == country }&.first || country || "-"
  end

  # 获取种族显示名称
  def race_label
    RACE_ENUM.find { |label, value| value == race }&.first || race || "-"
  end

  # 获取职业显示名称
  def occupation_label
    OCCUPATION_ENUM.find { |label, value| value == occupation }&.first || occupation || "-"
  end

  rails_admin do
    navigation_label '体脂秤用户'
    parent ScaleDevice
    weight 1

    list do
      filters [:wx_user,:created_at]
      field :id
      field :wx_user_id
      field :wx_user do
        pretty_value do
          bindings[:object].wx_user_contact
        end
      end
      field :nick_name
      field :avatar do
        formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_avatar_img,
                    :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
                    :onClick => "javascript:window.open('#{bindings[:object].get_avatar_img}')"})
        end
      end
      field :sex, :enum do
        filterable true
        enum do
          SEX_ENUM
        end
      end
      field :height
      field :age
      field :target_weight
      field :birthday
      field :country do
        pretty_value do
          bindings[:object].country_label
        end
      end
      field :race do
        pretty_value do
          bindings[:object].race_label
        end
      end
      field :occupation do
        pretty_value do
          bindings[:object].occupation_label
        end
      end
      field :created_at
      field :updated_at
    end

    show do
      field :wx_user_id
      field :wx_user do
        pretty_value do
          bindings[:object].wx_user_contact
        end
      end
      field :nick_name
      field :avatar do
        formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_avatar_img,
            :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
            :onClick => "javascript:window.open('#{bindings[:object].get_avatar_img}')"})
        end
      end
      field :sex, :enum do
        filterable true
        enum do
          SEX_ENUM
        end
      end
      field :height
      field :age
      field :target_weight
      field :birthday
      field :country do
        pretty_value do
          bindings[:object].country_label
        end
      end
      field :race do
        pretty_value do
          bindings[:object].race_label
        end
      end
      field :occupation do
        pretty_value do
          bindings[:object].occupation_label
        end
      end
      field :created_at
      field :updated_at
    end

    edit do
      # field :wx_user_id
      field :wx_user
      field :nick_name
      field :v_avatar, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_avatar_img, :class => 'preview'})
        end
      end
      field :sex, :enum do
        filterable true
        enum do
          SEX_ENUM
        end
      end
      field :height
      field :age
      field :target_weight
      field :birthday
      field :country, :enum do
        enum do
          COUNTRY_ENUM
        end
      end
      field :race, :enum do
        enum do
          RACE_ENUM
        end
      end
      field :occupation, :enum do
        enum do
          OCCUPATION_ENUM
        end
      end
      field :created_at
      field :updated_at
    end
  end

  def v_avatar=file
    unless file.blank?
      clear_avatar_img
      file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
      file_path = "#{IMG_STORE_PATH}/#{file_name}"
      Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
      self.head = file_path
      self.save
    end
  end

  def clear_avatar_img
    file_path = "#{self.head}"
    Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  end

end
