class SwitchPortConfiguration < ActiveRecord::Base
  belongs_to :switch_configuration
  include PublicActivity::Model

  SPEED_OPTIONS = {0  => "Auto Negotiate",1  => "2.5 Gigabit Full Duplex",2  => "1 Gigabit Full Duplex",3  => "100 Megabit Full Duplex",4  => "100 Megabit Half Duplex",5  => "10 Megabit Full Duplex",6  => "10 Megabit Half Duplex"}
  LINK_NEGOTIATION_OPTIONS = {"auto_negotiate" => "Auto Negotiate"}
  DUPLEX_MAP = {0=>"full",1=>"full", 2=>"full", 3=>"full", 4=>"half", 5=>"full", 6=>"half"}
  SPEED_MAP = {0=>"2500",1=>"2500", 2=>"1000", 3=>"100", 4=>"100", 5=>"10", 6=>"10"}

  tracked owner: ->(controller, model) { controller && controller.tracked_current_user },params: { :attributes => proc {|controller, model_instance| {"SwitchPortConfig(#{model_instance.name})" => model_instance.changes}}},organisation_id: ->(controller, model) { (controller && controller.tracked_current_user) ? controller.tracked_current_user.organisation_id : model.organisation_id},:location_network_id => proc {|controller, model_instance| model_instance.switch_configuration.location_network_id}

  tracked assumed_by: proc {|controller, model| controller.user_assumed_by if controller}

  def json_build
    port_config = self.attributes.except('created_at', 'updated_at', 'link_negotiation', 'port_isolation', 'rstp', 'acl_mac', 'mac_filter_policy', 'acl_mac_list', 'clone_id')
    port_config['speed_text'] = SPEED_OPTIONS[port_config['speed']]
    # port_config['link_negotiation'] = LINK_NEGOTIATION_OPTIONS[port_config['link_negotiation']]

    port_config
  end

  def is_poe_port?
    return false if [9,10].include?self.port_number
    return true
  end

  def port_attributes
    switch_attributes = {"duplex" => DUPLEX_MAP[self.speed],"enabled"=> self.try(:port_status),"speed"=>SPEED_MAP[self.speed].to_i, "select-ports"=> ["Ethernet#{(self.port_number - 1).to_s}"]}
    switch_attributes["poe-port"] = {"admin-mode" => self.try(:poe), "power-limit" => self.power} if self.is_poe_port?
    return switch_attributes
  end

  def old_interface_vlan_hash j
     self.vlan.split(",").each do |vl|
       j[vl.to_i] = (j[vl.to_i] || []) << [self.try(:port_number), self.port_type, false]
       j[vl.to_i] << [self.try(:port_number), self.port_type, true] if self.port_type.eql?("access")
     end
     j[self.native_vlan] = ((j[self.native_vlan] || []) << [self.try(:port_number), self.port_type, true]) unless self.port_type.eql?("access")
  end

  def interface_vlan_hash j
    vlans = self.vlan.split(",")
    #vlans += (self.switch_configuration.sw_mac_oui||[]).select {|d| d['vlanid'].present? }.map {|s| s['vlanid'].to_s }.uniq.compact if self.port_type.eql?("access")
    vlans.uniq.each do |vl|
      j[vl.to_i] = (j[vl.to_i] || []) << [self.try(:port_number), self.port_type, false]
      j[vl.to_i] << [self.try(:port_number), self.port_type, true] if self.port_type.eql?("access")
    end
    vlans_oui = (self.switch_configuration.sw_mac_oui||[]).select {|d| d['vlanid'].present? }.map {|s| s['vlanid'].to_s }.uniq.compact if self.port_type.eql?("access")
    vlans_oui.uniq.each { |vl| j[vl.to_i] = (j[vl.to_i] || []) << [self.try(:port_number), self.port_type, false] } unless vlans_oui.blank?
    j[self.native_vlan] = ((j[self.native_vlan] || []) << [self.try(:port_number), self.port_type, true]) unless self.port_type.eql?("access")
  end

end



