require 'rails/generators/active_record'
require 'dunlop/generators'
class Dunlop::Install::BaseGenerator < Rails::Generators::Base
  include Rails::Generators::Migration
  include Dunlop::Generators::GeneratorHelpers
  source_root File.expand_path('../templates', __FILE__)

  def generate_all_dependencies
    generate "devise User" unless Rails.root.join('app/models/user.rb').exist?
  end

  def setup_user_model
    copy_file "CHANGELOG.md", "CHANGELOG.md"
    generate "devise:install" unless File.exist?(Rails.root.join("config/initializers/devise.rb"))
    generate "devise User" unless File.exist?(Rails.root.join("app/models/user.rb"))
    #generate "simple_form:install --foundation"
    generate "rspec:install" unless File.exist?(Rails.root.join('spec/rails_helper.rb'))
    migration_template "migrations/add_admin_and_role_names_to_users.rb", "db/migrate/add_admin_and_role_names_to_users.rb"
    user_model = Rails.root.join('app/models/user.rb')
    if Rails.env != 'test' and File.exist?(user_model) # Test unlink does not work with special test destination target
      File.unlink user_model
    end
    copy_file "models/user.rb", "app/models/user.rb"
  end

  def create_basics
    template "controllers/application_controller.rb", "app/controllers/application_controller.rb"
    copy_file "models/application_record.rb", "app/models/application_record.rb"
    copy_file "models/ability.rb", "app/models/ability.rb"
    copy_file "models/execution_batch.rb", "app/models/execution_batch.rb"
    directory "views/layouts", "app/views/layouts"
    route 'mount Dunlop::Engine => "/dunlop"'

    inject_into_file "config/application.rb", after: "class Application < Rails::Application\n" do <<-RUBY

    config.application_name = "#{Rails.application.class.name.deconstantize}"

    config.git_version = Rails.env
    config.git_version = File.read('REVISION').strip if File.exists?('REVISION')
    config.git_version = File.read('OFFLINE_REVISION').strip if File.exists?('OFFLINE_REVISION')

    RUBY
    end

    inject_into_file "config/environments/development.rb", after: "Rails.application.configure do\n" do <<-RUBY

    config.time_zone = 'Moscow'
    config.git_version = 'develop'

    RUBY
    end

    inject_into_file "config/environments/test.rb", after: "Rails.application.configure do\n" do <<-RUBY

    config.time_zone = 'Moscow'
    config.git_version = 'test'

    RUBY
    end

    original_application_layout = Rails.root.join('app/views/layouts/application.html.erb')
    File.unlink original_application_layout if File.exist?(original_application_layout)
  end

  def add_required_gems
    return unless Rails.root.join('Gemfile').exist?
    gem 'cancancan'
    gem 'kaminari'
    gem 'exception_notification'
    gem_group :assets do
      gem 'semantic-ui-sass'
    end

    gem_group :development, :test do
      gem 'pry-rails'
      gem 'pry-doc'
      gem 'rspec-rails'
      gem 'spring'
      gem 'spring-commands-rspec'
      gem 'factory_bot_rails'
    end

    gem_group :test do
      gem 'timecop'
      gem 'poltergeist'
      gem 'capybara-screenshot'
      gem 'database_cleaner'
    end
  end

  def create_migrations
    migration_template "migrations/create_dunlop_log_entries.rb", "db/migrate/create_dunlop_log_entries.rb"
    migration_template "migrations/add_admin_and_role_names_to_users.rb", "db/migrate/add_admin_and_role_names_to_users.rb"
    migration_template "migrations/create_dunlop_execution_batches.rb", "db/migrate/create_dunlop_execution_batches.rb"
    copy_file "migrations/seeds.rb", "db/seeds.rb"
  end

  def setup_assets
    js_manifest = Rails.root.join("app/assets/javascripts/application.js")
    if File.exist?(js_manifest)
      File.unlink js_manifest
      copy_file "assets/javascripts/application.coffee", "app/assets/javascripts/application.coffee"
    end

    css_manifest = Rails.root.join("app/assets/stylesheets/application.css")
    if File.exist?(css_manifest)
      File.unlink css_manifest
      copy_file "assets/stylesheets/application.sass", "app/assets/stylesheets/application.sass"
    end
  end

  def setup_dashboard_with_home
    return if File.directory?(Rails.root.join("app/views/dashboard"))
    return unless yes? "Setup dashboard controller with homepage?"
    copy_file "controllers/dashboard_controller.rb", "app/controllers/dashboard_controller.rb"
    route "root to: 'dashboard#home'"
    directory "views/dashboard", "app/views/dashboard"
    copy_file "rspec/home_spec.rb", "spec/features/home_spec.rb"
  end

  def create_rspec_base
    copy_file "rspec/dunlop_integrity_spec.rb", "spec/dunlop_integrity_spec.rb"
    template "rspec/rails_helper.rb", "spec/rails_helper.rb", force: true
    copy_file "rspec/users_factory.rb", "spec/factories/users.rb"
  end

  def add_exception_notification
    #template "initializers/exception_notification.rb.erb", "config/initializers/exception_notification.rb"
    directory 'initializers', 'config/initializers'
  end

  def email
    return @email if @email
    default = %x[git config user.email].strip
    return @email = "test@example.com" if Rails.env.test?
    @email = ask("What is the developer's email?", default: default)
  end

end
