#!/usr/bin/env python3
# Copyright (c) 2011, 2012, 2013, 2014 Marek Sapota
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE

from Shade.shade.Sleep import Sleep
from Shade.shade.Power import Power
from Shade.shade.Display import Display
from Shade.ArgParse import ArgParse

parser = ArgParse(
    description = 'Set of usefull command line utilities.'
)

subparsers = parser.add_subparsers()

parser_sleep = subparsers.add_parser(
    'sleep',
    help = 'put computer to sleep',
)
parser_sleep.set_defaults(cmd = 'sleep')

parser_sleep.add_argument(
    'state',
    choices = ['suspend', 'hibernate', 'lock', 'sleep_on_lid_close'],
)

parser_pm = subparsers.add_parser(
    'power_management',
    aliases = ['pm'],
    help = 'switch between power management modes'
)
parser_pm.set_defaults(cmd = 'power_management')

parser_pm.add_argument(
    'state',
    choices = ['powersave', 'performance', 'auto'],
)

d = Display()
parser_display = subparsers.add_parser(
    'display',
    help = 'switch between display modes'
)
parser_display.set_defaults(cmd = 'display')

parser_display.add_argument(
    'mode',
    choices = d.list_modes(),
)

args = parser.parse_args()

if not hasattr(args, 'cmd'):
    parser.print_help()
elif args.cmd == 'power_management':
    p = Power()
    if args.state == 'powersave':
        p.powersave()
    elif args.state == 'performance':
        p.performance()
    elif args.state == 'auto':
        p.auto()
elif args.cmd == 'sleep':
    s = Sleep()
    if args.state == 'suspend':
        s.suspend()
    elif args.state == 'hibernate':
        s.hibernate()
    elif args.state == 'lock':
        s.lock()
    elif args.state == 'sleep_on_lid_close':
        s.sleep_on_lid_close()
elif args.cmd == 'display':
    d.switch(args.mode)
