from csv2_common import check_keys, requests, show_active_user_groups, show_table, verify_yaml_file
from subprocess import Popen, PIPE

import filecmp
import os

import csv2_defaults

KEY_MAP = {
    '-upw':  'password',
    }

def settings(gvar):
    """
    Modify the specified user.
    """

    mandatory = []
    required = []
    optional = ['-H', '-h', '-s', '-upw', '-xA']

    if gvar['retrieve_options']:
        return mandatory + required + optional

    # Check for missing arguments or help required.
    form_data = check_keys(
        gvar,
        mandatory,
        required,
        optional,
        key_map=KEY_MAP)

    if len(form_data) < 1:
        print('Error: "%s my settings" requires at least one option to update.' % gvar['command_name'])
        exit(1)

    # Create the user.
    response = requests(
        gvar,
        '/user/settings/',
        form_data
        )
    
    if response['message']:
        print(response['message'])

    if 'server' not in gvar['command_args']:
        gvar['command_args']['server'] = gvar['pid_defaults']['server']

    gvar['user_settings']['server-password'] = gvar['user_settings']['user-password']
    del gvar['user_settings']['user-password']
    csv2_defaults.set(gvar)


@@ -199,24 +199,129 @@
   '-
-upw':  'password
+gn':   'default_group',%0A    '-upw':  'password',%0A    '-sgs':  'flag_global_status',%0A    '-sri':  'status_refresh_interval
 ',%0A 
@@ -447,16 +447,23 @@
 onal = %5B
+'-gn', 
 '-H', '-
@@ -471,16 +471,32 @@
 ', '-s',
+ '-sgs', '-sri',
  '-upw',
@@ -760,17 +760,17 @@
 Y_MAP)%0A%0A
- 
+#
    if le
@@ -787,17 +787,17 @@
 a) %3C 1:%0A
- 
+#
        p
@@ -891,17 +891,17 @@
 name'%5D)%0A
- 
+#
        e
@@ -1094,16 +1094,104 @@
 age'%5D)%0A%0A
+    if response%5B'response_code'%5D == 0 and 'user-password' in gvar%5B'user_settings'%5D:%0A    
     if '
@@ -1231,24 +1231,28 @@
 '%5D:%0A        
+   
+ 
 gvar%5B'comman
@@ -1300,24 +1300,28 @@
 %5B'server'%5D%0A%0A
+    
     gvar%5B'us
@@ -1398,16 +1398,20 @@
 d'%5D%0A    
+   
+ 
 del gvar
@@ -1441,24 +1441,28 @@
 -password'%5D%0A
+    
     csv2_def
@@ -1478,8 +1478,429 @@
 (gvar)%0A%0A
+    # Print report.%0A    show_active_user_groups(gvar, response)%0A%0A    show_table(%0A        gvar,%0A        response%5B'user_list'%5D,%0A        %5B%0A            'username/Group,k',%0A            'cert_cn/Cert Common Name',%0A            'default_group/Default Group',%0A            'flag_global_status/Global Switch/Status',%0A            'status_refresh_interval/Refresh Interval/Status',%0A            %5D,%0A        title=%22Settings%22,%0A        )%0A%0A

fix my settings