#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $DEBUG = 1;
my $filename="c:\\datafile.dat";

while (1){
prompt();
}

#----------------------subroutines-------------------------#


# asks the user what to do then does it.
# each if/elsif points to the subroutine appropriate for the command given
sub prompt {
print "What do you want to do?\n";
print "search  find a person.\n";
print "new     enter a new contact in the database.\n";
print "quit    exit the program\n\n";
print "enter your choice:";

my $choice = user_input ('');

  if ($choice =~ /^s/ ){
    search_by();
  }
  elsif ($choice =~ /^n/ ){
    new_entry();
  }
  elsif ($choice =~ /^q/ ){
    print "Goodbye...";
    exit;
  }
  else {
    print "Invalid input.  Enter: Search, New, or Quit.\n";
  }
}


# new_entry asks the user for a name, phone number, address,
# and number of children, and calls the kid_info() subroutine
# to get the name and age of each child
sub new_entry{
my $new_name = user_input('name:');
my $new_phone = user_input('phone number:');
my $new_address = user_input('address (one line):');
my $num_children = user_input('nuumber of children:');
my $ref_kids;
if ($num_children > 0){
  $ref_kids = [$num_children, childrens_info($num_children)];
}
else {
  $ref_kids = 0;
}

my $new_person = {
  name => $new_name,
  phone => $new_phone,
  address => $new_address,
  children => $ref_kids
  };
print " DEBUG: new_person \n" if $DEBUG;
print Dumper($new_person)     if $DEBUG;

# open(DATABASE, ">> filename") or die "Oops - couldn't append to '$filename'";
# print DATABASE "%new_person";
# close DATABASE;
}


# Gets info on a new contact's children (if any) each child has their own hash
# that stores their name and age and each hash is assigned a reference.
# the references are then grouped into an array which is referenced at the
# new_entry() subroutine and assigned to the %new_person hash's "cildren" key
sub childrens_info {
my ($num_kids) = @_;
my $kids_info;
for (my $i=0; $i < $num_kids; $i++){
  $kids_info = {
  "name" . $i => user_input('child\'s name:'),
  "age" . $i => user_input('child\'s age:')
  }
}
return $kids_info;
}


#
#
#
#sub write_to_file {
#my ($person_info) = @_;
#open(DATABASE, ">> filename")
#  or die "Oops - couldn't append to '$filename'";
#print DATABASE %$person_info;
#close DATABASE;
#}


# asks the user if they would like to search for a perosn by
# A) name, B) phone number, C) address, D)number of children.
# then feeds the search subroutine the appropriate parameters
sub search_by {
print "search_by subroutine\n" if ($DEBUG);

print "search for contacts by:\n";
print "name       person's name\n";
print "phone      phone number\n";
print "address    person's full address\n";
print "children   number of children (returns multiple results)\n\n";
my $search_type = user_input('search by');

if ($search_type =~ /^n/ ){
  search('name');
  }
elsif ($search_type =~ /^p/ ){
  search('phone');
  }
elsif ($search_type =~ /^a/ ){
  search('address');
  }
elsif ($search_type =~ /^c/ ){
  search('number of children');
  }
else {
  print "'$search_type' is not a valid command";
  }
}


#
#
#
sub search {
my $search_key = @_;
print "DEBUG: Search subroutine\n" if ($DEBUG);

}
     
     
# gets all the input from the user and sends it back
# to the subroutine that called for it
# ex. If the search subroutine needs a name it calls
# user_input() with the parameter 'name' like this: user_input('name')
sub user_input {
my ($prompt_line) = @_;
print "$prompt_line ";
my $text=<>;
chomp ($text);
print "you typed '$text' \n" if ($DEBUG);
return $text;
}
