package spiderutil

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

//处理福建验证码识别
func Math2CQ(code string) string {
	reg, _ := regexp.Compile("(\\d)")
	tmp := reg.FindAllString(code, -1)
	ret := 0
	if len(tmp) > 0 {
		//判断是否只识别到一个数字
		if len(tmp) == 1 {
			tmp = append(tmp, "0")
			if strings.Index(code, "l") > -1 {
				tmp[1] = "1"
			}
		}
		x, _ := strconv.Atoi(tmp[0])
		y, _ := strconv.Atoi(tmp[1])
		if x < y {
			z := y
			y = x
			x = z
		}
		if strings.Index(code, "加") > -1 {
			ret = x + y
		} else if strings.Index(code, "减") > -1 {
			ret = x - y
		} else if strings.Index(code, "乘") > -1 {
			ret = x * y
		} else if strings.Index(code, "除") > -1 {
			ret = x / y
		} else if strings.Index(code, "臧") > -1 {
			ret = x - y
		} else if strings.Index(code, "咸") > -1 {
			ret = x - y
		}
		return fmt.Sprint(ret)
	}
	return ""
}

//处理加减乘除
func Math2NX(code string) string {
	reg, _ := regexp.Compile("(\\d+)([^\\d]+)(\\d+)")
	tmp := reg.FindAllStringSubmatch(code, -1)
	if len(tmp[0]) < 3 {
		return ""
	}
	x, _ := strconv.Atoi(tmp[0][1])
	y, _ := strconv.Atoi(tmp[0][3])
	ret := 0
	switch tmp[0][2] {
	case "+":
		ret = x + y
	case "x":
		ret = x * y
	case "-":
		ret = x - y
	case "/":
		ret = x / y
	}
	return fmt.Sprint(ret)
}

//处理加减乘除
func Math2(code string) string {
	reg, _ := regexp.Compile("(\\d+)([^\\d]+)(\\d+)")
	tmp := reg.FindAllStringSubmatch(code, -1)
	x, _ := strconv.Atoi(tmp[0][1])
	y, _ := strconv.Atoi(tmp[0][3])
	ret := 0
	switch tmp[0][2] {
	case "加":
		ret = x + y
	case "乘":
		ret = x * y
	case "减":
		ret = x - y
	case "除":
		ret = x / y
	}
	//log.Printf("%s %s %s=%d", tmp[0][1], tmp[0][2], tmp[0][3], ret)
	return fmt.Sprint(ret)
}

func Math2JX(code string) string {
	reg, _ := regexp.Compile("(\\d+|\\?)")
	if strings.Index(code, "7?") > -1 {
		code = strings.Replace(code, "7?", "-?", -1)
	}
	if strings.Index(code, "o") > -1 {
		code = strings.Replace(code, "o", "0", -1)
	}
	strs := reg.FindAllString(code, -1)
	if len(strs) == 3 {
		if strings.Index(code, "加") > -1 || strings.Index(code, "+") > -1 {
			if strs[0] == "?" {
				a, _ := strconv.Atoi(strs[1])
				b, _ := strconv.Atoi(strs[2])
				return strconv.Itoa(b - a)
			} else if strs[1] == "?" {
				a, _ := strconv.Atoi(strs[0])
				b, _ := strconv.Atoi(strs[2])
				return strconv.Itoa(b - a)
			} else {
				a, _ := strconv.Atoi(strs[0])
				b, _ := strconv.Atoi(strs[1])
				return strconv.Itoa(a + b)
			}
		} else if strings.Index(code, "减") > -1 || strings.Index(code, "-") > -1 || strings.Index(code, "咸") > -1 {
			if strs[0] == "?" {
				a, _ := strconv.Atoi(strs[1])
				b, _ := strconv.Atoi(strs[2])
				return strconv.Itoa(b + a)
			} else if strs[1] == "?" {
				a, _ := strconv.Atoi(strs[0])
				b, _ := strconv.Atoi(strs[2])
				return strconv.Itoa(a - b)
			} else {
				a, _ := strconv.Atoi(strs[0])
				b, _ := strconv.Atoi(strs[1])
				return strconv.Itoa(a - b)
			}
		}
	}
	return ""
}
func Math2SC(code string) string {
	//格式化code
	arr := make([][]string, 0)
	arr = append(arr, []string{"郏", "乘8"})
	arr = append(arr, []string{"17u", "加"})
	arr = append(arr, []string{"舞", "乘"})
	arr = append(arr, []string{"〇", "0"})
	arr = append(arr, []string{"蛳", "加"})
	arr = append(arr, []string{"D", "0"})
	arr = append(arr, []string{"<", "0"})
	arr = append(arr, []string{"Z", "2"})
	arr = append(arr, []string{"A", "4"})
	arr = append(arr, []string{"\"~", "4"})
	arr = append(arr, []string{"茎", "3"})
	arr = append(arr, []string{"B", "8"})
	arr = append(arr, []string{"曰", "8"})
	arr = append(arr, []string{"4%", "4乘5"})
	arr = append(arr, []string{"7]0", "加"})
	arr = append(arr, []string{"27|1", "加"})
	arr = append(arr, []string{"恤", "加"})
	arr = append(arr, []string{"柳", "加"})
	arr = append(arr, []string{"襄", "加"})
	arr = append(arr, []string{"俪", "加"})
	arr = append(arr, []string{"挪", "加"})
	arr = append(arr, []string{"加l7", "加0"})
	arr = append(arr, []string{"S", "5"})
	arr = append(arr, []string{"s", "5"})
	arr = append(arr, []string{"稞", "乘"})
	arr = append(arr, []string{"暴", "乘"})
	arr = append(arr, []string{"狮", "加"})
	arr = append(arr, []string{"刀盯", "加7"})
	arr = append(arr, []string{"正", "1"})
	arr = append(arr, []string{"舫", "1"})
	arr = append(arr, []string{"舫", "l"})
	arr = append(arr, []string{"\\", "1"})
	arr = append(arr, []string{"x", "1"})
	arr = append(arr, []string{"〔", "0"})
	arr = append(arr, []string{"U", "0"})
	arr = append(arr, []string{"n", "0"})
	arr = append(arr, []string{"口", "0"})
	arr = append(arr, []string{"霁", "乘"})
	arr = append(arr, []string{"洁", "乘"})
	arr = append(arr, []string{"碟", "乘"})
	arr = append(arr, []string{"菜", "乘"})
	arr = append(arr, []string{"骤", "乘"})
	arr = append(arr, []string{"剩", "乘2"})
	arr = append(arr, []string{"镰", "乘"})
	arr = append(arr, []string{"葬", "乘"})
	arr = append(arr, []string{"褰", "乘"})
	arr = append(arr, []string{"万l7", "加"})
	arr = append(arr, []string{"z717", "加"})
	arr = append(arr, []string{"1717", "加"})
	arr = append(arr, []string{"I7", "加"})
	arr = append(arr, []string{"枷", "加"})
	arr = append(arr, []string{"7m", "加"})
	arr = append(arr, []string{"/11", "加"})
	arr = append(arr, []string{"2w", "加"})
	arr = append(arr, []string{"zu", "加"})
	arr = append(arr, []string{"加等于", "加1等于"})
	arr = append(arr, []string{"侧", "加4"})
	arr = append(arr, []string{"L`", "4"})
	arr = append(arr, []string{"旨", "5"})
	arr = append(arr, []string{"芭乘臼", "3乘0"})
	arr = append(arr, []string{"%刀们", "8乘7"})
	for _, v := range arr {
		if strings.Index(code, v[0]) > -1 {
			code = strings.Replace(code, v[0], v[1], -1)
		}
	}
	reg, _ := regexp.Compile("(\\d)")
	tmp := reg.FindAllString(code, -1)
	if len(tmp) >= 2 {
		x, _ := strconv.Atoi(tmp[0])
		y, _ := strconv.Atoi(tmp[1])
		if strings.Index(code, "加") > -1 {
			return strconv.Itoa(x + y)
		} else if strings.Index(code, "乘") > -1 {
			return strconv.Itoa(x * y)
		}
	}
	return ""
}

func Math2JL(code string) string {
	nstr := []string{"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "窿", "玫", "〇", "嵋", "喧", "B", "冒", "蘑", "×", "+"}
	n := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "8", "9", "0", "加", "5", "8", "5", "8", "乘", "加"}
	for k, v := range nstr {
		if strings.Index(code, v) > -1 {
			code = strings.Replace(code, v, n[k], 1)
		}
	}
	r, _ := regexp.Compile("(\\d)")
	sign_regex, _ := regexp.Compile("加|减|乘|除")
	rs := r.FindAllString(code, -1)
	sign_rs := sign_regex.FindString(code)
	if len(rs) == 3 && rs[1] == "1" {
		rs[1] = rs[2]
		sign_rs = "减"
	}

	if len(rs) > 1 {
		a, _ := strconv.Atoi(rs[0])
		b, _ := strconv.Atoi(rs[1])
		switch sign_rs {
		case "加":
			return strconv.Itoa(a + b)
		case "减":
			return strconv.Itoa(a - b)
		case "乘":
			return strconv.Itoa(a * b)
		case "除":
			return strconv.Itoa(a / b)
		}
	}
	return ""
}
