#!/usr/bin/env ruby
# frozen_string_literal: true

require "optparse"
require_relative "../lib/fusuma/plugin/appmatcher"

option = {}
opt = OptionParser.new

opt.on("-l", "--list-applications",
  "List applications") do |v|
  option[:list] = v
end

opt.on("--version", "Show version") do |v|
  option[:version] = v
end

opt.on("--install-gnome-extension",
  "Install GNOME extension for appmatcher") do |v|
  option[:install_gnome_extension] = v
end

opt.on("--uninstall-gnome-extension",
  "Uninstall GNOME extension for appmatcher") do |v|
  option[:uninstall_gnome_extension] = v
end

opt.parse!(ARGV)

if option[:install_gnome_extension]
  require_relative "../lib/fusuma/plugin/appmatcher/gnome_extensions/installer"
  Fusuma::Plugin::Appmatcher::GnomeExtensions::Installer.new.install
  return
end

if option[:uninstall_gnome_extension]
  require_relative "../lib/fusuma/plugin/appmatcher/gnome_extensions/installer"
  Fusuma::Plugin::Appmatcher::GnomeExtensions::Installer.new.uninstall
  return
end

if option[:list]
  matcher = Fusuma::Plugin::Appmatcher.backend_klass::Matcher.new
  puts matcher.running_applications
  return
end

if option[:version]
  puts Fusuma::Plugin::Appmatcher::VERSION
  puts "XDG_CURRENT_DESKTOP: #{ENV.fetch("XDG_CURRENT_DESKTOP", nil)}"
  puts "XDG_SESSION_TYPE: #{ENV.fetch("XDG_SESSION_TYPE", nil)}"
  puts "Backend: #{Fusuma::Plugin::Appmatcher.backend_klass}"
  return
end

matcher = Fusuma::Plugin::Appmatcher.backend_klass::Matcher.new
puts matcher.active_application
