# encoding:utf-8
require 'uuid'
class ShareMaterial < ActiveRecord::Base
  has_paper_trail
  self.table_name = "share_materials"
  before_save :before_save
  
  belongs_to :wx_user, :foreign_key => :wx_uid
  validates :type_id,presence: true
  attr_accessor :v_image_1, :v_image_2, :v_image_3, :v_image_4, :v_image_5, :v_image_6, :v_image_7, :v_image_8, :v_image_9
  attr_accessor :is_delete_image_1, :is_delete_image_2, :is_delete_image_3, :is_delete_image_4, :is_delete_image_5, :is_delete_image_6, :is_delete_image_7, :is_delete_image_8, :is_delete_image_9
  attr_accessor :image_ratio_1, :image_ratio_2, :image_ratio_3, :image_ratio_4, :image_ratio_5, :image_ratio_6, :image_ratio_7, :image_ratio_8, :image_ratio_9

  MTYPE_ENUM = [["商品","product"],["项目","project"]]
  def type_title 
    if m_type == "project"
      project = Project.where("id = ?", self.type_id).first
      if !project.blank?
        return project.title
      end
    elsif m_type == "product"
      product = Product.where("id = ?", self.type_id).first
      if !product.blank?
        return product.name
      end
    end 
    return "-"
  end
   
  IMG_STORE_PATH = "share_materials" 

  rails_admin do
    navigation_label '发圈素材管理'
    weight -240

    list do
      filters [:id]
      field :id
      field :wx_user
      field :m_type, :enum do
          enum do 
              MTYPE_ENUM
          end
      end
      field :type_title
      field :type_id
      field :image_1st do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(1),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(1)}')"
              }
            )
        end
      end
      field :image_2nd do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(2),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(2)}')"
              }
            )
        end
      end
      field :is_published
      field :created_at
    end

    show do
      field :id
      field :wx_user
      field :m_type, :enum do
          enum do 
              MTYPE_ENUM
          end
      end
      field :type_title
      field :type_id
      field :material_text
      field :image_1st do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(1),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(1)}')"
              }
            )
        end
      end
      field :image_2nd do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(2),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(2)}')"
              }
            )
        end
      end
      field :image_3rd do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(3),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(3)}')"
              }
            )
        end
      end
      field :image_4th do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(4),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(4)}')"
              }
            )
        end
      end
      field :image_5th do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(5),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(5)}')"
              }
            )
        end
      end
      field :image_6th do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(6),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(6)}')"
              }
            )
        end
      end
      field :image_7th do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(7),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(7)}')"
              }
            )
        end
      end
      field :image_8th do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(8),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(8)}')"
              }
            )
        end
      end
      field :image_9th do
        formatted_value do
          bindings[:view].tag(
              :img, 
              {
                  :src => bindings[:object].get_img(9),
                  :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
                  :onClick => "javascript:window.open('#{bindings[:object].get_img(9)}')"
              }
            )
        end
      end
      field :is_published
      field :created_at
      field :updated_at
    end

    edit do
      field :wx_uid
      field :m_type, :enum do
          enum do 
              MTYPE_ENUM
          end
      end
      field :type_id
      field :material_text
      field :v_image_1, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(1), :class => 'preview'})
        end
      end
      field :image_ratio_1, :integer
      field :is_delete_image_1, :boolean do
        visible do
          bindings[:object].image_1st != "" && bindings[:object].image_1st != nil
        end
      end
      field :v_image_2, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(2), :class => 'preview'})
        end
      end
      field :image_ratio_2, :integer
      field :is_delete_image_2, :boolean do
        visible do
          bindings[:object].image_2nd != "" && bindings[:object].image_2nd != nil
        end
      end
      field :v_image_3, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(3), :class => 'preview'})
        end
      end
      field :image_ratio_3, :integer
      field :is_delete_image_3, :boolean do
        visible do
          bindings[:object].image_3rd != "" && bindings[:object].image_3rd != nil
        end
      end
      field :v_image_4, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(4), :class => 'preview'})
        end
      end
      field :image_ratio_4, :integer
      field :is_delete_image_4, :boolean do
        visible do
          bindings[:object].image_4th != "" && bindings[:object].image_4th != nil
        end
      end
      field :v_image_5, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(5), :class => 'preview'})
        end
      end
      field :image_ratio_5, :integer
      field :is_delete_image_5, :boolean do
        visible do
          bindings[:object].image_5th != "" && bindings[:object].image_5th != nil
        end
      end
      field :v_image_6, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(6), :class => 'preview'})
        end
      end
      field :image_ratio_6, :integer
      field :is_delete_image_6, :boolean do
        visible do
          bindings[:object].image_6th != "" && bindings[:object].image_6th != nil
        end
      end
      field :v_image_7, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(7), :class => 'preview'})
        end
      end
      field :image_ratio_7, :integer
      field :is_delete_image_7, :boolean do
        visible do
          bindings[:object].image_7th != "" && bindings[:object].image_7th != nil
        end
      end
      field :v_image_8, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(8), :class => 'preview'})
        end
      end
      field :image_ratio_8, :integer
      field :is_delete_image_8, :boolean do
        visible do
          bindings[:object].image_8th != "" && bindings[:object].image_8th != nil
        end
      end
      field :v_image_9, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img(9), :class => 'preview'})
        end
      end
      field :image_ratio_9, :integer
      field :is_delete_image_9, :boolean do
        visible do
          bindings[:object].image_9th != "" && bindings[:object].image_9th != nil
        end
      end
      field :is_published
    end
  end


  
  after_destroy :after_destroy

  def v_image_1=file
    upload(file, 1)
  end
  def v_image_2=file
    upload(file, 2)
  end
  def v_image_3=file
    upload(file, 3)
  end
  def v_image_4=file
    upload(file, 4)
  end
  def v_image_5=file
    upload(file, 5)
  end
  def v_image_6=file
    upload(file, 6)
  end
  def v_image_7=file
    upload(file, 7)
  end
  def v_image_8=file
    upload(file, 8)
  end
  def v_image_9=file
    upload(file, 9)
  end

  # ?x-oss-process=image/quality,q_70 控制图片质量，CDN转换也可以用
  def upload(file, img_idx)
    unless file.blank?
      clear_img(img_idx)
      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)
      case img_idx
      when 1
        self.image_1st = file_path
      when 2
        self.image_2nd = file_path
      when 3
        self.image_3rd = file_path
      when 4
        self.image_4th = file_path
      when 5
        self.image_5th = file_path
      when 6
        self.image_6th = file_path
      when 7
        self.image_7th = file_path
      when 8
        self.image_8th = file_path
      when 9
        self.image_9th = file_path
      end
    end
  end

  def getImageFilePath(img_idx) 
    case img_idx
    when 1
      if self.image_1st != nil && self.image_1st != ""
        if self.image_ratio_1 != nil && self.image_ratio_1 !="" 
          if self.image_1st.include?"?x-oss-process=image/quality,q_"
            self.image_1st = self.image_1st[0, self.image_1st.rindex("_")+1] + self.image_ratio_1
          else
            self.image_1st = self.image_1st + "?x-oss-process=image/quality,q_" + self.image_ratio_1
          end
        else
          if !self.image_1st.include?"?x-oss-process=image/quality,q_"
            self.image_1st = self.image_1st + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 2
      if self.image_2nd != nil && self.image_2nd != ""
        if self.image_ratio_2 != nil && self.image_ratio_2 != "" 
          if self.image_2nd.include?"?x-oss-process=image/quality,q_"
            self.image_2nd = self.image_2nd[0, self.image_2nd.rindex("_")+1] + self.image_ratio_2
          else
            self.image_2nd = self.image_2nd + "?x-oss-process=image/quality,q_" + self.image_ratio_2
          end
        else
          if !self.image_2nd.include?"?x-oss-process=image/quality,q_"
            self.image_2nd = self.image_2nd + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 3
      if self.image_3rd != nil && self.image_3rd != ""
        if self.image_ratio_3 != nil && self.image_ratio_3 != "" 
          if self.image_3rd.include?"?x-oss-process=image/quality,q_"
            self.image_3rd = self.image_3rd[0, self.image_3rd.rindex("_")+1] + self.image_ratio_3
          else
          self.image_3rd = self.image_3rd + "?x-oss-process=image/quality,q_" + self.image_ratio_3
          end
        else
          if !self.image_3rd.include?"?x-oss-process=image/quality,q_"
            self.image_3rd = self.image_3rd + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 4
      if self.image_4th != nil && self.image_4th != ""
        if self.image_ratio_4 != nil && self.image_ratio_4 != "" 
          if self.image_4th.include?"?x-oss-process=image/quality,q_"
            self.image_4th = self.image_4th[0, self.image_4th.rindex("_")+1] + self.image_ratio_4
          else
          self.image_4th = self.image_4th + "?x-oss-process=image/quality,q_" + self.image_ratio_4
          end
        else
          if !self.image_4th.include?"?x-oss-process=image/quality,q_"
            self.image_4th = self.image_4th + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 5
      if self.image_5th != nil && self.image_5th != ""
        if self.image_ratio_5 != nil && self.image_ratio_5 != "" 
          if self.image_5th.include?"?x-oss-process=image/quality,q_"
            self.image_5th = self.image_5th[0, self.image_5th.rindex("_")+1] + self.image_ratio_5
          else
          self.image_5th = self.image_5th + "?x-oss-process=image/quality,q_" + self.image_ratio_5
          end
        else
          if !self.image_5th.include?"?x-oss-process=image/quality,q_"
            self.image_5th = self.image_5th + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 6
      if self.image_6th != nil && self.image_6th != ""
        if self.image_ratio_6 != nil && self.image_ratio_6 != "" 
          if self.image_6th.include?"?x-oss-process=image/quality,q_"
            self.image_6th = self.image_6th[0, self.image_6th.rindex("_")+1] + self.image_ratio_6
          else
          self.image_6th = self.image_6th + "?x-oss-process=image/quality,q_" + self.image_ratio_6
          end
        else
          if !self.image_6th.include?"?x-oss-process=image/quality,q_"
            self.image_6th = self.image_6th + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 7
      if self.image_7th != nil && self.image_7th != ""
        if self.image_ratio_7 != nil && self.image_ratio_7 != ""
          if self.image_7th.include?"?x-oss-process=image/quality,q_"
            self.image_7th = self.image_7th[0, self.image_7th.rindex("_")+1] + self.image_ratio_7
          else
          self.image_7th = self.image_7th + "?x-oss-process=image/quality,q_" + self.image_ratio_7
          end
        else
          if !self.image_7th.include?"?x-oss-process=image/quality,q_"
            self.image_7th = self.image_7th + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 8
      if self.image_8th != nil && self.image_8th != ""
        if self.image_ratio_8 != nil && self.image_ratio_8 != ""
          if self.image_8th.include?"?x-oss-process=image/quality,q_"
            self.image_8th = self.image_8th[0, self.image_8th.rindex("_")+1] + self.image_ratio_8
          else
          self.image_8th = self.image_8th + "?x-oss-process=image/quality,q_" + self.image_ratio_8
          end
        else
          if !self.image_8th.include?"?x-oss-process=image/quality,q_"
            self.image_8th = self.image_8th + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    when 9
      if self.image_9th != nil && self.image_9th != ""
        if self.image_ratio_9 != nil && self.image_ratio_9 != ""
          if self.image_9th.include?"?x-oss-process=image/quality,q_"
            self.image_9th = self.image_9th[0, self.image_9th.rindex("_")+1] + self.image_ratio_9
          else
          self.image_9th = self.image_9th + "?x-oss-process=image/quality,q_" + self.image_ratio_9
          end
        else
          if !self.image_9th.include?"?x-oss-process=image/quality,q_"
            self.image_9th = self.image_9th + "?x-oss-process=image/quality,q_100"
          end
        end
      end
    end
  end

  def get_img(img_idx)
    store_name = ""
    case img_idx
    when 1
      store_name = self.image_1st
    when 2
      store_name = self.image_2nd
    when 3
      store_name = self.image_3rd
    when 4
      store_name = self.image_4th
    when 5
      store_name = self.image_5th
    when 6
      store_name = self.image_6th
    when 7
      store_name = self.image_7th
    when 8
      store_name = self.image_8th
    when 9
      store_name = self.image_9th     
    end
    if store_name != "" && store_name != nil 
      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{store_name}"
      return url
    end
    return ""
  end

  def clear_img(img_idx)
    file_path = ""
    case img_idx
    when 1
      file_path = "#{self.image_1st}"
    when 2
      file_path = "#{self.image_2nd}"
    when 3
      file_path = "#{self.image_3rd}"
    when 4
      file_path = "#{self.image_4th}"
    when 5
      file_path = "#{self.image_5th}"
    when 6
      file_path = "#{self.image_6th}"
    when 7
      file_path = "#{self.image_7th}"
    when 8
      file_path = "#{self.image_8th}"
    when 9
      file_path = "#{self.image_9th}"
    end
    Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  end

  def after_destroy
    for i in 1..9
      clear_img(i)
    end
  end

  def is_delete_img(img_idx, is_delete)
    case img_idx
    when 1
      if is_delete == 1
        clear_img(1)
        self.image_1st = ""
      end
    when 2
      if is_delete == 1
        clear_img(2)
        self.image_2nd = ""
      end
    when 3
      if is_delete == 1
        clear_img(3)
        self.image_3rd = ""
      end
    when 4
      if is_delete == 1
        clear_img(4)
        self.image_4th = ""
      end
    when 5
      if is_delete == 1
        clear_img(5)
        self.image_5th = ""
      end
    when 6
      if is_delete == 1
        clear_img(6)
        self.image_6th = ""
      end
    when 7
      if is_delete == 1
        clear_img(7)
        self.image_7th = ""
      end
    when 8  
      if is_delete == 1
        clear_img(8)
        self.image_8th = ""
      end
    when 9 
      if is_delete == 1
        clear_img(9)
        self.image_9th = ""
      end
    end
  end

  def get_is_delete(img_idx)
    case img_idx
    when 1
      return self.is_delete_image_1.to_i
    when 2
      return self.is_delete_image_2.to_i
    when 3
      return self.is_delete_image_3.to_i
    when 4
      return self.is_delete_image_4.to_i
    when 5
      return self.is_delete_image_5.to_i
    when 6
      return self.is_delete_image_6.to_i
    when 7
      return self.is_delete_image_7.to_i
    when 8
      return self.is_delete_image_8.to_i
    when 9
      return self.is_delete_image_9.to_i
    end
  end

  def before_save
    for i in 1..9
      is_delete_img(i, get_is_delete(i))
    end
    for i in 1..9
      getImageFilePath(i)
    end
  end

end
