
# TODO: add information protection

___x_cmd_coco_writefile(){
    local fp=""
    local content=""
    local is_patchdiff="false"

    arg:init:x  coco
    while [ $# -gt 0 ]; do
        case "$1" in
            --filepath)     fp="$2";            arg:2:shift ;;
            --content)      content="$2";       arg:2:shift ;;
            --is_patchdiff) is_patchdiff="$2";  arg:2:shift ;;
            *)              break ;;
        esac
    done
    [ -n "$content" ] || N=coco M="Please provide the text content to write" log:ret:64

    ___x_cmd_coco_valid_fp --filepath "$fp" || return $?
    coco:info "Writing to file -> $fp"

    local fporig="${fp}.orig"
    local fprej="${fp}.rej"
    local hasorig=0; [ ! -e "$fporig" ] || hasorig=1
    local hasrej=0;  [ ! -e "$fprej"  ] || hasrej=1

    local content_line=""; local confirmid=""
    while true; do
        content_line="$( ___x_cmd_cmds wc -l <<A
$content
A
)"
        if [ "$content_line" -gt 50 ]; then
            coco:info "Requesting user confirmation" 2>/dev/tty >&2
            ___x_cmd less --autoview-all 2>/dev/tty >&2  <<A
$content
A
        else
            coco:info --m:content "$content" "Requesting user confirmation" 2>/dev/tty >&2
        fi
        ___x_cmd ui select confirmid "Requesting user confirmation" \
            "Yes" "No" 2>/dev/tty >&2 || return $?

        case "$confirmid" in
            1)  coco:info "User granted permission to write to file"
                break ;;
            2)  coco:error "User permission denied for writing to file"
                printf "[BREAK] 1\n" >> "$X_COCO_TOOL_ENACTCMD_FOLDER/log"
                return 1
                ;;
        esac
    done

    if [ ! -e "$fp" ]; then
        coco:info "File does not exist"
        local dir="${fp%/*}"
        if [ ! -d "$dir" ]; then
            coco:info "Creating parent directory -> $dir"
            ___x_cmd mkdirp "${fp%/*}" || return $?
        fi
    fi

    if [ "$is_patchdiff" = "true" ]; then
        ! ___x_cmd_coco_writefile___patch_raw "$fp" << A || return 0
$content
A

        coco:info "Possible diff content error, attempting to correct diff content"
        printf "%s\n" "$content" | ___x_cmd_cmds awk -f "$___X_CMD_ROOT_MOD/coco/lib/awk/fix_patchdiff.awk" | ___x_cmd_coco_writefile___patch_raw "$fp"
        if [ $? -ge 0 ]; then
            coco:info "Succeeded to apply patch diff to file '$fp'"
            return 0
        else
            coco:error "Failed to apply patch diff to file '$fp'"
            if [ "$hasorig" = 0 ] && [ -e "$fporig" ]; then
                coco:info "Removing temp file -> $fporig"
                ___x_cmd rmrf "$fporig"
            fi
            if [ "$hasrej" = 0 ] && [ -e "$fprej" ]; then
                coco:info "Removing temp file -> $fprej"
                ___x_cmd rmrf "$fprej"
            fi
            return 1
        fi
    else
        # append or overwrite
        printf "%s\n" "$content" >> "$fp"
    fi
}


# check validate filepath
___x_cmd_coco_valid_fp(){
    local fp=""
    arg:init:x  coco
    while [ $# -gt 0 ]; do
        case "$1" in
            --filepath)     fp="$2";    arg:2:shift ;;
            *)              break ;;
        esac
    done

    [ -n "$fp" ] || N=coco M="Please provide file path value" log:ret:64
    case "$fp" in
        /*) ;;
        *)  N=coco M="File path '$fp' must be an absolute path" log:ret:1
    esac

    if [ -e "$fp" ] && [ -d "$fp" ]; then
        coco:error "File path '$fp' already exists and is a directory."
        return 1
    fi

    return 0
}

___x_cmd_coco_writefile___patch_raw(){
    if true | ___x_cmd_cmds patch --verbose --ignore-whitespace /dev/null 2>/dev/null 1>&2 ; then
        ___x_cmd_coco_writefile___patch_raw(){
            ___x_cmd_cmds patch -u --verbose --ignore-whitespace "$@"
        }
    elif  ___x_cmd_hascmd patch; then
        ___x_cmd_coco_writefile___patch_raw(){
            ___x_cmd_cmds patch "$@"
        }
    elif  ___x_cmd_hascmd busybox; then
        ___x_cmd_coco_writefile___patch_raw(){
            ___x_cmd buysbox patch "$@"
        }
    else
        ___x_cmd_coco_writefile___patch_raw(){
            ___x_cmd cosmo patch -u --verbose --ignore-whitespace "$@"
        }
    fi

    ___x_cmd_coco_writefile___patch_raw "$@"
}
