package cmdline

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/spf13/cobra"
	"gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
	"gitlink.org.cn/cloudream/common/pkgs/logger"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/accessstat"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/config"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/db"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/downloader"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/downloader/strategy"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/metacache"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/services"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/uploader"
	stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
	"gitlink.org.cn/cloudream/jcs-pub/common/models/datamap"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/connectivity"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/distlock"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch2"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch2/parser"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/pool"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/sysevent"
)

func init() {
	var configPath string
	cmd := cobra.Command{
		Use:   "test",
		Short: "test",
		Run: func(cmd *cobra.Command, args []string) {
			test(configPath)
		},
	}
	cmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
	RootCmd.AddCommand(&cmd)
}

func doTest(svc *services.Service) {
	ft := ioswitch2.NewFromTo()

	space1 := svc.UserSpaceMeta.Get(3)
	space2 := svc.UserSpaceMeta.Get(4)

	// ft.AddFrom(ioswitch2.NewFromPublicStore(*space1.MasterHub, *space1, "space3/blocks/1A/Full1AE5436AF72D8EF93923486E0E167315CEF0C91898064DADFAC22216FFBC5E3D"))
	// ft.AddTo(ioswitch2.NewToPublicStore(*space2.MasterHub, *space2, "block"))
	// plans := exec.NewPlanBuilder()
	// parser.Parse(ft, plans)
	// fmt.Println(plans)

	// _, err := plans.Execute(exec.NewExecContext()).Wait(context.Background())
	// fmt.Println(err)

	ft = ioswitch2.NewFromTo()
	ft.AddFrom(ioswitch2.NewFromShardstore("Full1AE5436AF72D8EF93923486E0E167315CEF0C91898064DADFAC22216FFBC5E3D", *space1.MasterHub, *space1, ioswitch2.RawStream()))
	ft.AddTo(ioswitch2.NewToPublicStore(*space2.MasterHub, *space2, "test3.txt"))
	plans := exec.NewPlanBuilder()
	parser.Parse(ft, plans)
	fmt.Println(plans)
	_, err := plans.Execute(exec.NewExecContext()).Wait(context.Background())
	fmt.Println(err)

}

func test(configPath string) {
	err := config.Init(configPath)
	if err != nil {
		fmt.Printf("init config failed, err: %s", err.Error())
		os.Exit(1)
	}

	err = logger.Init(&config.Cfg().Logger)
	if err != nil {
		fmt.Printf("init logger failed, err: %s", err.Error())
		os.Exit(1)
	}

	stgglb.InitLocal(config.Cfg().Local)
	stgglb.InitMQPool(config.Cfg().RabbitMQ)
	stgglb.InitHubRPCPool(config.Cfg().HubRPC)

	// 数据库
	db, err := db.NewDB(&config.Cfg().DB)
	if err != nil {
		logger.Fatalf("new db failed, err: %s", err.Error())
	}

	// 初始化系统事件发布器
	evtPub, err := sysevent.NewPublisher(sysevent.ConfigFromMQConfig(config.Cfg().RabbitMQ), &datamap.SourceClient{
		UserID: config.Cfg().Local.UserID,
	})
	if err != nil {
		logger.Errorf("new sysevent publisher: %v", err)
		os.Exit(1)
	}
	evtPubChan := evtPub.Start()
	defer evtPub.Stop()

	// 连接性信息收集
	conCol := connectivity.NewCollector(&config.Cfg().Connectivity, nil)
	conCol.CollecNow()

	// 元数据缓存
	metaCacheHost := metacache.NewHost(db)
	go metaCacheHost.Serve()
	stgMeta := metaCacheHost.AddStorageMeta()
	hubMeta := metaCacheHost.AddHubMeta()
	conMeta := metaCacheHost.AddConnectivity()

	// 公共锁
	publock := distlock.NewService()

	// 访问统计
	acStat := accessstat.NewAccessStat(accessstat.Config{
		// TODO 考虑放到配置里
		ReportInterval: time.Second * 10,
	}, db)
	acStatChan := acStat.Start()
	defer acStat.Stop()

	// 存储管理器
	stgPool := pool.NewPool()

	// 下载策略
	strgSel := strategy.NewSelector(config.Cfg().DownloadStrategy, stgMeta, hubMeta, conMeta)

	// 下载器
	dlder := downloader.NewDownloader(config.Cfg().Downloader, &conCol, stgPool, strgSel, db)

	// 上传器
	uploader := uploader.NewUploader(publock, &conCol, stgPool, stgMeta, db)

	svc := services.NewService(publock, dlder, acStat, uploader, strgSel, stgMeta, db, evtPub, nil)

	go func() {
		doTest(svc)
		os.Exit(0)
	}()
	/// 开始监听各个模块的事件

	evtPubEvt := evtPubChan.Receive()
	acStatEvt := acStatChan.Receive()

loop:
	for {
		select {
		case e := <-evtPubEvt.Chan():
			if e.Err != nil {
				logger.Errorf("receive publisher event: %v", err)
				break loop
			}

			switch val := e.Value.(type) {
			case sysevent.PublishError:
				logger.Errorf("publishing event: %v", val)

			case sysevent.PublisherExited:
				if val.Err != nil {
					logger.Errorf("publisher exited with error: %v", val.Err)
				} else {
					logger.Info("publisher exited")
				}
				break loop

			case sysevent.OtherError:
				logger.Errorf("sysevent: %v", val)
			}
			evtPubEvt = evtPubChan.Receive()

		case e := <-acStatEvt.Chan():
			if e.Err != nil {
				logger.Errorf("receive access stat event: %v", err)
				break loop
			}
			switch e := e.Value.(type) {
			case accessstat.ExitEvent:
				logger.Infof("access stat exited, err: %v", e.Err)
				break loop
			}
			acStatEvt = acStatChan.Receive()
		}
	}
}
