#!/usr/bin/env ruby
# coding: utf-8

require 'cgi'
require_relative 'msd'

cgi = CGI.new(:accept_charset => "UTF-8")
puts "Content-type: text/html\n\n"

genre = cgi['genre']
mood = cgi['mood']
year_start = cgi['year_start']
year_end = cgi['year_end']

results = MUSIC_DATA.select do |song|
  (genre.empty? || song[:genre] == genre) &&
    (mood.empty? || song[:mood] == mood) &&
    (year_start.empty? || song[:year].to_i >= year_start.to_i) &&
    (year_end.empty? || song[:year].to_i <= year_end.to_i)
end

puts <<-HTML
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>検索結果</title>
    <link rel="stylesheet" href="mss.css">
</head>
<body>
<h1>音楽検索結果</h1>
HTML

if results.empty?
  puts "<p>すみません。該当する曲が見つかりませんでした。</p>"
else
  puts "<h2>検索結果</h2>"
  puts "<ul>"
  results.each do |song|
    puts "<li>#{song[:title]} - #{song[:artist]} (#{song[:year]})</li>"
  end
  puts "</ul>"
end

puts <<-HTML
<br>
<a href="mss.html">戻る</a>
</body>
</html>
HTML
