#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
MAGENTA='\033[0;35m'
RESET='\033[0m'

flag_add=false
flag_commit=false
flag_md=false
flag_mm=false
flag_pm=false
commit_message=""

function execute_command() {
    command="$1"
    message="$2"
    exit_on_failure=${3:-true}

    echo "${GREEN}执行命令：${command}${RESET}"

    eval "${command}"
    status=$?

    if [[ ${status} -ne 0 ]]; then
        echo "${RED}错误：${message}${RESET}"
        if [[ "$exit_on_failure" == "true" ]]; then
            exit ${status}
        else
            return ${status}
        fi
    fi
}

function show_usage() {
    echo "用法：$0 [选项]"
    echo "选项："
    echo "  -a              添加所有更改到暂存区"
    echo "  -c <message>    提交更改到 xiaom 分支"
    echo "  -md             合并 xiaom 到 dev 分支"
    echo "  -mm             合并 xiaom 到 master 分支"
    echo "  -pm             合并 master 到 xiaom 分支"
    echo "  -h              显示此帮助信息"
    echo ""
    echo "示例："
    echo "  $0 -a -c \"feat: 添加新功能\""
    echo "  $0 -md"
    echo "  $0 -mm"
    echo "  $0 -pm"
}

# 解析参数
while [[ $# -gt 0 ]]; do
    case $1 in
    -a)
        flag_add=true
        shift
        ;;
    -c)
        if [[ -z "$2" || "$2" == -* ]]; then
            echo "${RED}错误：-c 选项需要一个提交信息参数${RESET}"
            show_usage
            exit 1
        fi
        flag_commit=true
        commit_message="$2"
        shift 2
        ;;
    -md)
        flag_md=true
        shift
        ;;
    -mm)
        flag_mm=true
        shift
        ;;
    -pm)
        flag_pm=true
        shift
        ;;
    -h | --help)
        show_usage
        exit 0
        ;;
    -*)
        echo "${RED}错误：未知选项 $1${RESET}"
        show_usage
        exit 1
        ;;
    *)
        echo "${RED}错误：未知参数 $1${RESET}"
        show_usage
        exit 1
        ;;
    esac
done

if [ "$flag_add" = true ]; then
    echo "${MAGENTA}执行添加所有更改操作...${RESET}"
    execute_command "git add ." "git add 操作失败"
fi

if [ "$flag_commit" = true ] && [ -n "$commit_message" ]; then
    echo "${MAGENTA}正在向 xiaom 分支提交变更，提交信息：${RESET}${YELLOW}\"${commit_message}\"${RESET}"
    execute_command "git checkout xiaom" "切换至 xiaom 分支失败"
    execute_command "git commit -m \"$commit_message\"" "提交操作失败"
    execute_command "git push" "推送至 xiaom 分支失败"
fi

if [ "$flag_pm" = true ]; then
    echo "${MAGENTA}正在合并 master 分支代码到个人分支 xiaom...${RESET}"
    # 合并 master 到 xiaom
    execute_command "git checkout master" "切换至 master 分支失败"
    execute_command "git pull" "拉取 master 分支最新内容失败"
    execute_command "git checkout xiaom" "切换至 xiaom 分支失败"
    execute_command "git merge master" "合并 master 到 xiaom 分支失败" "false"
    merge_m2x_status=$?
    if [ $merge_m2x_status -ne 0 ]; then
        # 检查是否有冲突
        if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
            echo "${YELLOW}检测到合并冲突，请手动解决冲突...${RESET}"
            echo "${YELLOW}解决冲突后，按任意键继续...${RESET}"
            read -n 1 -s -r -p ""

            if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
                echo "${RED}错误：仍存在未解决的冲突，请处理后再继续${RESET}"
                exit 1
            fi

            execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
        else
            exit ${merge_m2x_status}
        fi
    fi
    execute_command "git push" "推送 xiaom 分支失败"
fi

if [ "$flag_md" = true ]; then
    echo "${MAGENTA}正在合并代码到 nomo-dev 分支...${RESET}"
    # 合并 xiaom 到 nomo-dev
    execute_command "git checkout nomo-dev" "切换至 nomo-dev 分支失败"
    execute_command "git pull" "拉取 nomo-dev 分支最新内容失败"
    execute_command "git merge xiaom" "合并 xiaom 到 nomo-dev 分支失败" "false"
    merge_x2d_status=$?
    if [ $merge_x2d_status -ne 0 ]; then
        # 检查是否有冲突
        if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
            echo "${YELLOW}检测到合并冲突，请手动解决冲突...${RESET}"
            echo "${YELLOW}解决冲突后，按任意键继续...${RESET}"
            read -n 1 -s -r -p ""

            if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
                echo "${RED}错误：仍存在未解决的冲突，请处理后再继续${RESET}"
                exit 1
            fi

            execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
        else
            exit ${merge_x2d_status}
        fi
    fi
    execute_command "git push" "推送 nomo-dev 分支失败"
    execute_command "git checkout xiaom" "切换至 xiaom 分支失败"
fi

if [ "$flag_mm" = true ]; then
    echo "${MAGENTA}正在合并代码到 master 分支...${RESET}"
    # 合并 xiaom 到 master
    execute_command "git checkout master" "切换至 master 分支失败"
    execute_command "git pull" "拉取 master 分支最新内容失败"
    execute_command "git merge xiaom" "合并 xiaom 到 master 分支失败" "false"
    merge_x2m_status=$?
    if [ $merge_x2m_status -ne 0 ]; then
        # 检查是否有冲突
        if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
            echo "${YELLOW}检测到合并冲突，请手动解决冲突...${RESET}"
            echo "${YELLOW}解决冲突后，按任意键继续...${RESET}"
            read -n 1 -s -r -p ""

            if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
                echo "${RED}错误：仍存在未解决的冲突，请处理后再继续${RESET}"
                exit 1
            fi

            execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
        else
            exit ${merge_x2m_status}
        fi
    fi
    execute_command "git push" "推送 master 分支失败"
    execute_command "git checkout xiaom" "切换至 xiaom 分支失败"
fi

if [ "$flag_add" = false ] && [ "$flag_commit" = false ] && [ "$flag_pm" = false ] && [ "$flag_md" = false ] && [ "$flag_mm" = false ]; then
    echo "${YELLOW}未提供有效参数${RESET}"
    show_usage
fi
