package main

import (
	"strings"
)

//对比项目名称、项目编号

func comparePNC(info *Info, compareProject *ProjectInfo) (compareStr string, score int) {
	if info.ProjectName != "" {
		pns := []string{}
		if compareProject.ProjectName != "" {
			pns = append(pns, compareProject.ProjectName)
		}
		if len(compareProject.MPN) > 0 {
			pns = append(pns, compareProject.MPN...)
		}
		ifind := 0
		templen := 0
		buyer := info.Buyer
		if buyer == "" {
			buyer = compareProject.Buyer
		}
		pn := info.ProjectName
		if buyer != "" {
			pn = strings.Replace(pn, buyer, "", -1)
		}
		for _, v := range pns {
			if info.ProjectName == v {
				ifind = 1
				break
			} else {
				//if strings.Contains(info.ProjectName, v) || strings.Contains(v, info.ProjectName) ||
				retv := CheckContain(info.ProjectName, v)
				if retv == 1 {
					ifind = 1
					break
				} else {
					if buyer != "" {
						v = strings.Replace(v, buyer, "", -1)
					}
					//v1 := CosineSimilar(pn, v)
					if retv == 2 {
						templen = len([]rune(v))
						ifind = 2
					} else if ifind == 0 {
						ifind = 3
					}
				}
			}
		}
		switch ifind {
		case 0:
			compareStr = "D"
		case 1:
			compareStr = "A"
			score += 4
			if len([]rune(info.ProjectName)) > 18 {
				score += 2
			}
		case 2:
			compareStr = "B"
			score += 2
			if templen > info.LenPN {
				templen = info.LenPN
			}
			info.PNBH = templen
			if templen > 12 {
				score += 1
			}
		case 3:
			compareStr = "C"
		}
	} else {
		compareStr = "D"
	}

	/*
				项目编号 - －()（） 要注意
				init_text = ["号","(重)","(第二次)","（重）"]
		all_clean_mark = ["[","(","【","（","〖","]",")","】","）","〗","-","〔","〕","《","［","］","｛","｝","{","—"," ","－","﹝","﹞","–"]
	*/
	for index, pc := range []string{info.ProjectCode, info.PTC} {
		if pc != "" {
			pcs := []string{}
			if compareProject.ProjectCode != "" {
				pcs = append(pcs, compareProject.ProjectCode)
			}
			if len(compareProject.MPC) > 0 {
				pcs = append(pcs, compareProject.MPC...)
			}
			ifind := 0
			templen := 0
			for _, v := range pcs {
				if pc == v {
					ifind = 1
					break
				} else {
					// math.Abs(float64(len([]rune(pc))-len([]rune(v)))) < 6
					//if !_numreg1.MatchString(pc) && !_zimureg1.MatchString(pc) && !_numreg1.MatchString(v) && !_zimureg1.MatchString(v)
					if strings.Contains(pc, v) || strings.Contains(v, pc) {
						t1 := pc
						t2 := v
						if len(v) > len(pc) {
							t1 = v
							t2 = pc
						}
						t3 := strings.Replace(t1, t2, "", -1)
						t3 = _datereg.ReplaceAllString(t3, "")
						if t3 == "" {
							ifind = 1
							break
						} else {
							ifind = 2
							templen = len([]rune(v))
						}
					} else if ifind == 0 {
						ifind = 3
					}
				}
			}
			switch ifind {
			case 0:
				compareStr += "D"
			case 1:
				compareStr += "A"
				score += 4
				if len([]rune(pc)) > 18 {
					score += 2
				}
			case 2:
				compareStr += "B"
				score += 2
				if index == 0 {
					if templen > info.LenPC {
						templen = info.LenPC
					}
					info.PCBH = templen
					if templen > 12 {
						score += 1
					}

				} else {
					if templen > info.LenPTC {
						templen = info.LenPTC
					}
					info.PTCBH = templen
					if templen > 12 {
						score += 1
					}
				}

			case 3:
				compareStr += "C"
			}

		} else {
			compareStr += "D"
		}
	}
	return
}

func CheckContain(b1, b2 string) (res int) {
	b1 = replaceStr.ReplaceAllString(b1, "")
	b2 = replaceStr.ReplaceAllString(b2, "")

	if b1 == b2 {
		res = 1 //相等
		return
	}
	bs1 := []rune(b1)
	bs2 := []rune(b2)
	tmp := ""
	for i := 0; i < len(bs1); i++ {
		for j := 0; j < len(bs2); j++ {
			if bs1[i] == bs2[j] {
				tmp += string(bs1[i])
			} else if tmp != "" {
				b1 = strings.Replace(b1, tmp, "", -1)
				b2 = strings.Replace(b2, tmp, "", -1)
				tmp = ""
			}
		}
	}
	if tmp != "" {
		b1 = strings.Replace(b1, tmp, "", -1)
		b2 = strings.Replace(b2, tmp, "", -1)
	}
	if b1 == b2 {
		res = 1 //相等
	} else if b1 == "" || b2 == "" {
		res = 2 //包含
	} else {
		res = 3 //不相等
	}
	return
}
