package m9zApi

import (
	"sync"
	"time"

	"tgk-touch/internal/library/m9z"
)

const automaticModeUseDemoAPI = false

type automaticModeTaskReq struct {
	CommUid string `json:"comm_uid"`
	Mode    uint8  `json:"mode"`
}

type automaticModeDemoStore struct {
	mu          sync.RWMutex
	currentMode uint8
	startTasks  [3]m9z.ControlTask
	stopTasks   [3]m9z.ControlTask
	middleTasks [3]m9z.MiddleControlTask
}

var automaticModeDemo = newAutomaticModeDemoStore()

func newAutomaticModeDemoStore() *automaticModeDemoStore {
	store := &automaticModeDemoStore{}
	for mode := uint8(0); mode < 3; mode++ {
		store.startTasks[mode] = buildAutomaticModeDemoTask(6+int(mode), 30, true, uint8(60+mode*10))
		store.stopTasks[mode] = buildAutomaticModeDemoTask(22, int(mode)*10, false, 0)
		store.middleTasks[mode] = m9z.MiddleControlTask{
			ControlTaskList: []m9z.ControlTask{
				buildAutomaticModeDemoTask(12, int(mode)*10, true, uint8(45+mode*5)),
			},
		}
	}
	return store
}

func buildAutomaticModeDemoTask(hour int, minute int, turnOn bool, pwm uint8) m9z.ControlTask {
	loops := make([]m9z.TaskLoop, 10)
	for i := range loops {
		loops[i] = m9z.TaskLoop{
			Index:  uint(i),
			TurnOn: turnOn,
			Pwm:    pwm,
		}
	}
	return m9z.ControlTask{
		ExecTime: time.Date(2025, 6, 7, hour, minute, 0, 0, time.Local),
		TimeType: 0,
		Loops:    loops,
	}
}

func normalizeAutomaticMode(mode uint8) uint8 {
	if mode > 2 {
		return 0
	}
	return mode
}

func cloneAutomaticControlTask(task m9z.ControlTask) m9z.ControlTask {
	task.Loops = append([]m9z.TaskLoop(nil), task.Loops...)
	return task
}

func cloneAutomaticMiddleTask(task m9z.MiddleControlTask) m9z.MiddleControlTask {
	task.ControlTaskList = append([]m9z.ControlTask(nil), task.ControlTaskList...)
	for i := range task.ControlTaskList {
		task.ControlTaskList[i] = cloneAutomaticControlTask(task.ControlTaskList[i])
	}
	return task
}

func (s *automaticModeDemoStore) getMode() uint8 {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.currentMode
}

func (s *automaticModeDemoStore) setMode(mode uint8) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.currentMode = normalizeAutomaticMode(mode)
}

func (s *automaticModeDemoStore) getStartTask(mode uint8) m9z.ControlTask {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return cloneAutomaticControlTask(s.startTasks[normalizeAutomaticMode(mode)])
}

func (s *automaticModeDemoStore) getStartTaskProgram(mode uint8) (*m9z.TaskProgram, error) {
	normalizedMode := normalizeAutomaticMode(mode)
	return m9z.ControlTaskToTaskProgram(s.getStartTask(normalizedMode), normalizedMode)
}

func (s *automaticModeDemoStore) setStartTask(mode uint8, task m9z.ControlTask) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.startTasks[normalizeAutomaticMode(mode)] = cloneAutomaticControlTask(task)
}

func (s *automaticModeDemoStore) getStopTask(mode uint8) m9z.ControlTask {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return cloneAutomaticControlTask(s.stopTasks[normalizeAutomaticMode(mode)])
}

func (s *automaticModeDemoStore) getStopTaskProgram(mode uint8) (*m9z.TaskProgram, error) {
	normalizedMode := normalizeAutomaticMode(mode)
	return m9z.ControlTaskToTaskProgram(s.getStopTask(normalizedMode), normalizedMode)
}

func (s *automaticModeDemoStore) setStopTask(mode uint8, task m9z.ControlTask) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.stopTasks[normalizeAutomaticMode(mode)] = cloneAutomaticControlTask(task)
}

func (s *automaticModeDemoStore) getMiddleTask(mode uint8) m9z.MiddleControlTask {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return cloneAutomaticMiddleTask(s.middleTasks[normalizeAutomaticMode(mode)])
}

func (s *automaticModeDemoStore) getMiddleTaskProgram(mode uint8) (*m9z.TaskProgram, error) {
	normalizedMode := normalizeAutomaticMode(mode)
	return m9z.MiddleControlTaskToTaskProgram(s.getMiddleTask(normalizedMode), normalizedMode)
}

func (s *automaticModeDemoStore) setMiddleTask(mode uint8, task m9z.MiddleControlTask) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.middleTasks[normalizeAutomaticMode(mode)] = cloneAutomaticMiddleTask(task)
}
