package pretreated

import (
	u "jy/util"
	qutil "qfw/util"
	"regexp"
	"strings"
)

/*
*
之前爬虫过来的数据对table表格的抓取异常问题
查找并修正不规则表格的字符串，只对全文做处理，块内的表格不需要修正
*
*/
var thbf = regexp.MustCompile("(?i)</?t(head|body|foot)>")

// 需要保留thead
var saveThead = regexp.MustCompile("(?is)<thead>(.+?)</thead>")
var clearpkg = regexp.MustCompile("(标示|标识)")
var clearMoneyReg1 = regexp.MustCompile("(总成交金额：[0-9.]+)[\\s ]+([(（]?万元[)）]?)")

func RepairCon(con string) string {
	con = clearpkg.ReplaceAllString(con, "")
	con = clearMoneyReg1.ReplaceAllString(con, "$1$2")
	res := saveThead.FindAllStringSubmatch(con, 1)
	th := ""
	if len(res) == 1 && len(res[0]) == 2 {
		th = u.TrimLeftSpace(res[0][1], "")
	}
	con = thbf.ReplaceAllString(con, "")
	con = u.TrimLeftSpace(con, "")
	itbody := strings.Index(con, "<tr")
	iLen := 3
	if itbody == 0 {
		con = findpos(con, iLen, itbody)
	} else {
		itable := strings.Index(con, "<table")
		if itable == -1 || itable > itbody {
			con = findpos(con, iLen, itbody)
		}
	}
	//保留第一个thead
	if th != "" {
		con = strings.Replace(con, th, "<thead>"+th+"</thead>", 1)
	}
	//u.Debug(con)
	return con
}

// 修复表格
func findpos(con string, iLen, start int) (newcon string) {
	defer qutil.Catch()
	n := len(con)
	layer := 0
	pos := 0
	if start >= 0 {
		if iLen == 6 {
			for i := iLen + start; i < len(con); i++ {
				if con[i] == '<' && i+6 < n {
					str := con[i : i+6]
					if str == "</tbod" {
						if layer == 0 {
							pos = i
							break
						} else {
							layer--
						}
						i += 6
					} else if str == "<tbody" {
						layer++
						i += 6
					}
				}
			}
			if pos+7 <= n && start+6 < pos {
				newcon = con[:start] + "<table" + con[start+6:pos] + "</table" + con[pos+7:]
			}
		} else {
			layer++
			nq := 0
			lasttr := 0
			for i := iLen + start; i < len(con); i++ {
				if con[i] == '<' && i+4 < n {
					if nq == 0 {
						str := con[i : i+4]
						if str == "</tr" {
							if layer <= 0 {
								pos = i //正常情况不会存在此类情况
								break
							} else {
								layer--
								lasttr = i
							}
							i += 4
						} else if str[:3] == "<tr" {
							layer++
							i += 4
						} else if str == "<tab" && i+6 < n && con[i+4:i+6] == "le" {
							if layer == 0 {
								break
							} else {
								//内嵌的表格
								nq++
							}
						}
					} else {
						if i+6 < n {
							str := con[i : i+6]
							if str == "</tabl" {
								nq--
							} else if str == "<table" {
								nq++
							}
						} else {
							break
						}
					}
				}
			}
			if pos == 0 && lasttr > 3 {
				pos = lasttr + 5
			} else if pos > 0 {
				pos += 5
			}
			if pos <= n && pos < len(con) && start < pos {
				newcon = con[:start] + "<table>" + con[start:pos] + "</table>" + con[pos:]
			}
		}
	}
	if newcon == "" {
		newcon = con
	}
	return
}

// td的值里面有一个包，并且没有冒号kv
func isHasOnePkgAndNoKv(v1 string) (bool, string) {
	v1s := FindVal_1.FindAllString(v1, -1)
	colonCount := len(regDivision.FindAllString(v1, -1))
	if len(v1s) == 1 && colonCount < 2 {
		ispkgcolon := regexp.MustCompile(v1s[0] + "[:：]").MatchString(v1)
		if (ispkgcolon && colonCount == 1) || (!ispkgcolon && colonCount == 0) {
			return true, v1s[0]
		}
	}
	return false, v1
}

// 替换分包中混淆的词
func replPkgConfusion(v1 string) string {
	v1 = PreReg.ReplaceAllString(v1, "")
	v1 = PreReg1.ReplaceAllString(v1, "")
	v1 = PreCon.ReplaceAllString(v1, "")
	v1 = PreCon2.ReplaceAllString(v1, "")
	v1 = PreCon3.ReplaceAllString(v1, "${1}:")
	v1 = PreCon5.ReplaceAllString(v1, "${2}")
	return v1
}
