#!python
# myth-channel-updater
# Copyright (C) 2022 Andrew Wilkinson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys

from mythchannels.arguments import get_arguments
from mythchannels.backend import MythTVBackend
from mythchannels.rules import Rules

def main(raw_args):
    args = get_arguments(raw_args[1:])

    backend = MythTVBackend("http://192.168.1.12:6544")
    channels = backend.get_channel_info_list(1) \
               + backend.get_channel_info_list(2)

    rules = Rules("channels.yml")

    for channel in channels:
        channel["visible"] = True
        rules.match(channel)


    for channel in channels:
        if len(channel.set_values) == 0:
            continue

        if args.commit:
            setters = ", ".join(sorted(f"{k}={v}" for (k, v) in channel.set_values.items()))
            print(f"{channel}. Setting {setters}")

            backend.update_channel(channel)
        else:
            setters = ", ".join(sorted(f"{k}={v}" for (k, v) in channel.set_values.items()))
            print(f"{channel}. Would set {setters}")

if __name__ == "__main__":
    main(sys.argv)
