package models_table

import (
	"fmt"
	"kkscrap-go/locals"
	"reflect"
	"strings"
	"time"

	"github.com/dabory/abango-rest"
	e "github.com/dabory/abango-rest/etc"
	"github.com/go-xorm/xorm"
	"github.com/google/uuid"
)

func ComAddaRowDefault(y *abango.Controller, table interface{}) {

	fe := reflect.ValueOf(table).Elem()
	fieldNum := fe.NumField()
	for i := 0; i < fieldNum; i++ {
		tag := string(fe.Type().Field(i).Tag)
		name := fe.Type().Field(i).Name
		value := fe.Field(i)

		if strings.Contains(tag, "DECIMAL") && value.String() == "" {
			fe.Field(i).SetString("0.0000")
		} else if name == "CreatedOn" {
			fe.Field(i).SetInt(e.GetNowUnix())
		} else if name == "OfficialDate" {
			fe.Field(i).SetString((time.Now().Format("20060102")))
		} else if name == "OfficialTime" {
			fe.Field(i).SetString((time.Now().Format("15:04:05")))
		} else if name == "IsUnused" && value.String() == "" {
			fe.Field(i).SetString("0")
		} else if name == "Status" && value.String() == "" {
			fe.Field(i).SetString("0")
		} else if name == "Sort" && value.String() == "" {
			fe.Field(i).SetString("0")
		} else if name == "Ip" {
			fe.Field(i).SetString(y.Gtb.RemoteIp)
		} else if name == "UserId" && y.Gtb.UserId != 0 {
			fe.Field(i).SetInt(int64(y.Gtb.UserId))
		} else if name == "MemberId" && y.Gtb.MemberId != 0 {
			fe.Field(i).SetInt(int64(y.Gtb.MemberId))
		} else if name == "StorageId" {
			fe.Field(i).SetInt(int64(y.Gtb.StorageId))
		} else if name == "BranchId" {
			fe.Field(i).SetInt(int64(y.Gtb.BranchId))
		} else if name == "MemberBuyerId" && y.Gtb.MemberBuyerId != 0 {
			fe.Field(i).SetInt(int64(y.Gtb.MemberBuyerId))
		} else if name == "BuyerId" && value.String() == "" && y.Gtb.MemberBuyerId != 0 { //Pro에서 MemberBuyerId 지정되어 있으면
			fe.Field(i).SetInt(int64(y.Gtb.MemberBuyerId))
		} else if name == "ItemCode" && value.String() == "" { //UniqKey가 지정되어 있으면
			fe.Field(i).SetString(e.RandString(16))
			//ForeignId에  1 을 넣는 조건
		} else if name == "Duid" && value.String() == "" { //UniqKey가 지정되어 있으면
			uid := uuid.New()
			fe.Field(i).SetString(uid.String())
		}
		//redundunt but to make sure to have no 0 value in Ids
		if (strings.Contains(name, "MediaId") || name == "MemberId" || name == "UserId" || name == "BuyerId" || name == "SupplierId" || name == "IgroupId" || name == "CgroupId" || name == "BranchId" || name == "StorageId" || name == "AgroupId") && value.Int() == 0 {
			fe.Field(i).SetInt(int64(1))
		}
		if name == "FromBuyerId" && value.Int() == 0 {
			fe.Field(i).SetInt(int64(1))
		}
		if name == "FromBuyerId" && value.Int() == 0 {
			fmt.Println("FromBuyerId:", value.Int())
		}
	}
}

func EmptyTableColumn(table interface{}) { // 정리해서 연결할 것.
	fmt.Println("aaaa")
	fe := reflect.ValueOf(table).Elem()
	fmt.Println("bbbb")
	fieldNum := fe.NumField()
	fmt.Println("cccc")
	for i := 0; i < fieldNum; i++ {
		name := fe.Type().Field(i).Name
		if name == "Password" || name == "ActivateCode" || name == "ResetCode" {
			fe.Field(i).SetString("")
		}
	}
}

func ComEditaRowDefault(y *abango.Controller, table interface{}) {

	fe := reflect.ValueOf(table).Elem()
	fieldNum := fe.NumField()
	for i := 0; i < fieldNum; i++ {

		tag := string(fe.Type().Field(i).Tag)
		name := fe.Type().Field(i).Name
		value := fe.Field(i)

		if strings.Contains(tag, "DECIMAL") && value.String() == "" {
			fe.Field(i).SetString("0.0000")
		} else if name == "UpdatedOn" {
			fe.Field(i).SetInt(e.GetNowUnix())
		} else if name == "OfficialDate" && value.String() == "" { //직접수정시에는 변경가능
			fe.Field(i).SetString((time.Now().Format("20060102")))
		} else if name == "OfficialTime" && value.String() == "" { //직접수정시에는 변경가능
			fe.Field(i).SetString((time.Now().Format("15:04:05")))
		} else if name == "UserId" && value.String() == "" { // 수정한 관리자 입력
			fe.Field(i).SetInt(int64(y.Gtb.UserId))
		} else if name == "MemberId" && value.String() == "" { // 수정한 회원 입력
			fe.Field(i).SetInt(int64(y.Gtb.MemberId))
		} else if name == "MemberBuyerId" && value.String() == "" { // 수정한 회원 입력
			fe.Field(i).SetInt(int64(y.Gtb.MemberBuyerId))
		} else if name == "IsUnused" && value.String() == "" {
			fe.Field(i).SetString("0")
		} else if name == "Status" && value.String() == "" {
			fe.Field(i).SetString("0")
		} else if name == "Sort" && value.String() == "" {
			fe.Field(i).SetString("0")
		} else if name == "Ip" { //최종업데이트한 Ip가 들어간다.
			fe.Field(i).SetString(y.Gtb.RemoteIp)
		}

		if (strings.Contains(name, "MediaId") || name == "MemberId" || name == "UserId" || name == "BuyerId" || name == "SupplierId" || name == "IgroupId" || name == "CgroupId" || name == "BranchId" || name == "StorageId" || name == "AgroupId") && value.Int() == 0 {
			fe.Field(i).SetInt(int64(1))
		}
	}
}

func ComUpdateQry(y *abango.Controller, id int) *xorm.Session {

	qry := y.Db.Id(id)
	if y.UpdateFieldList != "" {
		slc := strings.Split(y.UpdateFieldList, ",")
		for _, str := range slc {
			qry = qry.Cols(str)
		}
	} else { //UpdateFieldList가 비었으면 전체 컬럼선텍
		qry = qry.AllCols()
	}
	return qry
}

func SyncEditaRowDefault(y *locals.SyncController, table interface{}) {

	fe := reflect.ValueOf(table).Elem()
	fieldNum := fe.NumField()
	for i := 0; i < fieldNum; i++ {

		tag := string(fe.Type().Field(i).Tag)
		name := fe.Type().Field(i).Name
		value := fe.Field(i)

		if strings.Contains(tag, "DECIMAL") && value.String() == "" {
			fe.Field(i).SetString("0.0000")
		} else if name == "UpdatedOn" {
			fe.Field(i).SetInt(e.GetNowUnix())
		} else if name == "Status" && value.String() == "" {
			fe.Field(i).SetString("0")
		} else if name == "Sort" && value.String() == "" {
			fe.Field(i).SetString("0")
		}

		if (strings.Contains(name, "SampleId")) && value.Int() == 0 {
			fe.Field(i).SetInt(int64(1))
		}
	}
}

func SyncUpdateQry(y *locals.SyncController, id int) *xorm.Session {

	qry := y.Db.Id(id)
	if y.UpdateFieldList != "" {
		slc := strings.Split(y.UpdateFieldList, ",")
		for _, str := range slc {
			qry = qry.Cols(str)
		}
	} else { //UpdateFieldList가 비었으면 전체 컬럼선텍
		qry = qry.AllCols()
	}
	return qry
}
