package model

import (
	"active/tools"
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"git.jiaxianghudong.com/go/logs"
	"git.jiaxianghudong.com/webs/pkg/pb"
)

//OpenTimes 开始时间
type OpenTimes struct {
	Start string `json:"time_start"`
	End   string `json:"time_end"`
}

//AdsOpenTimes 开始时间
type AdsOpenTimes struct {
	Start string `json:"istimeoffon_time_start"`
	End   string `json:"istimeoffon_time_end"`
}

//Tagdata 活动数据
type Tagdata struct {
	Start            string        `json:"radix,omitempty"`
	RebateType       string        `json:"rebate_type,omitempty"`
	GoodIDs          string        `json:"goodids,omitempty"`
	RuleImg          string        `json:"rule_bgimg,omitempty"`
	ActiveModel      string        `json:"active_model,omitempty"`
	ActiveType       string        `json:"active_type,omitempty"`
	GoodsType        string        `json:"goods_type,omitempty"`
	GrandTotal       []interface{} `json:"grand_total,omitempty"`
	ActiveTitleImg   []interface{} `json:"active_title_img,omitempty"`
	InvestmentRebate []interface{} `json:"investment_rebate,omitempty"`
	GiftBag          []interface{} `json:"gift_bag,omitempty"`
	Welfare          []interface{} `json:"welfare,omitempty"`
	Rank             []interface{} `json:"rank,omitempty"`
}

//CheckOpenCondition 判断限制渠道，包，地区
func CheckOpenCondition(u *pb.User, openCond string) bool {

	if openCond != "" {
		opencondition := make(map[string]string)
		err := json.Unmarshal([]byte(openCond), &opencondition)
		if err != nil {
			logs.Errorf("Opencondition: %s err: %v", openCond, err)
			return false
		}

		if val, ok := opencondition["channel_app"]; ok && val != "" {
			channelapp := strings.Split(val, ",")
			if !tools.InStringArray(fmt.Sprintf("%d-%d", u.ChannelID, u.AppID), channelapp) {
				return false
			}
		}

		if val, ok := opencondition["region"]; ok && val != "000000" && val != "0" && val != u.Region {
			logs.Debug("region != 000000 and != ", u.Region, " continue")
			return false
		}
	}

	return true
}

//GetOpenTime 获取活动时间
func (opentm *OpenTimes) GetOpenTime(tm string) {
	if tm != "" {
		err := json.Unmarshal([]byte(tm), &opentm)
		if err != nil {
			logs.Error(fmt.Sprintf("json opentime:%s err:%v", tm, err))
		}
	}
}

//CompareTime 活动时间判断
func (opentm *OpenTimes) CompareTime() (bool, string) {

	if time.Now().Unix() < tools.TimeParseUnix(opentm.Start, "2006-01-02 15:04:05") || (opentm.Start == "" && opentm.End == "") {
		return false, "活动尚未开启！"
	}
	if time.Now().Unix() > tools.TimeParseUnix(opentm.End, "2006-01-02 15:04:05") {
		return false, "活动已经结束！"
	}

	return true, ""
}

//CompareRegTime 注册时间判断
func (opentm *OpenTimes) CompareRegTime(unix int64) bool {
	if unix < tools.TimeParseUnix(opentm.Start, "2006-01-02 15:04:05") || (opentm.Start == "" && opentm.End == "") {
		return false
	}
	return true
}

//CheckTimeOut 显示时间
func CheckTimeOut(begin, end string) (bool, string) {

	if time.Now().Unix() < tools.TimeParseUnix(begin, "2006-01-02 15:04:05") {
		return false, "当前活动领取时间尚未开启，敬请期待！"
	}
	if time.Now().Unix() > tools.TimeParseUnix(end, "2006-01-02 15:04:05") {
		return false, "当前活动领取时间已结束，谢谢参与！"
	}
	return true, ""
}

//GetOpenTime 获取活动时间
func (adtm *AdsOpenTimes) GetOpenTime(tm string) {
	if tm != "" {
		err := json.Unmarshal([]byte(tm), &adtm)
		if err != nil {
			logs.Error(fmt.Sprintf("json opentime:%s err:%v", tm, err))
		}
	}
}

//CompareTime 活动时间判断
func (adtm *AdsOpenTimes) CompareTime() (bool, string) {

	if time.Now().Unix() < tools.TimeParseUnix(adtm.Start, "2006-01-02 15:04:05") || (adtm.Start == "" && adtm.End == "") {
		return false, "活动尚未开启！"
	}
	if time.Now().Unix() > tools.TimeParseUnix(adtm.End, "2006-01-02 15:04:05") {
		return false, "活动已经结束！"
	}

	return true, ""
}
