// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

package power

import (
	"io/ioutil"
	"path/filepath"

	"github.com/linuxdeepin/go-lib/arch"
)

func (m *Manager) initLidSwitch() {
	if arch.Get() == arch.Sunway && isSWLidStateFileExist() {
		m.initLidSwitchSW()
	} else {
		err := m.initLidSwitchByUPower()
		if err != nil {
			logger.Warningf("failed to init watch lid switch by upower(%v),start init watch by gudev", err)
			m.initLidSwitchCommon()
		}
	}
	logger.Info("hasLidSwitch:", m.HasLidSwitch)
}

func (m *Manager) handleLidSwitchEvent(closed bool) {
	if closed {
		logger.Info("Lid Closed")
		err := m.service.Emit(m, "LidClosed")
		if err != nil {
			logger.Warning(err)
		}
		if m.disconnectMonitorWhenLidClosedEnabled && m.buildinMonitorDrmPath != "" {
			logger.Info("auto disconnect monitor")
			path := filepath.Join(m.buildinMonitorDrmPath, "status")
			err := ioutil.WriteFile(path, []byte("off"), 0644)
			if err != nil {
				logger.Warning(err)
			}
		}
	} else {
		logger.Info("Lid Opened")
		err := m.service.Emit(m, "LidOpened")
		if err != nil {
			logger.Warning(err)
		}
		if m.buildinMonitorDrmPath != "" {
			logger.Info("auto connect monitor")
			path := filepath.Join(m.buildinMonitorDrmPath, "status")
			err := ioutil.WriteFile(path, []byte("on"), 0644)
			if err != nil {
				logger.Warning(err)
			}
			if !m.disconnectMonitorWhenLidClosedEnabled {
				m.lidSwitchPermit()
			}
		}
	}
}
