#!/opt/cloudlinux/venv/bin/python3 -bb
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import argparse
import sys

import cldetectlib as detect
from dynamicui_base import DynamicUIBase

sys.path.append('/usr/share/l.v.e-manager/cpanel/utils')


class DynamicUI(DynamicUIBase):

    def __init__(self):
        super(DynamicUI, self).__init__(None)

    def main(self):
        """
        For cpanel use old script
        :return:
        """
        # we MUST run argument parser BEFORE doing any stuff
        # because for cPanel we call our old script called dynamicuictl.py
        # which has it's own argument parser and those arguments may be different
        parser = argparse.ArgumentParser(description='Update dynamic UI')
        # TODO: looks like sync-conf must be an action and not an argument
        # we should replace it with parser.add_subparsers(...)
        parser.add_argument('--sync-conf',
                            action='store',
                            choices=['all', 'php', 'nodejs', 'python', 'xray'],
                            help='Sync with dynamicui configs')
        parser.add_argument('--show-for-reseller',
                            metavar='RESELLER NAME',
                            action='store',
                            help='Show plugin for reseller')
        parser.add_argument('--hide-for-reseller',
                            metavar='RESELLER NAME',
                            action='store',
                            help='Hide plugin for reseller')
        parser.add_argument('--fix-display-plugin',
                            action='store_true',
                            help='Fix visibility plugin for resellers')
        parser.add_argument('--silent',
                            action='store_true',
                            help='Silent mode')

        self.args = parser.parse_args()

        self.silent = self.args.silent

        if self.args.sync_conf:
            self.sync_conf(self.args.sync_conf)
        elif self.args.show_for_reseller:
            self.show_for_reseller(self.args.show_for_reseller)
        elif self.args.hide_for_reseller:
            self.hide_for_reseller(self.args.hide_for_reseller)
        elif self.args.fix_display_plugin:
            self.fix_display_plugin()
        else:
            parser.print_help()

    def sync_conf(self, interpreter):
        """
        Detect panel and run sync script
        """
        detect.getCP()
        if detect.is_plesk():
            from dynamicui_plesk import DynamicUIPlesk
            DynamicUIPlesk(self.args).sync(interpreter)
        elif detect.is_da():
            from dynamicui_da import DynamicUIDA
            DynamicUIDA(self.args).sync(interpreter)
        elif detect.is_cpanel():
            from dynamicuictl import main as cpanelDynamic
            # dynamicuiclt has different --sync-conf values
            # so let's convert them to needed format
            aliases = {
                'all': 'all',
                'php': 'lve_hide_selector',
                'nodejs': 'lve_hide_selector'
            }
            if interpreter in aliases:
                cpanelDynamic(['--sync-conf', aliases[interpreter]])
            else:
                self.print_message("%s is not supported argument for cPanel" % interpreter)
        else:
            self.not_supported()

    def show_for_reseller(self, reseller_name):
        if detect.is_cpanel():
            from dynamicui_cpanel import DynamicUICpanel
            DynamicUICpanel(self.args).show_for_reseller(reseller_name)
        # In case when custom panel but not one of the list cPanel, Plesk, DA.
        elif not detect.is_unknown():
            pass
        else:
            self.not_supported()

    def hide_for_reseller(self, reseller_name):
        if detect.is_cpanel():
            from dynamicui_cpanel import DynamicUICpanel
            DynamicUICpanel(self.args).hide_for_reseller(reseller_name)
        else:
            self.not_supported()

    def fix_display_plugin(self):
        if detect.is_cpanel():
            from dynamicui_cpanel import DynamicUICpanel
            DynamicUICpanel(self.args).fix_display_plugin()
        else:
            self.not_supported()

    def not_supported(self):
        self.print_message("Not supported control panel")


if __name__ == '__main__':
    DynamicUI().main()
