package jobs

import (
	"encoding/json"
	"gadmin/config"
	"gadmin/internal/gorm/model"
	"gadmin/internal/gorm/query"
	"github.com/sirupsen/logrus"
	"gorm.io/gen"
)

var FilterInitDps = new(jFilterInitDps)

type jFilterInitDps struct{}

func (j *jFilterInitDps) Run() {
	logrus.Info("jFilterInitDps Run.....")
	j.filter()
}

func (j *jFilterInitDps) filter() {

	var (
		results []*model.ChapterLogsUserDetail
		d       = query.Use(config.DB).ChapterLogsUserDetail
		bp      = query.Use(config.DB).PlayerBattleDp
	)

	err := d.Select(d.ID, d.UserID, d.Extra).
		Where(d.EventID.Eq(13)).
		FindInBatches(&results, 1000, func(tx gen.Dao, batch int) error {
			for _, result := range results {
				dps := j.parseMaxDPS(result.Extra)
				if dps == 0 {
					continue
				}

				arl, _ := bp.Where(bp.Playerid.Eq(result.UserID)).First()
				if arl == nil {
					if err := query.Use(config.DB).PlayerBattleDp.
						Where(bp.Playerid.Eq(result.UserID)).
						Create(&model.PlayerBattleDp{DetailsID: result.ID, Playerid: result.UserID, Dps: dps}); err != nil {
						panic(err)
					}
					continue
				}

				if arl.Dps >= dps {
					continue
				}

				if err := bp.Save(arl); err != nil {
					panic(err)
				}

				if _, err := query.Use(config.DB).PlayerBattleDp.
					Where(bp.Playerid.Eq(result.UserID)).
					Updates(&model.PlayerBattleDp{Dps: dps, DetailsID: result.ID}); err != nil {
					panic(err)
				}

			}
			return nil
		})

	logrus.Warnf("run ok, err:%+v", err)
}

func (j *jFilterInitDps) parseMaxDPS(extraStr string) float64 {
	type Extra struct {
		Equip struct {
			Attributes struct {
				InitDps float64 `json:"initDps"`
			} `json:"attributes"`
			TempAttributes struct {
				InitDps float64 `json:"initDps"`
			} `json:"tempAttributes"`
		} `json:"equip"`
	}

	type Extra2 struct {
		Attr []struct {
			Attr struct {
				InitDps float64 `json:"initDps"`
			} `json:"attr"`
		} `json:"attr"`
	}

	type Extra3 struct {
		Attr struct {
			InitDps float64 `json:"initDps"`
		} `json:"attr"`
		TempAttr struct {
			InitDps float64 `json:"initDps"`
		} `json:"tempAttr"`
	}
	var (
		extra1 *Extra
		extra2 *Extra2
		extra3 *Extra3
		dps    float64
	)
	err1 := json.Unmarshal([]byte(extraStr), &extra1)
	err2 := json.Unmarshal([]byte(extraStr), &extra2)
	err3 := json.Unmarshal([]byte(extraStr), &extra3)

	if err1 != nil && err2 != nil && err3 != nil {
		logrus.Errorf("Unmarshal extra1 err1:%+v,err2:%+v,err3：%+v", err1, err2, err3)
		return 0
	}

	if err1 == nil {
		if extra1.Equip.Attributes.InitDps > extra1.Equip.TempAttributes.InitDps {
			dps = extra1.Equip.Attributes.InitDps
		} else {
			dps = extra1.Equip.TempAttributes.InitDps
		}
	}

	if err2 == nil && len(extra2.Attr) > 0 {
		for _, v := range extra2.Attr {
			if v.Attr.InitDps > dps {
				dps = v.Attr.InitDps
			}
		}
	}

	if err3 == nil {
		var tmpDps float64 = 0
		if extra3.Attr.InitDps > extra3.TempAttr.InitDps {
			tmpDps = extra3.Attr.InitDps
		} else {
			tmpDps = extra3.TempAttr.InitDps
		}

		if tmpDps > dps {
			dps = tmpDps
		}
	}

	if dps <= 50000 {
		return 0
	}

	return dps
}
