#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../config/environment.rb'
require 'optparse'

I18n.locale = :en

options = {}
options[:invisible] = false
options[:hidden] = false
options[:access_restrictions] = 'HD'
errors = false

OptionParser.new do |opts|
  opts.banner = "
  use quotes if needed (spaces, \#{:creator})
  Usage: add_provider [options]
  Visible is set as default (agency is seen by and sees the other agencies)
  Access restriction for clients is set as HD by default
  "

  opts.on("--name string", String, "!mandatory! provider name") do |l|
    options[:name] = l
  end

  opts.on("--pp_name string", String, "provider name for stats") do |l|
    options[:pp_name] = l
  end

  opts.on("--string_key string", String, "!mandatory! provider string_key like on MS") do |l|
    options[:string_key] = l
  end

  opts.on("-no_copyright_symbol", String, "Add this option to set copyright_symbol to false (is true by default), to not add copyright symbol at the beginning of normalized_credit field (credit PP)") do |l|
    options[:copyright_symbol] = false
  end

  opts.on("--copyright_rule \"\#{:creator}\"", String, "!mandatory! provider copyright rule be careful with the syntax") do |l|
    options[:copyright_rule] = l
  end

  opts.on("--logo \"/absolute/path/to/logo.jpg\"", String, "provider logo full path, automatically renamed") do |l|
    options[:logo] = l
  end

  opts.on("--description string", String, "provider description") do |l|
    options[:description] = l
  end

  opts.on("--days_keep number", Integer, "Provider files will be erased after this number of days") do |l|
    options[:days_keep] = l
  end

  opts.on("--days_keep_per_picture", "If set and days keep is filled files coming in in_modif will not be erased") do |l|
    options[:days_keep_per_picture] = l
  end

  opts.on("--provider_conditions string", String, "Provider conditions shown in a pop before download") do |l|
    options[:provider_conditions] = l
  end

  opts.on("--site string", String, "provider website url") do |l|
    options[:site] = l
  end

  opts.on("--address string", String, "provider address") do |l|
    options[:address] = l
  end

  opts.on("--city string", String, "provider city") do |l|
    options[:city] = l
  end

  opts.on("--zip_code string", String, "provide zip code") do |l|
    options[:zip_code] = l
  end

  opts.on("--country string", String, "provider country") do |l|
    options[:country] = l
  end

  opts.on("--cgv string", String, "provider CGV") do |l|
    options[:cgv] = l
  end

  opts.on("-inv", "Add if agency is not visible to other agencies") do |l|
    options[:invisible] = true
  end

  opts.on("-hid", "Add if agency is hidden") do |l|
    options[:hidden] = true
  end

  opts.on("--access_restrictions string", "Select an access restriction level (HD,BD,PE or VG). HD is set as default") do |l|
    options[:access_restrictions] = l
  end


end.parse!

if options[:name].blank?
  puts 'error : name is mandatory, use -h or --help for help'
  errors = true
end

if options[:string_key].blank?
  puts 'error : string_key is mandatory, use -h or --help for help'
  errors = true
end

if options[:copyright_rule].blank?
  puts 'error : copyright_rule is mandatory, use -h or --help for help'
  errors = true
end

if options[:country].blank?
  puts 'error : country is mandatory, use -h or --help for help'
  errors = true
end

unless options[:access_restrictions].blank?
  case options[:access_restrictions]
  when 'HD'
    restrictions = [1, 2, 3]
  when 'BD'
    restrictions = [1, 3]
  when 'PE'
    restrictions = [3]
  when 'VG'
    restrictions = []
  else
    puts 'error : access restriction unknown, use -h or --help for help'
    errors = true
  end
end

exit if errors

unless Server.type_is?('PIXTECH') || Server.itself?(VINGTMINUTES_SERVER_NAME)
  p = Provider.create(
    :name =>                  options[:name],
    :pp_name =>               options[:pp_name],
    :string_key =>            options[:string_key],
    :description =>           options[:description],
    :site =>                  options[:site],
    :address =>               options[:address],
    :copyright_rule =>        options[:copyright_rule],
    :days_keep =>             options[:days_keep],
    :days_keep_per_picture => options[:days_keep_per_picture],
    :toomany_limit =>         options[:toomany_limit],
    :provider_conditions =>   options[:provider_conditions],
    :city =>                  options[:city],
    :country =>               options[:country],
    :zip_code =>              options[:zip_code],
    :cgv =>                   options[:cgv],
    :visible =>               !options[:invisible]
  )
  unless p.errors.empty?
    p.errors.full_messages.each{|e| puts "error : #{e.to_s}"}
    exit
  end

  p.update(copyright_symbol: false) if options[:copyright_symbol] == false

  if options[:hidden]
    puts "Agency hidden : adding provider to 'toutes' group only, with all authorizations"
    TitleProviderGroupName.where('name = "toutes"').each{|tpgn| tpgn.add_provider({authorizations: [1, 2, 3], id: p.id})}
    p.update_attributes(actif: 0)
  else
    if (Server.itself? PTREF_SERVER_NAME)
      puts "Adding provider to 'toutes' group only, with all authorizations"
      TitleProviderGroupName.where('name = "toutes"').each{|tpgn| tpgn.add_provider({authorizations: [1, 2, 3], id: p.id})}
      p.update_attributes(actif: 0,local: 1)
    else
      unless options[:invisible]
        puts "Adding provider to visible agencies groups without authorization"
        TitleProviderGroupName.where('name like "1_%"').each{|tpgn| tpgn.add_provider({id: p.id})}
      end
      if restrictions.blank?
        puts "Adding provider to clients groups without authorization"
        TitleProviderGroupName.where('name not like "0_%" and name not like "1_%"').each{|tpgn| tpgn.add_provider({id: p.id})}
      else
        if Server.itself? PP2_SERVER_NAME
          puts "Adding provider to clients groups with PE authorization (on PP2 servers)"
          TitleProviderGroupName.where('name not like "0_%" and name not like "1_%"').each{|tpgn| tpgn.add_provider({authorizations: [3], id: p.id})}
        else
          puts "Adding provider to clients groups with #{options[:access_restrictions]} authorization"
          TitleProviderGroupName.where('name not like "0_%" and name not like "1_%"').each{|tpgn| tpgn.add_provider({authorizations: restrictions, id: p.id})}
        end
      end
    end
  end

  unless options[:logo].blank?
    puts 'error : unable to add logo' unless p.add_logo(File.basename(options[:logo]), options[:logo])
  end
end