package events

import (
	"fmt"
	"box-gm/utils"
	"log"
	"strconv"
	"time"

	"github.com/garyburd/redigo/redis"
)

func AccountQuery_get_account_info(account string) (role_map map[string]map[string]string, account_status string) {
	if account == "" {
		return
	}
	uid_map, err := utils.Hgetall(fmt.Sprintf("tb_acc:%s", account))
	if err != nil || len(uid_map) == 0 {
		return
	}
	role_map = make(map[string]map[string]string)
	for k, v := range uid_map {
		if k != "status" && v != "" {
			info := PlayerQuery_get_player_info(v)
			role_map[v] = info
		}
		if k == "status" {
			account_status = v
		}
	}
	if account_status == "" {
		account_status = "0"
	}
	return
}

func AccountQuery_set_account_ban(account string, status int) error {
	if account == "" {
		return nil
	}
	err := utils.Hset(fmt.Sprintf("tb_acc:%s", account), "status", status)
	if err != nil {
		log.Printf("AccountQuery_set_account_ban :account[%v] err[%v]", account, err)
	}
	return err
}

func AccountQuery_get_account_status(account string) int {
	if account == "" {
		return 0
	}
	rep, err := utils.Hget(fmt.Sprintf("tb_acc:%s", account), "status")
	status, err := redis.Int(rep, err)
	if err != nil {
		log.Printf("AccountQuery_get_account_status :account[%v] err[%v]", account, err)
	}
	return status
}

func AccountQuery_get_show_data(account string) (role_map map[string]map[string]string, account_status string) {
	role_map, account_status = AccountQuery_get_account_info(account)
	if len(role_map) != 0 {

		for _, info := range role_map {
			for k, v := range info {
				if k == "login_time" || k == "logout_time" || k == "create_time" {
					tick, err := strconv.ParseInt(v, 10, 64)
					if err == nil {
						info[k] = time.Unix(tick, 0).Format("2006-01-02 15:04:05")
					}
				}
				if k == "status" {
					if v == "0" {
						info[k] = "正常"
					}
					if v == "1" {
						info[k] = "封禁"
					}
				}
			}
		}
		if account_status == "0" {
			account_status = "正常"
		}
		if account_status == "1" {
			account_status = "封禁"
		}
	}
	return role_map, account_status
}
