package spiderutil

import (
	"qfw/util"
	"regexp"
	"strings"
	"time"
	//"unicode/utf8"
)

var (
	HanDate     = regexp.MustCompile(`([一|二|三|四|五|六|七|八|九|十|零|〇|\d]{4})年([元|一|二|三|四|五|六|七|八|九|十|\d]{1,2})月([一|二|三|四|五|六|七|八|九|十|\d]{1,3})日`)
	Date1       = regexp.MustCompile("(20|19)\\d{2}[\\s\u3000\u2003\u00a0]{0,}年[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}月[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}日")
	Date2       = regexp.MustCompile("(20|19)\\d{2}[\\s\u3000\u2003\u00a0]{0,}[-/.]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}[-/.]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}")
	Date3       = regexp.MustCompile("发(布|表)(时间|日期)(从)?[\\s\u3000\u2003\u00a0:：]{0,}\\d{4}[\\s\u3000\u2003\u00a0]{0,}[-/.年]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}[-/.月]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}日{0,}")
	Date4       = regexp.MustCompile("(发布|发售时间|领取开始(及|和)截止时间|报名|公告|公示|开标|招标|询价|填报|(购买)?(文件|标书)(发售|出售|获取|起止|提供下载|购买|提供))(发布|起止)?(开始)?(时间|日期|期限)[\\s\u3000\u2003\u00a0:：从于自为]*\\d{4}[\\s\u3000\u2003\u00a0]{0,}[-/.年]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}[-/.月]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}日{0,}")
	Date5       = regexp.MustCompile("于[\\s\u3000\u2003\u00a0]{0,}\\d{4}[\\s\u3000\u2003\u00a0]{0,}[-/.年][\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}[-/.月][\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}[-/.日].*?")
	Date6       = regexp.MustCompile("(报名|招标|填报|(文件|标书)(的)?(发售|出售|获取|起止|提供下载|购买|提供))(发布)?(开始)?(时间|日期|期限).*?\\d{4}[\\s\u3000\u2003\u00a0]{0,}[-/.年]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}[-/.月]{1,}[\\s\u3000\u2003\u00a0]{0,}\\d{1,2}[\\s\u3000\u2003\u00a0]{0,}日{0,}")
	Date7       = regexp.MustCompile("(公示|公告|报名)(期限|时间)[\\s\u3000\u2003\u00a0:：从于自为]*[\\s\u3000\u2003\u00a0]*\\d{4}[\\s\u3000\u2003\u00a0]*[-/.年][\\s\u3000\u2003\u00a0]*\\d{1,2}[\\s\u3000\u2003\u00a0]*[-/.月][\\s\u3000\u2003\u00a0]*\\d{1,2}[\\s\u3000\u2003\u00a0]*[-/.日]*")
	FilterReg   = regexp.MustCompile("<[^>]+>")
	FilterReg2  = regexp.MustCompile("[a-zA-Z]+")
	RegEndSpace = regexp.MustCompile("[\\s\u3000\u2003\u00a0]+$")
	RegSpace    = regexp.MustCompile("[\\s\u3000\u2003\u00a0]+")
	HanNum      = map[string]interface{}{
		"零":  "0",
		"〇":  "0",
		"一":  "1",
		"元":  "1",
		"二":  "2",
		"三":  "3",
		"四":  "4",
		"五":  "5",
		"六":  "6",
		"七":  "7",
		"八":  "8",
		"九":  "9",
		"十":  "10",
		"年":  "-",
		"月":  "-",
		"日":  "",
		"/":  "-",
		".":  "-",
		"二十": "2",
		"三十": "3",
	}
)

func GetPublishtime(textArr []string) string {
	defer util.Catch()
	for _, text := range textArr {
		if text != "" {
			t, b := GetPublishtimeByText(text)
			if b && t < time.Now().Unix() {
				return util.FormatDateByInt64(&t, util.Date_Full_Layout)
			}
		}
	}
	return "0"
}

func GetPublishtimeByText(text string) (int64, bool) {
	defer util.Catch()
	runeDetail := []rune(text)
	length := len(runeDetail)
	if length < 30 {
		return int64(0), false
	}
	text = FilterReg.ReplaceAllString(text, "")   //去除html标签
	text = FilterReg2.ReplaceAllString(text, "")  //去除字母
	text = RegEndSpace.ReplaceAllString(text, "") //去除结尾空格
	text = RegSpace.ReplaceAllString(text, "")    //去除空格
	result := text
	if length > 70 {
		result = string(runeDetail[length-70:])
	}

	//1、结尾时间
	result = strings.ReplaceAll(result, "○", "0")
	publishtime, endMatch := MatchDate(result)
	if !endMatch { //开头时间
		if length > 70 {
			result = string(runeDetail[:70])
		}
		result = strings.ReplaceAll(result, "○", "0")
		publishtime, endMatch = MatchDate(result)
	}

	//2.发布时间
	if !endMatch { //结尾时间没有匹配到
		if date := Date3.FindString(text); date != "" {
			publishtime, endMatch = MatchDate(date)

		}
	}
	//4.公告(开始)时间
	if !endMatch {
		if date := Date4.FindString(text); date != "" {
			publishtime, endMatch = MatchDate(date)
		}
	}
	//5.
	if !endMatch {
		if date := Date6.FindString(text); date != "" {
			publishtime, endMatch = MatchDate(date)
		}
	}
	//6.定于2021年1月18日
	if !endMatch {
		if date := Date5.FindString(text); date != "" {
			date = RegSpace.ReplaceAllString(date, "")
			date = Date1.FindString(date)
			d := ""
			for _, r := range []rune(date) {
				sr := string(r)
				if srH := HanNum[sr]; srH != nil {
					d = d + util.ObjToString(srH)
				} else {
					d = d + sr
				}
			}
			d = CompletDate(d)
			if d != "" {
				t, err := time.ParseInLocation(util.Date_Short_Layout, d, time.Local)
				if err == nil {
					publishtime = t.Unix()
					endMatch = true
				}
			}
		}
	}

	//if date := Date7.FindString(text); date != "" {
	//	publishtime, _ = MatchDate( date)
	//}
	return publishtime, endMatch
}

func MatchDate(text string) (int64, bool) {
	defer util.Catch()
	var dateArr []int64
	for _, reg := range []*regexp.Regexp{HanDate, Date1, Date2} {
		date := reg.FindAllString(text, -1)
		for _, v := range date {
			d := replaceDate(v)
			if d != "" {
				t, err := time.ParseInLocation(util.Date_Short_Layout, d, time.Local)
				if err == nil {
					dateArr = append(dateArr, t.Unix())
				}
			}
		}
	}
	if len(dateArr) > 0 {
		minDate := int64(0)
		for i, d := range dateArr {
			if i == 0 {
				minDate = d
			} else {
				if d < minDate {
					minDate = d
				}
			}
		}
		return minDate, true
	} else {
		return int64(0), false
	}
}

func replaceDate(s string) string {
	defer util.Catch()
	dateText := RegSpace.ReplaceAllString(s, "")
	d := ""
	for _, r := range []rune(dateText) {
		sr := string(r)
		if srH := HanNum[sr]; srH != nil {
			d = d + util.ObjToString(srH)
		} else {
			d = d + sr
		}
	}
	return CompletDate(d)
}

//2020-2-2 ->2020-02-02
func CompletDate(date string) string {
	defer util.Catch()
	result := ""
	dateArr := strings.Split(date, "-")
	if len(dateArr) == 3 {
		result = result + dateArr[0] + "-"
		r1 := dateArr[1]
		r2 := dateArr[2]
		if len(r1) == 1 {
			result = result + "0" + r1 + "-"
		} else if len(r1) == 3 {
			result = result + string(r1[0]) + string(r1[2]) + "-"
		} else {
			result = result + r1 + "-"
		}
		if len(r2) == 1 {
			result = result + "0" + r2
		} else if len(r2) == 3 {
			result = result + string(r2[0]) + string(r2[2])
		} else if len(r2) == 4 {
			if strings.HasPrefix(r2, "210") {
				result = result + "2" + string(r2[3])
			} else if strings.HasPrefix(r2, "310") {
				result = result + "3" + string(r2[3])
			}
		} else {
			result = result + r2
		}
	}
	return result
}
