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 */
15
16import { BaseElement, element } from '../../../base-ui/BaseElement';
17import LitSwitch, { LitSwitchChangeEvent } from '../../../base-ui/switch/lit-switch';
18import '../../../base-ui/select/LitAllocationSelect';
19import '../../../base-ui/switch/lit-switch';
20import { SpRecordTrace } from '../SpRecordTrace';
21import { HdcDeviceManager } from '../../../hdc/HdcDeviceManager';
22import { LitAllocationSelect } from '../../../base-ui/select/LitAllocationSelect';
23import { SpHiSysEventHtml } from './SpHisysEvent.html';
24import { LitSelectV } from '../../../base-ui/select/LitSelectV';
25
26@element('sp-hisys-event')
27export class SpHisysEvent extends BaseElement {
28  private domainInputEL: LitSelectV | undefined | null;
29  private eventNameInputEL: LitSelectV | undefined | null;
30  private sysEventConfigList: NodeListOf<LitSelectV> | undefined | null;
31  private sysEventSwitch: LitSwitch | undefined | null;
32  private domainInputEl: HTMLInputElement | undefined | null;
33  private nameInputEl: HTMLInputElement | undefined | null;
34  private eventConfig: unknown = {};
35
36  set startSamp(start: boolean) {
37    if (start) {
38      this.setAttribute('startSamp', '');
39      this.domainInputEL!.removeAttribute('readonly');
40      this.eventNameInputEL!.removeAttribute('readonly');
41    } else {
42      this.removeAttribute('startSamp');
43      this.domainInputEL!.setAttribute('readonly', 'readonly');
44      this.eventNameInputEL!.setAttribute('readonly', 'readonly');
45    }
46  }
47
48  get domain(): string {
49    if (this.domainInputEL!.value.length > 0 && this.domainInputEL!.value !== 'ALL-Domain') {
50      return this.domainInputEL!.value;
51    }
52    return '';
53  }
54
55  get eventName(): string {
56    if (this.eventNameInputEL!.value.length > 0 && this.eventNameInputEL!.value !== 'ALL-Event') {
57      return this.eventNameInputEL!.value;
58    }
59    return '';
60  }
61
62  get startSamp(): boolean {
63    return this.hasAttribute('startSamp');
64  }
65
66  get sysEventConfigPath(): string {
67    return '/system/etc/hiview/hisysevent.def';
68  }
69
70  initElements(): void {
71    this.domainInputEL = this.shadowRoot?.querySelector<LitSelectV>('.record-domain-input');
72    this.eventNameInputEL = this.shadowRoot?.querySelector<LitSelectV>('.record-event-input');
73    this.sysEventConfigList = this.shadowRoot?.querySelectorAll<LitSelectV>('.record-input');
74    this.sysEventSwitch = this.shadowRoot?.querySelector('lit-switch') as LitSwitch;
75    this.sysEventSwitch?.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
76      let detail = event.detail;
77      this.startSamp = detail!.checked;
78      this.updateDisable(detail!.checked);
79    });
80    this.updateDisable(false);
81    this.domainInputEl = this.domainInputEL?.shadowRoot?.querySelector('input') as HTMLInputElement;
82    this.nameInputEl = this.eventNameInputEL?.shadowRoot?.querySelector('input') as HTMLInputElement;
83  }
84
85  connectedCallback(): void {
86    super.connectedCallback();
87    this.domainInputEl?.addEventListener('mousedown', this.domainInputEvent);
88    this.nameInputEl?.addEventListener('mousedown', this.nameInputEvent);
89  }
90
91  disconnectedCallback(): void {
92    super.disconnectedCallback();
93    this.domainInputEl?.removeEventListener('mousedown', this.domainInputEvent);
94    this.nameInputEl?.removeEventListener('mousedown', this.nameInputEvent);
95  }
96
97  domainInputEvent = (): void => {
98    if (this.startSamp) {
99      if (SpRecordTrace.serialNumber === '') {
100        this.domainInputEL!.dataSource([], '');
101      } else {
102        HdcDeviceManager.fileRecv(this.sysEventConfigPath, () => { }).then((pullRes) => {
103          pullRes.arrayBuffer().then((buffer) => {
104            if (buffer.byteLength > 0) {
105              let dec = new TextDecoder();
106              this.eventConfig = JSON.parse(dec.decode(buffer));
107              let domainList = Object.keys(this.eventConfig!);
108              if (domainList.length > 0) {
109                this.domainInputEL!.dataSource(domainList, 'ALL-Domain', true);
110              } else {
111                this.domainInputEL!.dataSource([], '');
112              }
113            }
114          });
115        });
116      }
117      this.domainInputEl!.removeAttribute('readonly');
118    } else {
119      this.domainInputEl!.setAttribute('readonly', 'readonly');
120      return;
121    }
122  };
123
124  nameInputEvent = (): void => {
125    if (this.startSamp) {
126      if (SpRecordTrace.serialNumber === '') {
127        this.eventNameInputEL!.dataSource([], '');
128      } else {
129        let domain = this.domainInputEL?.value;
130        // @ts-ignore
131        let eventConfigElement = this.eventConfig[domain];
132        if (eventConfigElement) {
133          let eventNameList = Object.keys(eventConfigElement);
134          if (eventNameList?.length > 0) {
135            this.eventNameInputEL!.dataSource(eventNameList, 'ALL-Event', true);
136          } else {
137            this.eventNameInputEL!.dataSource([], '');
138          }
139        } else {
140          let currentData: string[] = [];
141          if (domain === '' || domain === 'ALL-Domain') {
142            //@ts-ignore
143            let domainKey = Object.keys(this.eventConfig);
144            domainKey.forEach((item) => {
145              //@ts-ignore
146              let currentEvent = this.eventConfig[item];
147              let eventList = Object.keys(currentEvent);
148              currentData.push(...eventList);
149            });
150            this.eventNameInputEL!.dataSource(currentData, 'ALL-Event', true);
151          } else {
152            this.eventNameInputEL!.dataSource([], '');
153          }
154        }
155      }
156      this.nameInputEl!.removeAttribute('readonly');
157    } else {
158      this.nameInputEl!.setAttribute('readonly', 'readonly');
159      return;
160    }
161  };
162
163  private updateDisable(isDisable: boolean): void {
164    this.sysEventConfigList!.forEach((configEL) => {
165      if (isDisable) {
166        configEL.removeAttribute('disabled');
167      } else {
168        configEL.setAttribute('disabled', '');
169      }
170    });
171  }
172
173  initHtml(): string {
174    return SpHiSysEventHtml;
175  }
176}
177