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

package inputdevices

import (
	"github.com/linuxdeepin/dde-api/dxinput"
	"github.com/linuxdeepin/go-lib/gsettings"
	. "github.com/linuxdeepin/go-lib/gettext"
	notification "github.com/linuxdeepin/go-dbus-factory/org.freedesktop.notifications"
)

func (m *Manager) handleGSettings() {
	gsettings.ConnectChanged(gsSchemaInputDevices, gsKeyWheelSpeed, func(key string) {
		m.setWheelSpeed()
	})
}

func (kbd *Keyboard) handleGSettings() {
	gsettings.ConnectChanged(kbdSchema, "*", func(key string) {
		switch key {
		case kbdKeyRepeatEnable, kbdKeyRepeatDelay,
			kbdKeyRepeatInterval:
			kbd.applyRepeat()
		case kbdKeyCursorBlink:
			kbd.applyCursorBlink()
		case kbdKeyLayoutOptions:
			kbd.applyOptions()
		}
	})
}

func (m *Mouse) handleGSettings() {
	gsettings.ConnectChanged(mouseSchema, "*", func(key string) {
		switch key {
		case mouseKeyLeftHanded:
			m.enableLeftHanded()
		case mouseKeyDisableTouchpad:
			m.disableTouchPad()
		case mouseKeyNaturalScroll:
			m.enableNaturalScroll()
		case mouseKeyMiddleButton:
			m.enableMidBtnEmu()
		case mouseKeyAcceleration:
			m.motionAcceleration()
		case mouseKeyThreshold:
			m.motionThreshold()
		case mouseKeyScaling:
			m.motionScaling()
		case mouseKeyDoubleClick:
			m.doubleClick()
		case mouseKeyDragThreshold:
			m.dragThreshold()
		case mouseKeyAdaptiveAccel:
			m.enableAdaptiveAccelProfile()
		}
	})

	gsettings.ConnectChanged(tpadSchema, tpadKeyEnabled, func(key string) {
		tPadEnable := m.touchPad.TPadEnable.Get()
		logger.Debug("setting changed", key, tPadEnable)
		if tPadEnable && m.Exist && m.DisableTpad.Get() {
			m.DisableTpad.Set(false)
			//弹通知 关闭“插入鼠标时禁用触控板”
			m.notify("", Tr("The function of 'Disable touchpad when mouse is inserted' has been deactivated"))
		}
		m.touchPad.enable(tPadEnable)

		//设置system touchpad enable
		if m.sysTouchPad != nil {
			sysTPadEnable, err := m.sysTouchPad.Enable().Get(0)
			if err != nil {
				logger.Warning(err)
			}
			logger.Debugf("sysTPadEnable : %v, session tPadEnable : %v", sysTPadEnable, tPadEnable)
			if sysTPadEnable == tPadEnable {
				return
			}
			m.sysTouchPad.SetTouchpadEnable(0, tPadEnable)
		}
	})
}

func (m *Mouse)notify(summary, body string) {
	logger.Debugf("notify summary: %q, body: %q", summary, body)
	notification := notification.NewNotifications(m.service.Conn())
	_, err := notification.Notify(0, "dde-control-center", 101, "preferences-system", summary, body, nil, nil, 5000)
	if err != nil {
		logger.Warning(err)
	}
}

func (tp *TrackPoint) handleGSettings() {
	gsettings.ConnectChanged(trackPointSchema, "*", func(key string) {
		switch key {
		case trackPointKeyMidButton:
			tp.enableMiddleButton()
		case trackPointKeyMidButtonTimeout:
			tp.middleButtonTimeout()
		case trackPointKeyWheel:
			tp.enableWheelEmulation()
		case trackPointKeyWheelButton:
			tp.wheelEmulationButton()
		case trackPointKeyWheelTimeout:
			tp.wheelEmulationTimeout()
		case trackPointKeyWheelHorizScroll:
			tp.enableWheelHorizScroll()
		case trackPointKeyLeftHanded:
			tp.enableLeftHanded()
		case trackPointKeyAcceleration:
			tp.motionAcceleration()
		case trackPointKeyThreshold:
			tp.motionThreshold()
		case trackPointKeyScaling:
			tp.motionScaling()
		}
	})
}

func (tpad *Touchpad) handleGSettings() {
	gsettings.ConnectChanged(tpadSchema, "*", func(key string) {
		switch key {
		case tpadKeyLeftHanded:
			tpad.enableLeftHanded()
			tpad.enableTapToClick()
		case tpadKeyTapClick:
			tpad.enableTapToClick()
		case tpadKeyNaturalScroll:
			tpad.enableNaturalScroll()
		case tpadKeyScrollDelta:
			tpad.setScrollDistance()
		case tpadKeyEdgeScroll:
			tpad.enableEdgeScroll()
		case tpadKeyVertScroll, tpadKeyHorizScroll:
			tpad.enableTwoFingerScroll()
		case tpadKeyDisableWhileTyping:
			tpad.disableWhileTyping()
		case tpadKeyAcceleration:
			tpad.motionAcceleration()
		case tpadKeyThreshold:
			tpad.motionThreshold()
		case tpadKeyScaling:
			tpad.motionScaling()
		case tpadKeyPalmDetect:
			tpad.enablePalmDetect()
		case tpadKeyPalmMinWidth, tpadKeyPalmMinZ:
			tpad.setPalmDimensions()
		}
	})
}

func (w *Wacom) handleGSettings() {
	gsettings.ConnectChanged(wacomSchema, "*", func(key string) {
		logger.Debugf("wacom gsettings changed %v", key)
		switch key {
		case wacomKeyLeftHanded:
			w.enableLeftHanded()
		case wacomKeyCursorMode:
			w.enableCursorMode()
		case wacomKeySuppress:
			w.setSuppress()
		case wacomKeyForceProportions:
			w.setArea()
		}
	})

	gsettings.ConnectChanged(wacomStylusSchema, "*", func(key string) {
		logger.Debugf("wacom.stylus gsettings changed %v", key)
		switch key {
		case wacomKeyPressureSensitive:
			w.setPressureSensitiveForType(dxinput.WacomTypeStylus)
		case wacomKeyUpAction:
			w.setStylusButtonAction(btnNumUpKey, w.KeyUpAction.Get())
		case wacomKeyDownAction:
			w.setStylusButtonAction(btnNumDownKey, w.KeyDownAction.Get())
		case wacomKeyThreshold:
			w.setThresholdForType(dxinput.WacomTypeStylus)
		case wacomKeyRawSample:
			w.setRawSampleForType(dxinput.WacomTypeStylus)
		}
	})

	gsettings.ConnectChanged(wacomEraserSchema, "*", func(key string) {
		logger.Debugf("wacom.eraser gsettings changed %v", key)
		switch key {
		case wacomKeyPressureSensitive:
			w.setPressureSensitiveForType(dxinput.WacomTypeEraser)
		case wacomKeyThreshold:
			w.setThresholdForType(dxinput.WacomTypeEraser)
		case wacomKeyRawSample:
			w.setRawSampleForType(dxinput.WacomTypeEraser)
		}
	})
}
