// totimestamp
package clear

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

var reg, regA, regB, regC, regD, regAfter ,regAfterBool*regexp.Regexp

const (
	T = 365 * 86400
)

var item = map[string]string{
	"一": "1", "二": "2", "三": "3", "四": "4", "五": "5",
	"六": "6", "七": "7", "九": "9", "十": "10", "零": "0", "〇": "0",
	"１": "1", "２": "2", "３": "3", "４": "4", "５": "5",
	"６": "6", "７": "7", "８": "8", "９": "9", "０": "0",
}

func init() {
	//二〇一五年十一月四日十五时
	reg, _ = regexp.Compile(`\d+`)
	regA, _ = regexp.Compile(`[一|二|三|四|五|六|七|八|九|十|零|〇|１|２|３|４|５|６|７|８|９|０]`)
	regB, _ = regexp.Compile(`\d+年\d+月\d+日((上|下)午)?\s*\d+[:：时]\d+分?[-—]\d+[:：时]\d+时?分?`)
	regC, _ = regexp.Compile(`\s*\d+[:：时]\d+分?[-—]`)
	regD, _ = regexp.Compile(`([一|二|三|四|五|六|七|八|九|十|零|〇]{4})年([一|二|三|四|五|六|七|八|九|十]{1,2})月([一|二|三|四|五|六|七|八|九|十]{1,3})日([一|二|三|四|五|六|七|八|九|十]{1,3})时`)
	regAfter, _ = regexp.Compile(`(下午D?\d{1,2}[时|:|：|h|H])`)
	regAfterBool, _ = regexp.Compile(`(下午D?[1-2][0-9][时|:|：|h|H])`)

}

/*字符时间转时间戳
支持全角
20060102->时间戳
20060102150405->时间戳
01%02->时间戳
2006%01%02->时间戳
2006%01%02%15->时间戳
2006%01%02%15%04->时间戳
2006%01%02%15%04%05->时间戳
*/
func ObjToTimestamp(data []interface{},spidercode ...string) []interface{} {
	tmp := fmt.Sprint(data[0])
	//处理类似：二〇一五年十一月四日十五时
	cht := regD.FindStringSubmatch(tmp)
	if len(cht) == 5 {
		y := chineseToNumber(cht[1])
		m := 0
		for _, v := range []rune(cht[2]) {
			it, _ := strconv.Atoi(item[string(v)])
			m += it
		}
		d := 0
		for _, v := range []rune(cht[3]) {
			it, _ := strconv.Atoi(item[string(v)])
			d += it
		}
		M := 0
		for _, v := range []rune(cht[4]) {
			it, _ := strconv.Atoi(item[string(v)])
			M += it
		}
		tmp = fmt.Sprintf("%s年%d月%d日%d时", y, m, d, M)
	}
	//2016年12月7日上午9:00-11：30时 时间范围处理 取后面的时间
	if regB.MatchString(tmp) {
		tmp = regC.ReplaceAllString(tmp, "")
	}
	//2017年11月13日下午3时30分
	addreptime := int64(0)
	//2021年09月10日下午15时30分
	if regAfter.MatchString(tmp) && !regAfterBool.MatchString(tmp) {
		addreptime = 12 * 60 * 60
	}
	regRepl, _ := regexp.Compile(`[,，]`)
	tmp = regRepl.ReplaceAllString(tmp, "")
	for _, v := range spaces {
		strings.Replace(tmp, v, " ", -1)
	}
	tmps := reg.FindAllString(chineseToNumber(tmp), -1)
	//处理类似2016-12-0909:30:00时间
	if len(tmps) > 2 && len(tmps[2]) > 2 {
		newtmp := []string{}
		for k, v := range tmps {
			if k == 2 {
				newtmp = append(newtmp, v[0:2], v[2:])
			} else {
				newtmp = append(newtmp, v)
			}
		}
		tmps = newtmp
	}
	timestr := "" //2006-01-02 15:04:05
	timestamp := int64(0)
	if len(tmps) == 1 {
		if len(tmps[0]) == 8 {
			timestr = tmps[0][0:4] + "-" + tmps[0][4:6] + "-" + tmps[0][6:8]
			t, _ := time.ParseInLocation("2006-01-02-15-04", timestr+"-09-00", time.Local)
			timestamp = t.Unix()
		} else if len(tmps[0]) == 14 {
			timestr = tmps[0][0:4] + "-" + tmps[0][4:6] + "-" + tmps[0][6:8] + " " + tmps[0][8:10] + ":" + tmps[0][10:12] + ":" + tmps[0][12:14]
			t, _ := time.ParseInLocation("2006-01-02 15:04:00", timestr, time.Local)
			timestamp = t.Unix()
		}
	} else if len(tmps) == 2 {
		timestr = fmt.Sprint(time.Now().Year()) + "-" + MDhmsRepair(tmps[0]) + "-" + MDhmsRepair(tmps[1])
		t, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
		timestamp = t.Unix()
	} else if len(tmps) == 3 {
		timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2])
		t, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
		timestamp = t.Unix()
	} else if len(tmps) == 4 {
		timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2]) + " " + MDhmsRepair(tmps[3])
		t, _ := time.ParseInLocation("2006-01-02 15", timestr, time.Local)
		timestamp = t.Unix()
	} else if len(tmps) >= 5 {
		timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2]) + " " + MDhmsRepair(tmps[3]) + ":" + MDhmsRepair(tmps[4])
		t, _ := time.ParseInLocation("2006-01-02 15:04", timestr, time.Local)
		timestamp = t.Unix()
	}
	if timestamp <= 0 || timestamp > (time.Now().Unix()+T) {
		data[0] = ""
	} else {
		if addreptime > 0 {
			timestamp += addreptime
		}
		data[0] = timestamp
	}
	return data
}

//补位
func MDhmsRepair(t string) string {
	if len(t) == 1 {
		return "0" + t
	} else {
		return t
	}
}

//汉子数和全角转数字
func chineseToNumber(con string) string {
	tmp := regA.ReplaceAllStringFunc(con, func(key string) string {
		if item[key] != "" {
			return item[key]
		} else {
			return key
		}
		return key
	})
	return tmp
}
