package wework

import (
	"fmt"
	"github.com/silenceper/wechat/v2"
	"github.com/silenceper/wechat/v2/cache"
	"github.com/silenceper/wechat/v2/work/config"
	"github.com/silenceper/wechat/v2/work/msgaudit"
	"github.com/xen0n/go-workwx"
	_ "github.com/xen0n/go-workwx" // package workwx
)

var (
	WeWorkCorpID = "ww5008ae926e352838"
	WeWorkFinanceCorpSecret = "-A48aGB1zZYURAPJBgYk5Lpaqx_j7sGgUL7gUVfzfcc" //企业微信 会话存档secret
	WeWorkExternalContactCorpSecret = "LgKgtar8gxnKlh_GO8VcvDa9V1oEO_A8o5qPjQoj4to" //企业微信 客户联系 secret
	//WeWorkContactCorpSecret = "ER4eEIski4oGVXjSwZ2uCTL_r0h5qERQz8vvOxE3xsM" //企业微信 通讯录 secret
	agentID = int64(0)
	UserNameAgentID = int64(1000011)
	WeWorkUserNameAgentSecret = "fkzl42sVb4OuwePxJ1WaUvhCAfpwiCyIG_1hLJzF8Qc" //企业微信 自建应用的获取员工姓名的 secret

	NotifyToken = "uPK8fG9xHfQaWuhNnGGEhN"
	NotifyEncodingAESKey = "N57BVy29HoxTlSlCJfm7cwLWu1KUjvEu5oqnyJXuMfm"
	WeWorkFinanceCorpRasPrivateKey = `
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAtbdCeRcXRrq/4sa8k/1aCRmSjZxHYKy/wG/iqX5GkX86T8xi
ZOUgD+2eVKGwa8PJK2aH6DGWnGTDUgPr4/vTyToSf4roA+pJADOA4dtIlxUv3Rh2
n+fI/O8spk+IZnZshhQIkpfYQUH9XKOm8EHAA9yYk3fmZh60iJ9R3cvQoscqEC+6
K0VnlKvfiz3XpB+zK9yIOWWpqdtjIHWOCKzwZKqTjSheLBfDDKmu7N/YMquFBLvf
rIPW9A1cBAo4b4tR8fbciuhlQymZQjn1RQAidZDwjnGZCTm10G/3xZSw8BNeTqjt
TThTk0YTxE1fnEVKwfSFk6BxQDVcg40u3K960QIDAQABAoIBAAOMCHbjIb/ucMTt
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o90MWXIeDIR2EhP
nCGRqeOfiUR69ZrjOohlt//2PWmlE1Y8v/pKmwfqZdB/xtn3klxJaAy708TrjDe2
+0PzPXuFP5iyIxa44mEakRvw9RbCvXg5VNiwztHoWj+Om8P9+LMCgw==
-----END RSA PRIVATE KEY-----`
)

type WeWorkMsgAuditClient struct {
	Client  *msgaudit.Client
}

type WeWorkExternalContactClient struct {
	Client *workwx.WorkwxApp
}

type WeWorkMsgAuditApiClient struct {
	Client *workwx.WorkwxApp
}

type WeWorkUserNameClient struct {
	Client *workwx.WorkwxApp
}

func NewWeWorkMsgAuditClient() *WeWorkMsgAuditClient {
	wc := wechat.NewWechat()
	memory := cache.NewMemory()
	//memory := cache.NewRedis(global.Redis)
	cfg := &config.Config{
		CorpID:         WeWorkCorpID,
		CorpSecret:     WeWorkFinanceCorpSecret,
		AgentID:        "",
		Cache:          memory,
		RasPrivateKey:  WeWorkFinanceCorpRasPrivateKey,
		Token:          "",
		EncodingAESKey: "",
	}

	wework := wc.GetWork(cfg)
	client, err := wework.GetMsgAudit()
	if err != nil {
		return nil
	}
	return &WeWorkMsgAuditClient{
		Client: client,
	}
}

func NewWeWorkExternalContactClient() *WeWorkExternalContactClient {
	client := workwx.New(WeWorkCorpID)

	// work with individual apps
	app := client.WithApp(WeWorkExternalContactCorpSecret, agentID)
	return &WeWorkExternalContactClient{
		Client: app,
	}
}

func NewWeWorkUserNameClient() *WeWorkUserNameClient {
	client := workwx.New(WeWorkCorpID)

	// work with individual apps
	app := client.WithApp(WeWorkUserNameAgentSecret, UserNameAgentID)
	return &WeWorkUserNameClient{
		Client: app,
	}
}

func NewWeWorkMsgAuditApiClient() *WeWorkMsgAuditApiClient {
	client := workwx.New(WeWorkCorpID)

	// work with individual apps
	app := client.WithApp(WeWorkFinanceCorpSecret, agentID)
	return &WeWorkMsgAuditApiClient{
		Client: app,
	}
}

func (we *WeWorkExternalContactClient) GetExternalContact(externalUserid string) (ret *workwx.ExternalContactInfo, err error) {
	//ret, err := we.Client.GetExternalContact("wmPhSiBwAA8bqgBzwlPcttfPZbZh7heQ")
	ret, err = we.Client.GetExternalContact(externalUserid)
	return
}

func (we *WeWorkExternalContactClient) BatchListExternalContact(userId string) {
	//userId := "YanLiNa"
	ret, err := we.Client.BatchListExternalContact(userId, "", 100)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(ret)
}

func (wa *WeWorkMsgAuditApiClient) GetMsgAuditGroupChat(roomId string) (ret *workwx.MsgAuditGroupChat, err error){
//	roomId := "wrPhSiBwAAqC4Ctn430KPc-TEu8UtmAw"
	ret, err = wa.Client.GetMsgAuditGroupChat(roomId)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(ret)
	return
}

func (we *WeWorkUserNameClient) GetUser(userId string) (ret *workwx.UserInfo, err error) {
	ret, err = we.Client.GetUser(userId)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(ret)
	return
}