1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { BaseElement, element } from '../../../base-ui/BaseElement';
17fb726d48Sopenharmony_ciimport LitSwitch, { LitSwitchChangeEvent } from '../../../base-ui/switch/lit-switch';
18fb726d48Sopenharmony_ciimport '../../../base-ui/select/LitAllocationSelect';
19fb726d48Sopenharmony_ciimport '../../../base-ui/switch/lit-switch';
20fb726d48Sopenharmony_ciimport { SpRecordTrace } from '../SpRecordTrace';
21fb726d48Sopenharmony_ciimport { HdcDeviceManager } from '../../../hdc/HdcDeviceManager';
22fb726d48Sopenharmony_ciimport { LitAllocationSelect } from '../../../base-ui/select/LitAllocationSelect';
23fb726d48Sopenharmony_ciimport { SpHiSysEventHtml } from './SpHisysEvent.html';
24fb726d48Sopenharmony_ciimport { LitSelectV } from '../../../base-ui/select/LitSelectV';
25fb726d48Sopenharmony_ci
26fb726d48Sopenharmony_ci@element('sp-hisys-event')
27fb726d48Sopenharmony_ciexport class SpHisysEvent extends BaseElement {
28fb726d48Sopenharmony_ci  private domainInputEL: LitSelectV | undefined | null;
29fb726d48Sopenharmony_ci  private eventNameInputEL: LitSelectV | undefined | null;
30fb726d48Sopenharmony_ci  private sysEventConfigList: NodeListOf<LitSelectV> | undefined | null;
31fb726d48Sopenharmony_ci  private sysEventSwitch: LitSwitch | undefined | null;
32fb726d48Sopenharmony_ci  private domainInputEl: HTMLInputElement | undefined | null;
33fb726d48Sopenharmony_ci  private nameInputEl: HTMLInputElement | undefined | null;
34fb726d48Sopenharmony_ci  private eventConfig: unknown = {};
35fb726d48Sopenharmony_ci
36fb726d48Sopenharmony_ci  set startSamp(start: boolean) {
37fb726d48Sopenharmony_ci    if (start) {
38fb726d48Sopenharmony_ci      this.setAttribute('startSamp', '');
39fb726d48Sopenharmony_ci      this.domainInputEL!.removeAttribute('readonly');
40fb726d48Sopenharmony_ci      this.eventNameInputEL!.removeAttribute('readonly');
41fb726d48Sopenharmony_ci    } else {
42fb726d48Sopenharmony_ci      this.removeAttribute('startSamp');
43fb726d48Sopenharmony_ci      this.domainInputEL!.setAttribute('readonly', 'readonly');
44fb726d48Sopenharmony_ci      this.eventNameInputEL!.setAttribute('readonly', 'readonly');
45fb726d48Sopenharmony_ci    }
46fb726d48Sopenharmony_ci  }
47fb726d48Sopenharmony_ci
48fb726d48Sopenharmony_ci  get domain(): string {
49fb726d48Sopenharmony_ci    if (this.domainInputEL!.value.length > 0 && this.domainInputEL!.value !== 'ALL-Domain') {
50fb726d48Sopenharmony_ci      return this.domainInputEL!.value;
51fb726d48Sopenharmony_ci    }
52fb726d48Sopenharmony_ci    return '';
53fb726d48Sopenharmony_ci  }
54fb726d48Sopenharmony_ci
55fb726d48Sopenharmony_ci  get eventName(): string {
56fb726d48Sopenharmony_ci    if (this.eventNameInputEL!.value.length > 0 && this.eventNameInputEL!.value !== 'ALL-Event') {
57fb726d48Sopenharmony_ci      return this.eventNameInputEL!.value;
58fb726d48Sopenharmony_ci    }
59fb726d48Sopenharmony_ci    return '';
60fb726d48Sopenharmony_ci  }
61fb726d48Sopenharmony_ci
62fb726d48Sopenharmony_ci  get startSamp(): boolean {
63fb726d48Sopenharmony_ci    return this.hasAttribute('startSamp');
64fb726d48Sopenharmony_ci  }
65fb726d48Sopenharmony_ci
66fb726d48Sopenharmony_ci  get sysEventConfigPath(): string {
67fb726d48Sopenharmony_ci    return '/system/etc/hiview/hisysevent.def';
68fb726d48Sopenharmony_ci  }
69fb726d48Sopenharmony_ci
70fb726d48Sopenharmony_ci  initElements(): void {
71fb726d48Sopenharmony_ci    this.domainInputEL = this.shadowRoot?.querySelector<LitSelectV>('.record-domain-input');
72fb726d48Sopenharmony_ci    this.eventNameInputEL = this.shadowRoot?.querySelector<LitSelectV>('.record-event-input');
73fb726d48Sopenharmony_ci    this.sysEventConfigList = this.shadowRoot?.querySelectorAll<LitSelectV>('.record-input');
74fb726d48Sopenharmony_ci    this.sysEventSwitch = this.shadowRoot?.querySelector('lit-switch') as LitSwitch;
75fb726d48Sopenharmony_ci    this.sysEventSwitch?.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
76fb726d48Sopenharmony_ci      let detail = event.detail;
77fb726d48Sopenharmony_ci      this.startSamp = detail!.checked;
78fb726d48Sopenharmony_ci      this.updateDisable(detail!.checked);
79fb726d48Sopenharmony_ci    });
80fb726d48Sopenharmony_ci    this.updateDisable(false);
81fb726d48Sopenharmony_ci    this.domainInputEl = this.domainInputEL?.shadowRoot?.querySelector('input') as HTMLInputElement;
82fb726d48Sopenharmony_ci    this.nameInputEl = this.eventNameInputEL?.shadowRoot?.querySelector('input') as HTMLInputElement;
83fb726d48Sopenharmony_ci  }
84fb726d48Sopenharmony_ci
85fb726d48Sopenharmony_ci  connectedCallback(): void {
86fb726d48Sopenharmony_ci    super.connectedCallback();
87fb726d48Sopenharmony_ci    this.domainInputEl?.addEventListener('mousedown', this.domainInputEvent);
88fb726d48Sopenharmony_ci    this.nameInputEl?.addEventListener('mousedown', this.nameInputEvent);
89fb726d48Sopenharmony_ci  }
90fb726d48Sopenharmony_ci
91fb726d48Sopenharmony_ci  disconnectedCallback(): void {
92fb726d48Sopenharmony_ci    super.disconnectedCallback();
93fb726d48Sopenharmony_ci    this.domainInputEl?.removeEventListener('mousedown', this.domainInputEvent);
94fb726d48Sopenharmony_ci    this.nameInputEl?.removeEventListener('mousedown', this.nameInputEvent);
95fb726d48Sopenharmony_ci  }
96fb726d48Sopenharmony_ci
97fb726d48Sopenharmony_ci  domainInputEvent = (): void => {
98fb726d48Sopenharmony_ci    if (this.startSamp) {
99fb726d48Sopenharmony_ci      if (SpRecordTrace.serialNumber === '') {
100fb726d48Sopenharmony_ci        this.domainInputEL!.dataSource([], '');
101fb726d48Sopenharmony_ci      } else {
102fb726d48Sopenharmony_ci        HdcDeviceManager.fileRecv(this.sysEventConfigPath, () => { }).then((pullRes) => {
103fb726d48Sopenharmony_ci          pullRes.arrayBuffer().then((buffer) => {
104fb726d48Sopenharmony_ci            if (buffer.byteLength > 0) {
105fb726d48Sopenharmony_ci              let dec = new TextDecoder();
106fb726d48Sopenharmony_ci              this.eventConfig = JSON.parse(dec.decode(buffer));
107fb726d48Sopenharmony_ci              let domainList = Object.keys(this.eventConfig!);
108fb726d48Sopenharmony_ci              if (domainList.length > 0) {
109fb726d48Sopenharmony_ci                this.domainInputEL!.dataSource(domainList, 'ALL-Domain', true);
110fb726d48Sopenharmony_ci              } else {
111fb726d48Sopenharmony_ci                this.domainInputEL!.dataSource([], '');
112fb726d48Sopenharmony_ci              }
113fb726d48Sopenharmony_ci            }
114fb726d48Sopenharmony_ci          });
115fb726d48Sopenharmony_ci        });
116fb726d48Sopenharmony_ci      }
117fb726d48Sopenharmony_ci      this.domainInputEl!.removeAttribute('readonly');
118fb726d48Sopenharmony_ci    } else {
119fb726d48Sopenharmony_ci      this.domainInputEl!.setAttribute('readonly', 'readonly');
120fb726d48Sopenharmony_ci      return;
121fb726d48Sopenharmony_ci    }
122fb726d48Sopenharmony_ci  };
123fb726d48Sopenharmony_ci
124fb726d48Sopenharmony_ci  nameInputEvent = (): void => {
125fb726d48Sopenharmony_ci    if (this.startSamp) {
126fb726d48Sopenharmony_ci      if (SpRecordTrace.serialNumber === '') {
127fb726d48Sopenharmony_ci        this.eventNameInputEL!.dataSource([], '');
128fb726d48Sopenharmony_ci      } else {
129fb726d48Sopenharmony_ci        let domain = this.domainInputEL?.value;
130fb726d48Sopenharmony_ci        // @ts-ignore
131fb726d48Sopenharmony_ci        let eventConfigElement = this.eventConfig[domain];
132fb726d48Sopenharmony_ci        if (eventConfigElement) {
133fb726d48Sopenharmony_ci          let eventNameList = Object.keys(eventConfigElement);
134fb726d48Sopenharmony_ci          if (eventNameList?.length > 0) {
135fb726d48Sopenharmony_ci            this.eventNameInputEL!.dataSource(eventNameList, 'ALL-Event', true);
136fb726d48Sopenharmony_ci          } else {
137fb726d48Sopenharmony_ci            this.eventNameInputEL!.dataSource([], '');
138fb726d48Sopenharmony_ci          }
139fb726d48Sopenharmony_ci        } else {
140fb726d48Sopenharmony_ci          let currentData: string[] = [];
141fb726d48Sopenharmony_ci          if (domain === '' || domain === 'ALL-Domain') {
142fb726d48Sopenharmony_ci            //@ts-ignore
143fb726d48Sopenharmony_ci            let domainKey = Object.keys(this.eventConfig);
144fb726d48Sopenharmony_ci            domainKey.forEach((item) => {
145fb726d48Sopenharmony_ci              //@ts-ignore
146fb726d48Sopenharmony_ci              let currentEvent = this.eventConfig[item];
147fb726d48Sopenharmony_ci              let eventList = Object.keys(currentEvent);
148fb726d48Sopenharmony_ci              currentData.push(...eventList);
149fb726d48Sopenharmony_ci            });
150fb726d48Sopenharmony_ci            this.eventNameInputEL!.dataSource(currentData, 'ALL-Event', true);
151fb726d48Sopenharmony_ci          } else {
152fb726d48Sopenharmony_ci            this.eventNameInputEL!.dataSource([], '');
153fb726d48Sopenharmony_ci          }
154fb726d48Sopenharmony_ci        }
155fb726d48Sopenharmony_ci      }
156fb726d48Sopenharmony_ci      this.nameInputEl!.removeAttribute('readonly');
157fb726d48Sopenharmony_ci    } else {
158fb726d48Sopenharmony_ci      this.nameInputEl!.setAttribute('readonly', 'readonly');
159fb726d48Sopenharmony_ci      return;
160fb726d48Sopenharmony_ci    }
161fb726d48Sopenharmony_ci  };
162fb726d48Sopenharmony_ci
163fb726d48Sopenharmony_ci  private updateDisable(isDisable: boolean): void {
164fb726d48Sopenharmony_ci    this.sysEventConfigList!.forEach((configEL) => {
165fb726d48Sopenharmony_ci      if (isDisable) {
166fb726d48Sopenharmony_ci        configEL.removeAttribute('disabled');
167fb726d48Sopenharmony_ci      } else {
168fb726d48Sopenharmony_ci        configEL.setAttribute('disabled', '');
169fb726d48Sopenharmony_ci      }
170fb726d48Sopenharmony_ci    });
171fb726d48Sopenharmony_ci  }
172fb726d48Sopenharmony_ci
173fb726d48Sopenharmony_ci  initHtml(): string {
174fb726d48Sopenharmony_ci    return SpHiSysEventHtml;
175fb726d48Sopenharmony_ci  }
176fb726d48Sopenharmony_ci}
177