package util

import (
	"go.mongodb.org/mongo-driver/bson/primitive"
	"qfw/util"
	"regexp"
	"sort"
	"strings"
)

// 审批数据判断字段,包含三个以上属于拟建
//var spFields = []string{"approvecode", "owner", "projectaddr", "project_scale_info", "projectname"}

// ChargeDetailResult 检测内容是否符合 toptype=结果
func ChargeDetailResult(detail string) bool {
	if ChargeDetailZB(detail) || ChargeDetailCJ(detail) {
		return true
	}
	return false
}

// ChargeDetailCJ 检测内容是否符合 toptype=结果；subtype=成交
func ChargeDetailCJ(detail string) bool {
	var reg = regexp.MustCompile("(成交单位|成交人|成交供应商)[:：][\\s ]*(.{2,25}(公司))")
	//含有 投标截止时间 关键词，代表不是结果，需要排除
	var reg2 = regexp.MustCompile("(投标截止时间)")
	res1 := reg.MatchString(detail)
	res2 := reg2.MatchString(detail)
	//针对 中南民族大学采购综合管理服务平台 站点 表格数据，匹配成交单位
	pattern := `<tr>.*<td>.*(成交供应商|成交单位|成交人)<br/>.*</td>.*<td.*>*(.{2,25}(公司))<br/>.*</td>.*</tr>`
	re := regexp.MustCompile(pattern)
	match := re.MatchString(detail)

	if !res2 && res1 || !res2 && match {
		return true
	}
	return false
}

// ChargeDetailZB 检测内容是否符合 toptype=结果；subtype=中标
func ChargeDetailZB(detail string) bool {
	var reg = regexp.MustCompile("(中标人单位名称|中标供应商|中标供应商名称|中标人名称|中签单位名称|中签单位|中标商家)[:：][\\s ]*(.{2,25}(公司|单位|局|厅))")
	var reg2 = regexp.MustCompile("(投标截止时间)")
	res1 := reg.MatchString(detail)
	res2 := reg2.MatchString(detail)

	if res1 && !res2 {
		return true
	}
	return false
}

// GetJyKey 免费订阅：o_jy.a_key.key/appendkey
func GetJyKey(data map[string]interface{}) (res []interface{}) {
	// 获取o_jy.a_key.key的值
	if ojy, ok := data["o_jy"].(map[string]interface{}); ok {
		if akey, ok := ojy["a_key"].(primitive.A); ok {
			for _, v := range akey {
				if m, ok := v.(map[string]interface{}); ok {
					if key, ok := m["key"].(primitive.A); ok {
						res = append(res, key...)
					}
					if key, ok := m["appendkey"].(primitive.A); ok {
						res = append(res, key...)
					}
				}
			}
		}
	}

	return
}

// GetVipKey 超级订阅：o_vipjy.a_items.a_key.key/appendkey
func GetVipKey(data map[string]interface{}) (res []interface{}) {
	//o_vipjy.a_items.a_key.key
	if o_vipjy, ok := data["o_vipjy"].(map[string]interface{}); ok {
		if aItems, ok := o_vipjy["a_items"].(primitive.A); ok {
			for _, aItem := range aItems {
				if a_k, ok := aItem.(map[string]interface{}); ok {
					if aKey, ok := a_k["a_key"].(primitive.A); ok {
						for _, ak := range aKey {
							if va, ok := ak.(map[string]interface{}); ok {
								if key, ok := va["key"].(primitive.A); ok {
									res = append(res, key...)
								}
								if key, ok := va["appendkey"].(primitive.A); ok {
									res = append(res, key...)
								}
							}
						}
					}
				}
			}
		}
	}

	return
}

// GetMemberKey 大会员订阅：o_member_jy.a_items.a_key.key/appendkey
func GetMemberKey(data map[string]interface{}) (res []interface{}) {
	//o_member_jy.a_items.a_key.key
	if o_member_jy, ok := data["o_member_jy"].(map[string]interface{}); ok {
		if aItems, ok := o_member_jy["a_items"].(primitive.A); ok {
			for _, aItem := range aItems {
				if tem, ok := aItem.(map[string]interface{}); ok {
					if aKey, ok := tem["a_key"].(primitive.A); ok {
						for _, ak := range aKey {
							if va, ok := ak.(map[string]interface{}); ok {
								if key, ok := va["key"].(primitive.A); ok {
									res = append(res, key...)
								}
								if key, ok := va["appendkey"].(primitive.A); ok {
									res = append(res, key...)
								}
							}
						}
					}
				}
			}
		}
	}

	return
}

// RemoveDuplicates 过滤重复数据
func RemoveDuplicates(elements []interface{}) []interface{} {
	encountered := map[interface{}]bool{}
	result := []interface{}{}
	for _, v := range elements {
		if encountered[v] == true {
			continue
		} else {
			encountered[v] = true
			result = append(result, v)
		}
	}
	return result
}

// GetUserKeys 获取用户订阅关键词
func GetUserKeys(data map[string]interface{}) (res []interface{}) {
	keys := GetJyKey(data)
	vkeys := GetVipKey(data)
	mkeys := GetMemberKey(data)

	res = append(res, keys...)
	res = append(res, vkeys...)
	res = append(res, mkeys...)
	res = RemoveDuplicates(res)

	return
}

// RemoveDuplicateString 去除重复字符串
func RemoveDuplicateString(arr []string) []string {
	encountered := map[string]bool{}
	result := []string{}

	for _, v := range arr {
		if encountered[v] == true {
			// Do not add duplicate.
		} else {
			// Append value to result slice.
			result = append(result, v)
			// Record this element as an encountered element.
			encountered[v] = true
		}
	}
	// Return the new slice.
	return result
}

// DealYuce 处理一级是预告，符合条件到采购意向
func DealYuce(data string) bool {
	//标题含有招标计划，并且含有 预公告或者预公示，分类划分到采购意向
	re1 := strings.Contains(data, "招标计划")
	re2 := strings.Contains(data, "预公告")
	re3 := strings.Contains(data, "预公示")
	if re1 && (re2 || re3) {
		return true
	}

	return false
}

// IsPurchasingIntent 采购意向判断处理,符合采购意向返回true
func IsPurchasingIntent(data map[string]interface{}) bool {
	if channel, ok := data["channel"]; ok && util.ObjToString(channel) != "" {
		if strings.Contains(channel.(string), "项目登记") {
			return true
		}
	}

	if title, ok := data["title"]; ok && util.ObjToString(title) != "" {
		if strings.HasPrefix(title.(string), "【项目登记】") {
			return true
		}
		if strings.HasSuffix(title.(string), "项目登记") {
			return true
		}
	}

	return false
}

// ProcessTopscopeclass 处理行业分类
func ProcessTopscopeclass(tops, subs []string) ([]string, []string) {
	// 去除 tops 中每个元素末尾的不固定字符
	cleanedTops := make([]string, 0)
	for _, top := range tops {
		parts := strings.Split(top, "")
		cleanedTop := strings.Join(parts[:len(parts)-1], "")
		if !IsInStringArray(cleanedTop, cleanedTops) {
			cleanedTops = append(cleanedTops, cleanedTop)
		}
	}

	// 用于标记 cleanedTops 中已存在于 subs 的元素
	presentMap := make(map[string]bool)

	// 遍历 subs 数组，标记已存在的 cleanedTops 元素
	for _, sub := range subs {
		for _, top := range cleanedTops {
			if strings.Contains(sub, top) {
				presentMap[top] = true
			}
		}
	}

	// 补充缺失的 cleanedTops 元素到 subs 中
	for _, top := range cleanedTops {
		if !presentMap[top] {
			subs = append(subs, top+"_其它")
		}
	}

	return tops, subs
}

// IsInStringArray 判断数组中是否存在字符串
func IsInStringArray(str string, arr []string) bool {
	// 先对字符串数组进行排序
	sort.Strings(arr)
	// 使用二分查找算法查找字符串
	pos := sort.SearchStrings(arr, str)
	// 如果找到了则返回 true，否则返回 false
	return pos < len(arr) && arr[pos] == str
}
