#!/usr/bin/python3

import salt.utils.dictupdate
import salt.utils.dictdiffer


def config(name, verify, url, cfg):
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': 'Config is up to date'}

    cfg = dict(cfg)

    apikey = __salt__['syncthing.get_apikey']()
    st_cfg = __salt__['syncthing.get_config'](url, verify, apikey)

    cfg['gui']['apiKey'] = apikey

    res_cfg = salt.utils.dictupdate.update(st_cfg, cfg, recursive_update=True, merge_lists=False)

    ## Return to managed to set result
    ret['result'] = __salt__['syncthing.set_config'](url, verify, apikey, res_cfg)
    ret['changes'] = salt.utils.dictdiffer.deep_diff(st_cfg, res_cfg)

    return ret