require 'csv'

namespace :mobile do
  task :jam_tracks_json => :environment do |task, args|
    fname = 'all-jamkazam-jamtracks.csv'
    tmp_fname = "/tmp/#{fname}"

    File.delete(tmp_fname) if File.exists?(tmp_fname)

    unless File.exists?(tmp_fname)
      cmd = "curl -s https://s3.amazonaws.com/jamkazam-public/public/lists/#{fname} > #{tmp_fname}"
      `#{cmd}`
    end

    unless File.exists?(tmp_fname)
      $stderr.puts "*** ERROR: download failed:  `#{cmd}`"
      return
    end

    section = {
        '#': [],
        A: [],
        B: [],
        C: [],
        D: [],
        E: [],
        F: [],
        G: [],
        H: [],
        I: [],
        J: [],
        K: [],
        L: [],
        M: [],
        N: [],
        O: [],
        P: [],
        Q: [],
        R: [],
        S: [],
        T: [],
        U: [],
        V: [],
        W: [],
        X: [],
        Y: [],
        Z: [],
    }
    jtx_json = {
      jtx_data_map: {},
      songs_sections: {
        '#': [],
        A: [],
        B: [],
        C: [],
        D: [],
        E: [],
        F: [],
        G: [],
        H: [],
        I: [],
        J: [],
        K: [],
        L: [],
        M: [],
        N: [],
        O: [],
        P: [],
        Q: [],
        R: [],
        S: [],
        T: [],
        U: [],
        V: [],
        W: [],
        X: [],
        Y: [],
        Z: [],
      },
      artists_sections: {
        '#': {},
        A: {},
        B: {},
        C: {},
        D: {},
        E: {},
        F: {},
        G: {},
        H: {},
        I: {},
        J: {},
        K: {},
        L: {},
        M: {},
        N: {},
        O: {},
        P: {},
        Q: {},
        R: {},
        S: {},
        T: {},
        U: {},
        V: {},
        W: {},
        X: {},
        Y: {},
        Z: {},
      }
    }

    by_artist = {}
    CSV.foreach(tmp_fname) do |row|
      jtartist = row[0]
      jtname = row[1]
      jt = JamTrack.
           where(name: jtname, original_artist: jtartist).
           includes([:genres, { jam_track_tracks: :instrument }]).
           limit(1).
           first
      next if jt.blank?

      jtdata = {
        artist: jtartist,
        genre: jt.genres.map(&:description).join(', '),
        instruments: jt.jam_track_tracks.map(&:instrument).compact.uniq.map(&:description).join(', '),
        language: jt.language,
        version: jt.version,
        price: jt.price,
        id: jt.id,
        key: jt.id,
        name: jtname,
        plan_code: jt.plan_code,
        year: jt.year
      }
      jtx_json[:jtx_data_map][jt.id] = jtdata

      first_char = jtartist[0].upcase
      idx = ((1..9) === first_char.to_i || '0' == first_char) ? :'#' : first_char.to_sym
      artists = jtx_json[:artists_sections][idx]
      unless artists[jtartist]
        artists[jtartist] = [jt.id]
      else
        artists[jtartist] << jt.id 
      end
      
      first_char = jtname.sub('(','')[0].upcase
      idx = ((1..9) === first_char.to_i || '0' == first_char) ? :'#' : first_char.to_sym
      jtx_json[:songs_sections][idx] << jt.id
    end

    jtx_json[:artists_section_idx] = jtx_json[:artists_sections].keys.collect do |key|
      { key: key, data: [], count: jtx_json[:artists_sections][key].count }
    end
    jtx_json[:songs_section_idx] = jtx_json[:songs_sections].keys.collect do |key|
      { key: key, data: [], count: jtx_json[:songs_sections][key].count }
    end

    File.open("#{Rails.root}/tmp/jtx_indices.json", 'w') do |ff|
      ff.write jtx_json.to_json
    end
    
  end # task
end # namespace

