#!/usr/bin/ruby
#
# This file is part of centurio.work/ing/commands.
#
# centurio.work/ing/commands is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# centurio.work/ing/commands is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# centurio.work/ing/commands (file COPYING in the main directory). If not, see
# <http://www.gnu.org/licenses/>.

require 'rubygems'
require 'json'
require 'xml/smart'
require 'riddl/server'
require 'fileutils'
require 'typhoeus'

# process:
# https://centurio.work/customers/evva/flow/?monitor=https://centurio.work/flow-test/engine/729/


class NewInstance < Riddl::Implementation
  def response
  
    entries = Dir.entries(File.join('data')).size
    Dir.mkdir(File.join('data',entries.to_s)) rescue nil
    
    Riddl::Parameter::Complex.new('text','text/plain',entries.to_s)
  end
end

class Index < Riddl::Implementation
  def response
    Riddl::Parameter::Complex.new('url','text/html',File.open(File.join(__dir__,'template','index.html')))
  end
end

class Builder < Riddl::Implementation
  def response
    Riddl::Parameter::Complex.new('url','text/html',File.open(File.join(__dir__,'template','builder.html')))
  end
end

class DisplayForm < Riddl::Implementation
  def response
    #Riddl::Parameter::Complex.new('url','text/html',File.open(File.join('data',@r[0],'form_min.html')))
    Riddl::Parameter::Complex.new('url','text/html',File.open(File.join(__dir__,'template','form.html')))
  end
end

class SaveForm < Riddl::Implementation
  def response 
    File.write(File.join('data',@r[0],'form.json'),@p[0].value.read)
  end
end


class SaveHtmlForm < Riddl::Implementation
  def response 
    file = File.read(File.join(__dir__,'template','form_min.html'))
    File.write(File.join('data',@r[0],'form_min.html'),(file.sub! '<!--FormComesHere-->', @p[0].value.read))
  end
end


class GetJson < Riddl::Implementation #{{{
  def response
    fname = File.join('data',@r[-2],'form.json')
    if File.exists? fname
      Riddl::Parameter::Complex.new('value','application/json',File.read(fname))
    else
      @status = 404
    end
  end
end #}}}

server = Riddl::Server.new(File.join(__dir__,'/forms.xml'), :host => 'localhost') do |opts|
  accessible_description true
  cross_site_xhr true


  on resource do
    run Index if get  
    run NewInstance if post  
    on resource do |r|
      run DisplayForm if get
      on resource 'builder' do
        run Builder if get
        run SaveForm if post 'form' 
        run SaveHtmlForm if post 'htmlform' 
      end
      on resource 'json' do
        run GetJson if get
      end
    end
  end
end.loop!