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 { LitSelectV } from '../../../base-ui/select/LitSelectV';
18import LitSwitch, { LitSwitchChangeEvent } from '../../../base-ui/switch/lit-switch';
19import '../../../base-ui/select/LitSelectV';
20import '../../../base-ui/select/LitSelect';
21
22import '../../../base-ui/switch/lit-switch';
23import { SpRecordTrace } from '../SpRecordTrace';
24import { Cmd } from '../../../command/Cmd';
25import { SpApplication } from '../../SpApplication';
26import { SpFIleSystemHtml } from './SpFIleSystem.html';
27
28@element('sp-file-system')
29export class SpFileSystem extends BaseElement {
30  private processInput: LitSelectV | undefined | null;
31  private maximum: HTMLInputElement | undefined | null;
32  private selectProcess: HTMLInputElement | undefined | null;
33
34  set startRecord(start: boolean) {
35    if (start) {
36      this.unDisable();
37      this.setAttribute('startRecord', '');
38      this.selectProcess!.removeAttribute('readonly');
39    } else {
40      if (!this.startFileSystem && !this.startVirtualMemory && !this.startIo) {
41        this.removeAttribute('startRecord');
42        this.disable();
43        this.selectProcess!.setAttribute('readonly', 'readonly');
44      }
45    }
46  }
47
48  get startRecord(): boolean {
49    return this.hasAttribute('startRecord');
50  }
51
52  set startFileSystem(start: boolean) {
53    if (start) {
54      this.setAttribute('startSamp', '');
55    } else {
56      this.removeAttribute('startSamp');
57    }
58    this.startRecord = start;
59  }
60
61  get startFileSystem(): boolean {
62    return this.hasAttribute('startSamp');
63  }
64
65  set startVirtualMemory(start: boolean) {
66    if (start) {
67      this.setAttribute('virtual', '');
68    } else {
69      this.removeAttribute('virtual');
70    }
71    this.startRecord = start;
72  }
73
74  get startVirtualMemory(): boolean {
75    return this.hasAttribute('virtual');
76  }
77
78  set startIo(start: boolean) {
79    if (start) {
80      this.setAttribute('io', '');
81    } else {
82      this.removeAttribute('io');
83    }
84    this.startRecord = start;
85  }
86
87  get startIo(): boolean {
88    return this.hasAttribute('io');
89  }
90
91  getSystemConfig(): SystemConfig | undefined {
92    let configVal = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
93    let systemConfig: SystemConfig = {
94      process: '',
95      unWindLevel: 0,
96    };
97    configVal!.forEach((value) => {
98      switch (value.title) {
99        case 'Process':
100          let processSelect = value as LitSelectV;
101          if (processSelect.all) {
102            systemConfig.process = 'ALL';
103            break;
104          }
105          if (processSelect.value.length > 0) {
106            let result = processSelect.value.match(/\((.+?)\)/g);
107            if (result) {
108              systemConfig.process = result.toString().replace(/[()]/g, '');
109            } else {
110              systemConfig.process = processSelect.value;
111            }
112          }
113          break;
114        case 'Max Unwind Level':
115          let maxUnwindLevel = value as HTMLInputElement;
116          if (maxUnwindLevel.value !== '') {
117            systemConfig.unWindLevel = Number(maxUnwindLevel.value);
118          }
119      }
120    });
121    return systemConfig;
122  }
123
124  initElements(): void {
125    this.switchChange();
126    this.processInput = this.shadowRoot?.querySelector<LitSelectV>('lit-select-v');
127    this.maximum = this.shadowRoot?.querySelector<HTMLInputElement>('#maxUnwindLevel');
128    this.maximum?.addEventListener('keyup', () => {
129      this.maximum!.value = this.maximum!.value.replace(/\D/g, '');
130      if (this.maximum!.value !== '') {
131        let mun = parseInt(this.maximum!.value);
132        if (mun > 64 || mun < 0) {
133          this.maximum!.value = '10';
134        }
135      }
136    });
137    this.selectProcess = this.processInput!.shadowRoot?.querySelector('input') as HTMLInputElement;
138    this.selectProcess!.addEventListener('mousedown', () => {
139      if (SpRecordTrace.serialNumber === '') {
140        this.processInput!.dataSource([], '');
141      } else {
142        Cmd.getProcess().then((processList) => {
143          if (processList.length > 0 && this.startRecord) {
144            this.selectProcess!.setAttribute('readonly', 'readonly');
145          }
146          this.processInput?.dataSource(processList, 'ALL-Process');
147        });
148      }
149    });
150    this.disable();
151  }
152
153  private switchChange(): void {
154    let fileSystemSwitch = this.shadowRoot?.querySelector<LitSwitch>('#fileSystem');
155    fileSystemSwitch!.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
156      let detail = event.detail;
157      this.startFileSystem = detail!.checked;
158    });
159    let pageFaultSwitch = this.shadowRoot?.querySelector<LitSwitch>('#pageFault');
160    pageFaultSwitch!.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
161      let detail = event.detail;
162      this.startVirtualMemory = detail!.checked;
163    });
164    let bioLatencySwitch = this.shadowRoot?.querySelector<LitSwitch>('#bioLatency');
165    bioLatencySwitch!.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
166      let detail = event.detail;
167      this.startIo = detail!.checked;
168    });
169  }
170
171  private unDisable(): void {
172    let fileSystemConfigVals = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
173    fileSystemConfigVals!.forEach((fileSystemConfigVal) => {
174      fileSystemConfigVal.removeAttribute('disabled');
175    });
176  }
177
178  private disable(): void {
179    let fileSystemConfigVals = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
180    fileSystemConfigVals!.forEach((fileSystemConfigVal) => {
181      if (
182        fileSystemConfigVal.title === 'Start FileSystem Record' ||
183        fileSystemConfigVal.title === 'Start Page Fault Record' ||
184        fileSystemConfigVal.title === 'Start BIO Latency Record'
185      ) {
186      } else {
187        fileSystemConfigVal.setAttribute('disabled', '');
188      }
189    });
190  }
191
192  connectedCallback(): void {
193    let traceMode = this.shadowRoot!.querySelector('#traceMode') as HTMLDivElement;
194    this.maximum!.onkeydown = (ev): void => {
195      // @ts-ignore
196      if (ev.key === '0' && ev.target.value.length === 1 && ev.target.value === '0') {
197        ev.preventDefault();
198      }
199    };
200    let isLongTrace = SpApplication.isLongTrace;
201    if (isLongTrace) {
202      traceMode!.style.display = 'block';
203    } else {
204      traceMode!.style.display = 'none';
205    }
206  }
207
208  initHtml(): string {
209    return SpFIleSystemHtml;
210  }
211}
212
213export interface SystemConfig {
214  process: string;
215  unWindLevel: number;
216}
217