package events

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strings"

	"box-gm/utils"
	"time"

	// "log"
	"github.com/astaxie/beego"
	"github.com/tealeg/xlsx"
)

// 注册分布
func Register_get_day_time_count(date string, shareCode string) (map[int]int, int) {
	var timeCount = make(map[int]int)

	filename := fmt.Sprintf("%s/register-%s.log", utils.GetKeyConf("events", "path"), date)
	f, err := os.Open(filename)
	if err != nil {
		return timeCount, 0
	}
	defer f.Close()

	startTime := utils.GetTime(date + " 00:00:00")
	totalCount := 0

	buff := bufio.NewReader(f)
	for {
		line, err := buff.ReadString('\n')

		if (err != nil || io.EOF == err) && line == "" {
			break
		}
		line = strings.TrimSpace(line)

		arr := strings.Split(line, ";")
		if shareCode == "" || arr[3] == shareCode {
			eventTime := utils.GetTime(arr[1])

			sec := eventTime - startTime
			sec = int(math.Floor(float64(sec / 300)))
			timeCount[sec]++
			totalCount++
		}

		if err != nil || io.EOF == err {
			break
		}
	}

	return timeCount, totalCount
}

func Register_get_range_day_time_count(days int, shareCode string) ([]*tgSeries, []string) {
	var series []*tgSeries
	currTime := int(time.Now().Unix())
	dayCount := 0
	for {
		tiEvent := currTime - 86400*dayCount
		date := time.Unix(int64(tiEvent), 0).Format("2006-01-02")

		item := &tgSeries{}
		glCount, totalCount := Register_get_day_time_count(date, shareCode)
		item.Name = date + fmt.Sprintf(" 总人数:%d", totalCount)
		for i := 0; i < 289; i++ {
			item.Data = append(item.Data, glCount[i])
		}
		item.MapCount = glCount
		// log.Printf("item[%v]", item)
		series = append(series, item)

		dayCount++
		if dayCount > days {
			break
		}
	}

	var arrCate []string
	loc, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" 00:00:00", loc)
	for i := 0; i < 289; i++ {
		ti := theTime.Add(time.Minute * 5 * time.Duration(i))
		arrCate = append(arrCate, fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
	}
	return series, arrCate
}

// 保存文件
func Save_register_records(series []*tgSeries, user string) string {
	var file *xlsx.File
	var sheet *xlsx.Sheet
	var row *xlsx.Row
	var err error

	file = xlsx.NewFile()
	sheet, err = file.AddSheet("注册分布")
	if err != nil {
		beego.Warn(err)
		return ""
	}

	row = sheet.AddRow()
	row.AddCell().Value = "日期"
	row.AddCell().Value = "时间"
	row.AddCell().Value = "人数"

	loc, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" 00:00:00", loc)
	for i := 0; i < len(series); i++ {
		item := series[i]
		for k, v := range item.MapCount {
			row = sheet.AddRow()
			row.AddCell().SetString(item.Name)
			ti := theTime.Add(time.Minute * 5 * time.Duration(k))
			row.AddCell().SetString(fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
			row.AddCell().SetInt(v)
		}
	}

	name := fmt.Sprintf("./static/统计-注册-%s.xlsx", user)
	os.Remove(name)
	err = file.Save(name)
	if err != nil {
		beego.Warn(err)
		return ""
	}

	return name
}
