#!/usr/bin/wish -f

#######
# GUI #
#######

wm title . "Movie Info"

frame .info

frame .info.labels
label .info.labels.title -text "Title:"
label .info.labels.year -text "Year:"
label .info.labels.director -text "Director:"
label .info.labels.actors -text "Actors:"
label .info.labels.duration -text "Duration:"

frame .info.answers
label .info.answers.title -textvariable title
label .info.answers.year -textvariable year
label .info.answers.director -textvariable director
label .info.answers.actors -textvariable actors
label .info.answers.duration -textvariable duration

pack .info.labels.title .info.labels.year .info.labels.director \
    .info.labels.actors .info.labels.duration -anchor w
pack .info.answers.title .info.answers.year .info.answers.director \
    .info.answers.actors .info.answers.duration -anchor w
pack .info.labels .info.answers -side left

frame .select
listbox .select.list -width 50 -yscrollcommand { .select.scroll set }
scrollbar .select.scroll -command { .select.list yview }
pack .select.list .select.scroll -side left -fill both

label .state -textvariable state

frame .buttons
button .buttons.select -text Select -command select -default disabled
button .buttons.next -text Next -command next
button .buttons.quit -text Quit -command exit
pack .buttons.select .buttons.next .buttons.quit -side left -padx 30 -fill x

pack .info .select .state .buttons -fill x

bind .select.list <<ListboxSelect>> getinfo
bind . n next
bind . s select
bind . q exit

##############
# Procedures #
##############

proc select {} {
    global title year director actors duration state
    puts $title*$year*$director*$actors*$duration
    set state "Information written."
}

proc get_year {page} {
    set tmp [regexp -inline -- {<title>.*?\(.*?\)} $page]
    set len [string length $tmp]
    return [string range $tmp [expr $len - 6] [expr $len - 3]]
}

proc get_director {page} {
    set tmp [regexp -inline -- {Director.*?/name/nm.*?</a>} $page]
    set tmp [regsub -- {^.*?<a.*?>} $tmp ""]
    return [regsub -- {</a.*$} $tmp ""]
}

proc get_actors {page} {
    set tmp [string range $page [string first "headerinline" $page] end]
    set tmp [regexp -all -inline -- {\"/name/nm.*?/a} $tmp]
    set tmp [lrange $tmp 0 2]
    foreach actor $tmp {
        set name [regsub -- {.*>} $actor ""]
        set name [regsub -- {<.*} $name ""]
        append result $name ", "
    }
    return [string trimright $result ", "]
}

proc get_duration {page} {
    set tmp [regexp -inline -- {Runtime.*?>.*?min} $page]
    set tmp [regsub -- {^.*?>[^0-9]*} $tmp ""]
    return [regsub -all -- {[^0-9]} $tmp ""]
}

proc getinfo {} {
    global state htmls year director actors duration state
    set state "Accessing IMDB Database..."
    update
    set imdb [lindex $htmls [.select.list curselection]]
    set page [exec -- wget -q --output-document=- $imdb]
    set year [get_year $page]
    set director [get_director $page]
    set actors [get_actors $page]
    set duration [get_duration $page]
    set state ""
}

proc candidates {} {
    global state title htmls
    set state "Accessing IMDB Database..."
    update
    set imdb "http://imdb.com/find?s=all&q="
    set page [exec -- wget -q --output-document=- $imdb$title]
    set data [regexp -all -inline -line -- {<a href[^>]*/title/tt[^<]*</a>[^<]*</td>} $page]
    set htmls ""
    foreach obj $data {
        set html "http://imdb.com"
        append html [regexp -inline -- {/title[^\"?]*} $obj]
        set objtitle [regsub -all -- {<.*?>} $obj ""]
        set objtitle [regsub -all -- {&.*?;} $objtitle " "]
        set objtitle [string trim $objtitle]
        .select.list insert end $objtitle
        lappend htmls $html
    }
    set state ""
}

proc next {} {
    global title year director actors duration state
    .select.list delete 0 end
    if {[eof stdin]} {
        tk_messageBox -message "This was the last title." -type ok \
            -parent . -title Quit
        exit
    }
    gets stdin title
    foreach info { year director actors duration } { set $info "" }
    candidates
    set state "Select one from the list."
}

########
# Init #
########

next