use Selkie::UI;
use Grammar::Editor::Styles;
use Grammar::Editor::Trace;
use Grammar::Editor::Match;
use Grammar::Editor::Pane::Grammar;
use Grammar::Editor::Pane::Input;
use Grammar::Editor::Pane::Trace;
use Grammar::Editor::Pane::Match;
use Grammar::Editor::Pane::Error;

App {
    $*ERR = $*OUT = open "log.log", :w;
    my $code    := new-state "";
    my $string  := new-state "";
    my $error   := new-state "";
    my @trace   := new-array-state [];
    my %visible := new-hash-state { :GRAMMAR, :INPUT, :TRACE, :MATCH };
    VBox {
        HBox :1size, {
            for <GRAMMAR INPUT TRACE MATCH> -> $label {
                Button(:$label).on-press: {
                    %visible{$label} = !%visible{$label}
                }
            }
        }
        HBox {
            VBox {
                grammar-pane($code, $error, %visible);
                input-pane($string, $error, %visible);
            }
            VBox {
                HBox :size(:5flex), {
                    trace-pane(@trace, %visible);
                    Border :title<Match>, :size{ %visible<MATCH> ?? :flex !! 0 }, {
                        $ = $string;
                        if $code {
                            my $match = match-pane($code, $string, $error, @trace);
                            .theme: :border($match ?? $s-match !! $s-fail);
                            ViewportedCardList {
                                match $match;
                            }
                        }
                    }
                }
                error-pane($error);
            }
        }
    }
}
