#################################################
#   ___________    ____ ______  __ __  _____    #
#   \____ \__  \ _/ ___\\____ \|  |  \/  ___/   #
#   |  |_> > __ \\  \___|  |_> >  |  /\___ \    #
#   |   __(____  /\___  >   __/|____//____  >   #
#   |__|       \/     \/|__|              \/    #
#                                               #
#################################################

# ========================================
# Create a Pacpus plugin
# ========================================
function(PACPUS_PLUGIN OUT_CPP OUT_H PLUGIN_NAME)

    set(PLUGIN_NAME ${PLUGIN_NAME}Plugin)
    set(${OUT_CPP} ${PLUGIN_NAME}.cpp ${PLUGIN_NAME}.json PARENT_SCOPE)
    set(${OUT_H} ${PLUGIN_NAME}.h PARENT_SCOPE)

    string(TOUPPER ${PLUGIN_NAME} PLUGIN_NAME_UPPER)

    # create the header file
    file(WRITE ${PLUGIN_NAME}.h
        "// Autogenerated file by PacpusUtilities.cmake\n"
        "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
        "\n"
        "#ifndef __${PLUGIN_NAME_UPPER}_H__\n"
        "#define __${PLUGIN_NAME_UPPER}_H__\n"
        "\n"
        "#include <QObject>\n"
        "#include <qplugin.h>\n"
        "\n"
        "#include <Pacpus/kernel/PacpusPluginInterface.h>\n"
        "\n"
        "/// Auto-generated plugin class\n"
        "class ${PLUGIN_NAME}\n"
        "    : public QObject\n"
        "    , public PacpusPluginInterface\n"
        "{\n"
        "    Q_OBJECT\n"
        "    //Qt5 Plugins Macro\n"
        "    Q_PLUGIN_METADATA(IID \"papus.pacpuscityvip." ${PLUGIN_NAME} "\" FILE \"" ${PLUGIN_NAME} ".json\")\n"
        "    Q_INTERFACES(PacpusPluginInterface)\n"    
        "\n"
        "public:\n"
        "    ${PLUGIN_NAME}();\n"
        "    ~${PLUGIN_NAME}();\n" 
        "\n"
        "protected:\n"
        "    QString name();\n"
        "};\n"
        "\n"
        "#endif // __${PLUGIN_NAME_UPPER}_H__\n"
    )

    # create the cpp file
    file(WRITE ${PLUGIN_NAME}.cpp
        "// Autogenerated file by PacpusUtilities.cmake\n"
        "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
        "\n"
        "#include \"${PLUGIN_NAME}.h\"\n"
        "\n"
        "${PLUGIN_NAME}::${PLUGIN_NAME}()\n"
        "{\n"
        "}\n"
        "\n"
        "${PLUGIN_NAME}::~${PLUGIN_NAME}()\n"
        "{\n"
        "}\n"
        "\n"
        "QString ${PLUGIN_NAME}::name()\n"
        "{\n"
        "    return \"${PLUGIN_NAME}\";\n"
        "}\n"
        "\n"
        "//Qt4 Plugin Macro desactivated\n"
        "//Q_EXPORT_PLUGIN2(${PLUGIN_NAME}, ${PLUGIN_NAME})\n"
    )

    file(WRITE ${PLUGIN_NAME}.json
        "{\n"
        "    \"Keys\": [\""${PLUGIN_NAME} "\"]\n"
        "}\n"
        )
endfunction(PACPUS_PLUGIN)

# ========================================
# Replace backslashes by slashes
# ========================================
macro(pacpus_purge_backslash var)
    string(REGEX REPLACE "\\\\" "/" ${var} "${${var}}")
endmacro()

# ========================================
# Get the version of MSVC as a string
# ========================================
macro(pacpus_get_msvc output)
    # By default, unknown
    set(${output} "unknown")
    if(MSVC9)
        set(${output} "msvc2008")
    elseif(MSVC10)
        set(${output} "msvc2010")
    else(MSVC11)
        set(${output} "msvc2012")
    endif()
endmacro()

# ========================================
# Print a debug message
# ========================================
function(pacpus_output_status msg)
    message(STATUS "${msg}")
    string(REPLACE "\\" "\\\\" msg "${msg}")
    string(REPLACE "\"" "\\\"" msg "${msg}")
endfunction()

# ========================================
# Pacpus log message
# ========================================
function(pacpus_info text)
    set(status_cond)
    set(status_then)
    set(status_else)

    set(status_current_name "cond")
    foreach(arg ${ARGN})
        if(arg STREQUAL "THEN")
            set(status_current_name "then")
        elseif(arg STREQUAL "ELSE")
            set(status_current_name "else")
        else()
            list(APPEND status_${status_current_name} ${arg})
        endif()
    endforeach()

    if(DEFINED status_cond)
        set(status_placeholder_length 32)
        string(RANDOM LENGTH ${status_placeholder_length} ALPHABET " " status_placeholder)
        string(LENGTH "${text}" status_text_length)
        if(status_text_length LESS status_placeholder_length)
            string(SUBSTRING "${text}${status_placeholder}" 0 ${status_placeholder_length} status_text)
        elseif(DEFINED status_then OR DEFINED status_else)
            pacpus_output_status("${text}")
            set(status_text "${status_placeholder}")
        else()
            set(status_text "${text}")
        endif()

        if(DEFINED status_then OR DEFINED status_else)
            if(${status_cond})
                string(REPLACE ";" " " status_then "${status_then}")
                string(REGEX REPLACE "^[ \t]+" "" status_then "${status_then}")
                pacpus_output_status("${status_text} ${status_then}")
            else()
                string(REPLACE ";" " " status_else "${status_else}")
                string(REGEX REPLACE "^[ \t]+" "" status_else "${status_else}")
                pacpus_output_status("${status_text} ${status_else}")
            endif()
        else()
            string(REPLACE ";" " " status_cond "${status_cond}")
            string(REGEX REPLACE "^[ \t]+" "" status_cond "${status_cond}")
            pacpus_output_status("${status_text} ${status_cond}")
        endif()
    else()
        pacpus_output_status("${text}")
    endif()
endfunction()

# ========================================
# Pacpus add library and set debug postfix
# ========================================
function(pacpus_add_library target)
    add_library(
        ${ARGV}
    )
    set_target_properties(
        ${target}
        PROPERTIES DEBUG_POSTFIX "_d"
    )
endfunction()

# ========================================
# Pacpus add executable and set debug postfix
# ========================================
function(pacpus_add_executable target)
    add_executable(
        ${ARGV}
    )
    set_target_properties(
        ${target}
        PROPERTIES DEBUG_POSTFIX "_d"
    )
endfunction()

# ========================================
# Pacpus install
# ========================================
function(pacpus_install)
    install(
        TARGETS ${ARGV}
        RUNTIME DESTINATION ${PACPUS_INSTALL_DIR}/bin
        LIBRARY DESTINATION ${PACPUS_INSTALL_DIR}/lib
        ARCHIVE DESTINATION ${PACPUS_INSTALL_DIR}/lib
    )
    if(MSVC)
        # install debug information files .pdb
        foreach(target ${ARGV})
            get_target_property(target_filepath ${target} LOCATION_DEBUG)
            get_filename_component(target_directory ${target_filepath} PATH)
            get_filename_component(target_basename ${target_filepath} NAME_WE)
            
            set(target_pdb "${target_directory}/${target_basename}.pdb")
            install(FILES ${target_pdb}
                CONFIGURATIONS Debug
                DESTINATION ${PACPUS_INSTALL_DIR}/lib
            )
        endforeach()
    endif(MSVC)
endfunction()

# ========================================
# Pacpus folder
# ========================================
function(pacpus_folder target folder_name)
    if(PACPUS_USE_SOLUTION_FOLDERS AND MSVC)
        set_target_properties(${target}
            PROPERTIES FOLDER ${folder_name}
        )
    endif()
endfunction()
