package cmdline

import (
	"fmt"
	"os"
	"time"

	"github.com/spf13/cobra"
	"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/http"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/metacache"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/mount"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/repl"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/services"
	"gitlink.org.cn/cloudream/jcs-pub/client/internal/ticktock"
	"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/storage/pool"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/sysevent"
)

// 初始化函数，将ServeHTTP命令注册到命令列表中。
func init() {
	var configPath string
	var opt serveHTTPOptions
	cmd := cobra.Command{
		Use:   "serve",
		Short: "start serving storage service",
		Run: func(cmd *cobra.Command, args []string) {
			serveHTTP(configPath, opt)
		},
	}
	cmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
	cmd.Flags().BoolVarP(&opt.DisableHTTP, "no-http", "", false, "disable http server")
	cmd.Flags().StringVarP(&opt.HTTPListenAddr, "listen", "", "", "http listen address, will override config file")
	cmd.Flags().BoolVarP(&opt.DisableMount, "no-mount", "", false, "disable mount")
	cmd.Flags().StringVarP(&opt.MountPoint, "mount", "", "", "mount point, will override config file")
	RootCmd.AddCommand(&cmd)
}

type serveHTTPOptions struct {
	DisableHTTP    bool
	HTTPListenAddr string
	DisableMount   bool
	MountPoint     string
}

func serveHTTP(configPath string, opts serveHTTPOptions) {
	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.CollectInPlace()

	// 元数据缓存
	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)

	// 定时任务
	tktk := ticktock.New(config.Cfg().TickTock, db, stgMeta, stgPool, evtPub, publock)
	tktk.Start()
	defer tktk.Stop()

	// 交互式命令行
	rep := repl.New(db, tktk)
	replCh := rep.Start()

	// 挂载
	mntCfg := config.Cfg().Mount
	if !opts.DisableMount && mntCfg != nil && mntCfg.Enabled {
		if opts.MountPoint != "" {
			mntCfg.MountPoint = opts.MountPoint
		}
	} else {
		mntCfg = nil
	}
	mnt := mount.NewMount(mntCfg, db, uploader, dlder)
	mntChan := mnt.Start()
	defer mnt.Stop()

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

	// HTTP接口
	httpCfg := config.Cfg().HTTP
	if !opts.DisableHTTP && httpCfg != nil && httpCfg.Enabled {
		if opts.HTTPListenAddr != "" {
			httpCfg.Listen = opts.HTTPListenAddr
		}
	} else {
		httpCfg = nil
	}
	httpSvr := http.NewServer(httpCfg, svc)
	httpChan := httpSvr.Start()
	defer httpSvr.Stop()

	/// 开始监听各个模块的事件

	evtPubEvt := evtPubChan.Receive()
	acStatEvt := acStatChan.Receive()
	replEvt := replCh.Receive()
	httpEvt := httpChan.Receive()
	mntEvt := mntChan.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()

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

			switch e.Value.(type) {
			case repl.ExitEvent:
				logger.Info("exit by repl")
				break loop
			}
			replEvt = replCh.Receive()

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

			switch e := e.Value.(type) {
			case http.ExitEvent:
				logger.Infof("http server exited, err: %v", e.Err)
				break loop
			}
			httpEvt = httpChan.Receive()

		case e := <-mntEvt.Chan():
			if e.Err != nil {
				logger.Errorf("receive mount event: %v", e.Err)
				break loop
			}

			switch e := e.Value.(type) {
			case mount.ExitEvent:
				logger.Infof("mount exited, err: %v", e.Err)
				break loop
			}
			mntEvt = mntChan.Receive()
		}
	}
}
