! Copyright (c) 2021 Approximatrix, LLC <support@approximatrix.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module special_filenames
implicit none

contains

    function get_special_full_filename(category, filename) result(res)
    use utilities, only: combine_paths
    use config
    use logging
    implicit none
    
        character(*), intent(in)::category, filename
        character(len=:), pointer::res

        res => null()
        
        call write_log("category: "//trim(category))
        call write_log("file: "//trim(filename))

        if(trim(category) == "releases") then
            allocate(character(len=(len_trim(release_dir)+len_trim(filename)+1)) :: res)
            call combine_paths(release_dir, filename, res)
        else if(trim(category) == "uploads") then
            allocate(character(len=(len_trim(release_dir)+len_trim(filename)+1)) :: res)
            call combine_paths(release_dir, filename, res)
        else if(trim(category) == "results") then
            allocate(character(len=(len_trim(results_dir)+len_trim(filename)+1)) :: res)
            call combine_paths(results_dir, filename, res)
        else if(trim(category) == "static") then
            allocate(character(len=(len_trim(static_dir)+len_trim(filename)+1)) :: res)
            call combine_paths(static_dir, filename, res)
        else if(trim(category) == "favicon.txt") then
            allocate(character(len=(len_trim(static_dir)+len_trim(filename)+1)) :: res)
            call combine_paths(static_dir, "favicon.txt", res)
        else if(trim(category) == "instructions") then
            allocate(character(len=(len_trim(instructions_dir)+len_trim(filename)+1)) :: res)
            call combine_paths(instructions_dir, filename, res)
        end if
        
        call write_log("path: "//trim(res))
        
    end function get_special_full_filename
    
    function get_instructions_static_filename(instruction_id) result(res)
    use captain_db
    implicit none
    
        integer, intent(in)::instruction_id
        character(len=:), pointer::res
        
        character(PLAYER_NAME_LENGTH)::instruction_name
        
        call get_instruction_name(instruction_id, instruction_name)
        
        res => get_special_full_filename("instructions", trim(instruction_name)//".json")
        
    end function get_instructions_static_filename
    
    function get_task_result_static_filename(job_id, task_num, no_path) result(res)
    use config
    implicit none
    
        integer, intent(in)::job_id, task_num
        
        logical, intent(in), optional::no_path
        
        character(len=:), pointer::res
        character(64)::filename
        
        character(12)::job_text, task_text
        
        write(job_text, '(I8)') job_id
        write(task_text, '(I8)') task_num
        filename = "results-job"//trim(adjustl(job_text))//"-task"//trim(adjustl(task_text))//".txt"
        
        if(present(no_path)) then
            if(no_path) then
                allocate(character(len=len_trim(filename)) :: res)
                res = filename
            else
                res => get_special_full_filename("results", filename)
            end if
        else
            res => get_special_full_filename("results", filename)
        end if
        
    end function get_task_result_static_filename
    
    function get_full_filename_from_request(req) result(res)
    use server_response
    implicit none
    
        class(request)::req
        character(len=:), pointer::res
    
        character(256)::first
        character(1024)::remaining
        integer::i
        
        call req%path_component(1, first)
        
        i = index(req%location, trim(first))
        i = i + len_trim(first) + 1
        remaining = req%location(i:len_trim(req%location))
        
        res => get_special_full_filename(first, trim(remaining))
        
    end function get_full_filename_from_request
    
    subroutine report_upload(req)
    use captain_db
    use server_response
    use logging
    implicit none
    
        class(request), intent(in)::req
        integer::job_id, ierr
        character(len=:), pointer:: job_text
        
        character(256)::first
        character(1024)::remaining
        integer::i
        
        job_text => req%q%get_value("job")
        call write_log("jobtext: "//trim(job_text))
        if(associated(job_text)) then
            read(job_text, *, iostat=ierr) job_id
            if(ierr == 0) then
                call write_log("jobid ok")
                call req%path_component(1, first)
                i = index(req%location, trim(first))
                i = i + len_trim(first) + 1
                remaining = req%location(i:len_trim(req%location))
                
                
                call write_log("Writing job upload for: "//trim(first)//"  ->  "//trim(remaining))
                call add_job_upload_db(job_id, trim(first), trim(remaining))
                
            end if
        end if
        
    end subroutine report_upload
    
end module special_filenames