module Makit
  class DotNet
    def self.is_installed?
      File.exist?(Makit::Environment.which("dotnet"))
    end

    def self.version
      `dotnet --version`
    end

    def self.new_project(template, name, output)
      if Dir.exist? output
        #puts "  #{output}".colorize(:green) + " exists.".colorize(:grey)
        #puts "Directory #{output} already exists".colorize(:yellow)
      else
        "dotnet new #{template} --name #{name} --output #{output}".run
      end
    end

    def self.add_package(project_path, package_name)
      project_content = File.read(project_path)
      if (!project_content.include?("\"#{package_name}\""))
        "dotnet add #{project_path} package #{package_name}".run
      else
        #puts "  package ".colorize(:grey) + "#{package_name}".colorize(:yellow) + " is in ".colorize(:grey) + "#{project_path}".colorize(:yellow)
      end
    end

    def self.add_reference(project_path, reference_path)
      project_content = File.read(project_path)
      if (project_content.include?("<PackageReference Include=\"#{package}\""))
        #puts "  reference ".colorize(:grey) + "#{reference_path}".colorize(:yellow) + " is in ".colorize(:grey) + "#{project_path}".colorize(:yellow)
      else
        "dotnet add #{project_path} reference #{reference_path}".run
      end
    end

    def self.new_solution(name)
      if File.exist? "#{name}.sln"
        #puts "Solution #{name}.sln already exists".colorize(:yellow)
        #puts "  #{name}.sln".colorize(:green) + " exists".colorize(:grey)
      else
        "dotnet new sln --name #{name}".run
      end
    end

    def self.build(project_path, configuration = "Release", output = "artifacts")
      project_dir = File.dirname(project_path)
      #puts "project_dir: #{project_dir}"
      newest_file = Makit::Directory::get_newest_file(project_dir)
      command_request = Makit::RUNNER::parse_command_request("dotnet build #{project_path} --configuration #{configuration} --output #{output}")
      if newest_file.nil?
        #puts "newest_git_file is nil, assigning a default"
        # assign a timestamp of now
        newest_file_date = Time.now
        RUNNER.cache_run(command_request, newest_file_date)
      else
        newest_file_date = File.mtime(newest_file)
        #puts "newest file: #{newest_file}"
        #puts "newest file date: #{newest_file_date}"
        RUNNER.cache_run(command_request, newest_file_date)
      end
    end

    def self.sln_add_projects(sln_name)
      if !File.exist? "#{sln_name}.sln"
        raise "Solution #{sln_name}.sln does not exist"
      else
        sln_path = "#{sln_name}.sln"
        sln_content = File.read(sln_path)
        Dir.glob("#{sln_path}/**/*.csproj").each do |project_path|
          project_name = File.basename(project_path, ".csproj")
          if (sln_content.include?("\"#{project_name}\""))
            puts "  #{project_name}".colorize(:green) + " is in ".colorize(:grey) + "#{sln_path}".colorize(:green)
            #puts "Project #{project_name} already exists in #{sln_path}".colorize(:yellow)
          else
            "dotnet sln #{sln_path} add #{project_path}".run
          end
        end
      end
    end
  end # class DotNet
end # module Makit