class WcoGame::Location

def self.empty_export; Gameui::Map.new.empty_export; end

def self.empty_export; Gameui::Map.new.empty_export; end

def self.export_key_to_class

def self.export_key_to_class
  Map.new.export_key_to_class
end

def self.list conditions = { is_trash: false }

def self.list conditions = { is_trash: false }
  out = self.order_by( created_at: :desc )
  [[nil, nil]] + out.map { |item| [ item.name, item.id.to_s ] }
end

def breadcrumbs

def breadcrumbs
  out = [{ name: self.name, slug: self.slug, link: false }]
  p = self.parent
  while p
    out.push({ name: p.name, slug: p.slug })
    p = p.parent
  end
  out.reverse
end

def collect export_object

def collect export_object
  map = self
  export_object[:maps][map.id.to_s] = map.id.to_s
  if map.markers.present?
    map.markers.map do |marker|
      id = marker.id.to_s
      if !export_object[:markers][id]
        marker.collect( export_object )
      end
      export_object[:markers][id] = id
    end
  end
  if map.newsitems.present?
    map.newsitems.map do |newsitem|
      id = newsitem.id.to_s
      export_object[:newsitems][id] = id
      newsitem.collect export_object
    end
  end
  ## @TODO: maybe implement this later, maybe not. _vp_ 2022-03-12
  # if map.childs.present?
  #   export_object[:maps].push( map.childs.map &:id )
  #   map.childs.map do |child|
  #     child.collect export_object
  #   end
  # end
  if map.creator_profile.present?
    export_object[:profiles][map.creator_profile.id.to_s] = map.creator_profile.id.to_s
  end
  if map.image.present?
    export_object[:image_assets][map.image.id.to_s] = map.image.id.to_s
  end
  export_object
end

def compute_w_h

def compute_w_h
  return if !image ## @TODO: test this
  begin
    geo = Paperclip::Geometry.from_file(Paperclip.io_adapters.for(image.image))
    self.w = geo.width
    self.h = geo.height
  rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e
    puts! e, 'Could not #compute_w_h'
    # @TODO: do something with this
  end
end

def empty_export

def empty_export
  return {
    galleries: {},
    image_assets: {},
    maps: {}, markers: {},
    newsitems: {},
    photos: {}, profiles: {},
    reports: {},
    videos: {},
  }
end

def empty_export_arr

def empty_export_arr
  return {
    galleries: [],
    image_assets: [],
    maps: [], markers: [],
    newsitems: [],
    photos: [], profiles: [],
    reports: [],
    videos: [],
  }
end

def export_fields

def export_fields
  %w|
    creator_profile_id config
    deleted_at description
    h
    is_public
    labels
    map_slug
    name
    ordering_type
    parent_slug
    rated
    slug
    version
    w
  |
end

def export_key_to_class

def export_key_to_class
  return {
    galleries: 'Gallery',
    image_assets: 'Ish::ImageAsset',
    maps: 'Gameui::Map',
    markers: 'Gameui::Marker',
    newsitems: 'Newsitem',
    photos: 'Photo',
    profiles: 'Ish::UserProfile',
    reports: 'Report',
    videos: 'Video',
    # 'galleries' => 'Gallery',
    # 'image_assets' => 'Ish::ImageAsset',
    # 'maps' => 'Gameui::Map',
    # 'markers' => 'Gameui::Marker',
    # 'newsitems' => 'Newsitem',
    # 'photos' => 'Photo',
    # 'profiles' => 'Ish::UserProfile',
    # 'reports' => 'Report',
    # 'videos' => 'Video',
  }.with_indifferent_access
end

def export_subtree

#
# This is the starting point _vp_ 2022-03-12
def export_subtree
  collected = collect(empty_export)
  exportable = empty_export_arr
  collected.map do |k, v|
    if v.present?
      v.map do |id|
        id = id[0]
        item = export_key_to_class[k].constantize.unscoped.find id
        export = item.export
        exportable[k].push( export )
      end
    end
  end
  JSON.pretty_generate exportable
end

def map

def map
  ::Gameui::Map.where( slug: map_slug ).first
end