package redisdo

import (
	"errors"
	"fmt"
	"strconv"
	"strings"
	"time"
	//"github.com/garyburd/redigo/redis"
)

func GetPlayerKey(playerId int64) []byte {
	return strconv.AppendInt([]byte("onlineer:"), playerId, 10)
}

func GetGateServerKey(serverId int64) []byte {
	return strconv.AppendInt([]byte("gateServer:"), serverId, 10)
}

func scanKeys(st int) (int, []string, error) {
	rep, err := Do("scan", st)
	if err != nil {
		return -1, nil, err
	}
	rep2 := rep.([]interface{})
	if len(rep2) != 2 {
		return -1, nil, errors.New("replay length is error")
	}

	nextSt := -1
	if strSt, ok := rep2[0].([]byte); !ok {
		return -1, nil, errors.New("replay type is error")
	} else {
		st, err := strconv.Atoi(string(strSt))
		if err != nil {
			return -1, nil, err
		}
		nextSt = st
	}

	var lst []string
	if ks, ok := rep2[1].([]interface{}); !ok {
		return -1, nil, errors.New("replay type is error")
	} else {
		for _, v := range ks {
			if nv, ok := v.([]byte); ok {
				k := string(nv)
				lst = append(lst, k)
			}
		}
	}
	return nextSt, lst, nil
}

// 日任务到期 , 幸运抽奖到期 2022-05-01
func checkKeyFormatDate(val string, main string) {
	if !strings.HasSuffix(main, ":") {
		main += ":"
	}
	nv := strings.ReplaceAll(val, main, "")

	// 日期
	lst := strings.Split(nv, "-")
	if len(lst) == 3 {
		y, _ := strconv.Atoi(lst[0])
		m, _ := strconv.Atoi(lst[1])
		d, _ := strconv.Atoi(lst[2])

		tt := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.Local)
		if time.Since(tt) > time.Duration(14*24*time.Hour) {
			fmt.Printf("del from redis %v\n", val)
			//Do("del", val)
		}
	}
}

// 古玩排行 20220501
func checkKeyFormatDate2(val string, main string) {
	if !strings.HasSuffix(main, ":") {
		main += ":"
	}
	nv := strings.ReplaceAll(val, main, "")

	// 日期
	dt, _ := strconv.Atoi(nv)
	if dt > 20220000 {
		y := dt / 10000
		m := (dt % 10000) / 100
		d := dt % 100

		tt := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.Local)
		if time.Since(tt) > time.Duration(14*24*time.Hour) {
			fmt.Printf("del from redis %v\n", val)
			//Do("del", val)
		}
	}
}

// pvp key:21
func checkKeyFormatWeek(val string, main string) {
	if !strings.HasSuffix(main, ":") {
		main += ":"
	}
	nv := strings.ReplaceAll(val, main, "")
	twk, _ := strconv.Atoi(nv)

	//星期
	// 取当前星期 上星期 上上星期做列表，不在此范围内的都删除
	var lst []int
	for i := 0; i < 3; i++ {
		dn := -7 * i * 24
		_, wk := time.Now().Add(time.Duration(dn) * time.Hour).ISOWeek()
		lst = append(lst, wk)
	}

	for _, v := range lst {
		if v == twk {
			return
		}
	}

	//Do("del", val)
	fmt.Println("del from redis", val)
}

func ScanAll() {

	process := func(val string) {
		if !strings.Contains(val, ":") && !strings.Contains(val, "onlineStat") {
			Do("del", val)
			return
		}

		// 个人章节先不处理
		if strings.HasPrefix(val, "chapter") {
			return
		}

		if strings.HasPrefix(val, "day_task_v2") {
			checkKeyFormatDate(val, "day_task_v2")
			return
		}
		if strings.HasPrefix(val, "luck_draw_v2") {
			checkKeyFormatDate(val, "luck_draw_v2")
			return
		}

		// pvp
		if strings.HasPrefix(val, "pvpplayers") {
			checkKeyFormatWeek(val, "pvpplayers")
			return
		}
		if strings.HasPrefix(val, "pvphistorys") {
			checkKeyFormatWeek(val, "pvphistorys")
			return
		}
		if strings.HasPrefix(val, "ranking_pvp") {
			checkKeyFormatWeek(val, "ranking_pvp")
			return
		}
		if strings.HasPrefix(val, "pvpmode") {
			checkKeyFormatWeek(val, "pvpmode")
			return
		}
		// 无尽
		if strings.HasPrefix(val, "ranking_list") {
			checkKeyFormatWeek(val, "ranking_list")
			return
		}
		//古玩排行
		if strings.HasPrefix(val, "curio_ranking") {
			checkKeyFormatDate2(val, "curio_ranking")
			return
		}

		fmt.Println(val)
	}

	st := 0
	for i := 0; i < 1000; i++ {
		nst, lst, err := scanKeys(st)
		if err != nil {
			return
		}

		fmt.Println("scan pos:", st)
		for _, v := range lst {
			process(v)
		}

		st = nst
		if st <= 0 {
			return
		}
	}
}
