1/**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import LooseObject from '../../data/LooseObject'
16import MmsBoolean from '../../data/MmsBoolean'
17import HiLog from '../../utils/HiLog';
18import router from '@system.router';
19// JS Common constants
20import common from '../../data/commonData';
21import settingService from '../../service/SettingService'
22
23const TAG = 'SettingsController';
24
25export default class SettingsController {
26    private static sInstance: SettingsController;
27    // Notification information integration
28    integrationSwitch: MmsBoolean = new MmsBoolean(true);
29    // Malicious website selection
30    maliciousWebSwitch: boolean = false;
31    // Verification code security protection
32    verificationCodeSwitch: boolean = false;
33    // Display contact avatar
34    showContactSwitch: boolean = true
35
36    static getInstance() {
37        if (SettingsController.sInstance == null) {
38            SettingsController.sInstance = new SettingsController();
39        }
40        return SettingsController.sInstance;
41    }
42
43    constructor() {
44        HiLog.i(TAG, 'constructor, start');
45        this.getSettingPageSwitchValue();
46    }
47
48    onInit() {
49        HiLog.i(TAG, 'onInit, start');
50    };
51
52    onShow() {
53        HiLog.i(TAG, 'onShow, start');
54    };
55
56    // Indicates whether to initialize the setting page.
57    getSettingPageSwitchValue() {
58        HiLog.i(TAG, 'getSettingPageSwitchValue, start');
59        let that = this;
60        settingService.setOnSettingValueListener(function (result) {
61            that.integrationSwitch.value = result.integrationSwitch;
62            that.maliciousWebSwitch = result.maliciousWebSwitch;
63            that.showContactSwitch = result.showContactSwitch;
64        });
65    };
66
67    // Notification information integration
68    integration(isOn: boolean) {
69        let messageCode = common.route.MESSAGE_CODE_UPDATE_ARCHIVE_INFO_MESSAGES_VALUE;
70        if (this.integrationSwitch.value != isOn) {
71            globalThis.needToUpdate = true;
72        }
73        let actionData: LooseObject = {};
74        this.integrationSwitch.value = isOn;
75        if (this.integrationSwitch.value) {
76            actionData.booleanValue = common.bool.TRUE;
77        } else {
78            actionData.booleanValue = common.bool.FALSE;
79        }
80        this.updateSettingPageSwitchValue(messageCode, actionData);
81    };
82
83    // Malicious website selection
84    maliciousWeb(e) {
85        HiLog.i(TAG, 'maliciousWeb, checked = ' + e.checked);
86        let messageCode = common.route.MESSAGE_CODE_UPDATE_MALICIOUS_WEBSITE_IDENTIFICATION_VALUE;
87        let actionData: LooseObject = {};
88        this.maliciousWebSwitch = e.checked;
89        if (this.maliciousWebSwitch) {
90            actionData.booleanValue = common.bool.TRUE;
91        } else {
92            actionData.booleanValue = common.bool.FALSE;
93        }
94        this.updateSettingPageSwitchValue(messageCode, actionData);
95    };
96
97    // Go to the Message Ring page. Choose Settings > Sound and Vibration > Message Ring.
98    jumpToMessageTonePage() {
99        HiLog.i(TAG, 'jumpToMessageTonePage')
100    };
101
102    // Display a contact's avatar
103    showContact(e) {
104        HiLog.i(TAG, 'showContact, checked = ' + e.checked);
105        let messageCode = common.route.MESSAGE_CODE_UPDATE_SHOW_CONTACT_PROFILE_PICS_VALUE;
106        let actionData: LooseObject = {};
107        this.showContactSwitch = e.checked;
108        if (this.showContactSwitch) {
109            actionData.booleanValue = common.bool.TRUE;
110        } else {
111            actionData.booleanValue = common.bool.FALSE;
112        }
113        this.updateSettingPageSwitchValue(messageCode, actionData);
114    };
115
116    // Restore the default values on the settings page.
117    restoreSettingsPageSwitchValue() {
118        let that = this;
119        settingService.restoreSwitchValue(function (result) {
120            if (result.code === common.int.SUCCESS) {
121                that.integrationSwitch.value = true;
122                that.maliciousWebSwitch = false;
123                that.showContactSwitch = true;
124                globalThis.needToUpdate = true;
125                HiLog.i(TAG, 'restoreSettingsPageSwitchValue, success');
126            } else {
127                HiLog.w(TAG, 'restoreSettingsPageSwitchValue, failed');
128            }
129        });
130    };
131
132    // Back button
133    back() {
134        router.back();
135    };
136
137    // Advanced page redirection
138    advancedSetting() {
139        HiLog.i(TAG, 'advancedSetting')
140        router.push({
141            uri: 'pages/settings/advancedSettings/advancedSettings'
142        });
143    };
144
145    // Update Switch Value
146    updateSettingPageSwitchValue(messageCode, actionData) {
147        HiLog.i(TAG, 'updateSettingPageSwitchValue,  messageCode = ' + messageCode);
148        settingService.updateSettingValue(messageCode, actionData, function (result) {
149            if (result.code == common.int.SUCCESS) {
150                HiLog.i(TAG, 'updateSettingPageSwitchValue, success');
151            } else {
152                HiLog.w(TAG, 'updateSettingPageSwitchValue, failed');
153            }
154        });
155    };
156}