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

require 'al_template'
require 'al_form'
require './database'

def reject_request2
  Alone::redirect_to(Alone::make_uri())
end

class AlController
  def action_index
    @addresses = Address.all
    AlTemplate.run('index.html.erb')
  end

  def action_show
    @form = AlForm.new
    @form.set_widgets( AlInteger.new("id", :required => true) )

    if @form.fetch_request && @form.validate
      @address = Address.find(:id => @form[:id])
      Alone::redirect_to(Alone::make_uri(:action => 'index')) unless @address
      AlTemplate.run('show.html.erb')
    else
      Alone::redirect_to(Alone::make_uri(:action => 'index'))
    end

  end

  def action_edit
    @form = AlForm.new
    @form.set_widgets(
      AlHidden.new( 'id', :required => true,
                   :validator => /^\d+$/ ),
      AlText.new( 'name', :label => '名前', 
                 :required => true ),
      AlMail.new( 'mail', :label => 'メールアドレス', 
                 :required => true ),
      AlText.new( 'zipcode', :label => '郵便番号', 
                 :required => true, :validator => /^\d{3}-?\d{4}$/ ),
      AlText.new( 'address', :label => '住所', 
                 :required => true ),
      AlText.new( 'phone', :label => '電話番号', 
                 :required => true, :validator => /^\d[\d-]+\d$/ ),

      AlSubmit.new( 'submit', :value => '登録')
    )


    if ENV['REQUEST_METHOD'] == 'GET'
      reject_request unless @form.validate([:id])
      @address = Address.find(:id => @form[:id])
      reject_request unless @address
      @address.values.each do |k, v|
        v.force_encoding('utf-8') if v.respond_to?('force_encoding')
      end
      @form.set_values( @address.values )
      AlTemplate.run('edit.html.erb')

    elsif ENV['REQUEST_METHOD'] == 'POST'
      if @form.validate()
        @address = Address.find(:id => @form[:id])
        reject_request unless @address
        @form.values.delete(:id)
        @address.update( @form.values )
        @address.save
        AlTemplate.run('show.html.erb')
      else
        AlTemplate.run('edit.html.erb')
      end

    else
      reject_request
    end
  end

  def action_new
    @form = AlForm.new
    @form.set_widgets(
      AlText.new( 'name', :label => '名前', 
                 :required => true ),
      AlMail.new( 'mail', :label => 'メールアドレス', 
                 :required => true ),
      AlText.new( 'zipcode', :label => '郵便番号', 
                 :required => true, :validator => /^\d{3}-?\d{4}$/ ),
      AlText.new( 'address', :label => '住所', 
                 :required => true ),
      AlText.new( 'phone', :label => '電話番号', 
                 :required => true, :validator => /^\d[\d-]+\d$/ ),

      AlSubmit.new( 'submit', :value => '登録')
    )

    if @form.fetch_request && @form.validate
      @address = Address.new( @form.values )
      @address.save
      AlTemplate.run('show.html.erb')
    else
      AlTemplate.run('new.html.erb')
    end
  end

  def action_destroy
    @form = AlForm.new
    @form.set_widgets( AlInteger.new( "id", :required => true) )

    if ENV['REQUEST_METHOD'] == 'GET'
      reject_request unless @form.validate()
      @address = Address.find(:id => @form[:id])
      reject_request unless @address
      @address.values.each do |k, v|
        v.force_encoding('utf-8') if v.respond_to?('force_encoding')
      end
      AlTemplate.run('destroy.html.erb')

    elsif ENV['REQUEST_METHOD'] == 'POST'
      reject_request unless @form.validate()
      @address = Address.find(:id => @form[:id])
      @address.destroy
      Alone::redirect_to(Alone::make_uri(:action => 'index'))

    else
      reject_request
    end
  end
end
