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 { StringUtil, HiLog } from '../../../../../../common';
17import { ArrayUtil } from '../../../../../../common/src/main/ets/util/ArrayUtil';
18import DialerPresenter from '../../presenter/dialer/DialerPresenter';
19import { PhoneNumber } from '../../../../../../feature/phonenumber/src/main/ets/PhoneNumber';
20import DetailInfoList from './DetailInfoList';
21import call from '@ohos.telephony.call';
22import EnvironmentProp from '../../feature/EnvironmentProp';
23import { DataItemType } from '../../../../../../feature/contact/src/main/ets/contract/DataType';
24import IndexPresenter from '../../presenter/IndexPresenter';
25import { SelectDialogBuilder } from '../mutisim/SelectSimIdDialog';
26import promptAction from '@ohos.promptAction'
27
28const TAG = 'ContactDetail-detailInfoList';
29
30enum MenuType {
31  Copy, EditBeforeCall
32}
33
34@Component
35export default struct DetailInfoListView {
36  @Link mPresenter: { [key: string]: any };
37  @Link selectSimBuilder: SelectDialogBuilder;
38
39  build() {
40    Column() {
41      //Phone List
42      TelList({
43        List: JSON.stringify(this.mPresenter.contactForm.phones),
44        mPresenter: $mPresenter,
45        selectSimBuilder: $selectSimBuilder
46      });
47
48      // email list
49      DetailInfoList({
50        List: JSON.stringify(this.mPresenter.contactForm.emails),
51        hasArrow: true,
52        dataType: DataItemType.EMAIL
53      });
54
55      // aim List
56      DetailInfoList({
57        List: JSON.stringify(this.mPresenter.contactForm.aims),
58        hasArrow: true,
59        dataType: DataItemType.IM
60      });
61
62      // Nickname
63      DetailInfoList({
64        List: JSON.stringify(this.mPresenter.contactForm.nickname),
65        hasArrow: false,
66        dataType: DataItemType.NICKNAME
67      });
68
69      // Websites
70      DetailInfoList({
71        List: JSON.stringify(this.mPresenter.contactForm.websites),
72        hasArrow: true,
73        dataType: DataItemType.WEBSITE
74      });
75
76      // residential address
77      DetailInfoList({
78        List: JSON.stringify(this.mPresenter.contactForm.houses),
79        hasArrow: false,
80        dataType: DataItemType.SIP_ADDRESS
81      });
82
83      // Remembrance Day
84      DetailInfoList({
85        List: JSON.stringify(this.mPresenter.contactForm.events),
86        hasArrow: true,
87        dataType: DataItemType.EVENT
88      });
89
90      // Associated Person
91      DetailInfoList({
92        List: JSON.stringify(this.mPresenter.contactForm.relationships),
93        hasArrow: true,
94        dataType: DataItemType.RELATION
95      });
96
97      // Remarks
98      DetailInfoList({
99        List: JSON.stringify(this.mPresenter.contactForm.remarks),
100        hasArrow: false,
101        dataType: DataItemType.NOTE
102      });
103    }
104    .margin({ top: $r('app.float.id_card_margin_max') })
105  }
106}
107
108/**
109 * Phone List
110 */
111@Component
112struct TelList {
113  @State List: string = '';
114  @Link private mPresenter: { [key: string]: any };
115  @Link selectSimBuilder: SelectDialogBuilder;
116
117  build() {
118    if (!ArrayUtil.isEmpty(JSON.parse(this.List))) {
119      List() {
120        ForEach(JSON.parse(this.List), item => {
121          ListItem() {
122            TelListItem({
123              message: JSON.stringify(item),
124              mPresenter: $mPresenter,
125              selectSimBuilder: $selectSimBuilder
126            });
127          }
128        }, item => JSON.stringify(item))
129      }
130      .divider({ strokeWidth: $r('app.float.id_divide_width'), color: $r('sys.color.ohos_id_color_list_separator') })
131      .backgroundColor(Color.White)
132      .scrollBar(BarState.Off)
133      .edgeEffect(EdgeEffect.None)
134    }
135  }
136}
137
138/**
139 * Phone Item
140 */
141@Component
142struct TelListItem {
143  @Prop private message: string;
144  @State mIndexPresenter: IndexPresenter = IndexPresenter.getInstance();
145  @State isEmergencyNum: boolean = false;
146  @StorageLink('haveSimCard') haveSimCard: boolean = false;
147  @StorageLink('haveMultiSimCard') haveMultiSimCard: boolean = false;
148  @Link private mPresenter: { [key: string]: any };
149  @Link selectSimBuilder: SelectDialogBuilder;
150
151  @Builder MenuBuilder() {
152    Flex({ direction: FlexDirection.Column,
153      justifyContent: FlexAlign.Center,
154      alignItems: ItemAlign.Start }) {
155      Text(JSON.parse(this.message).data)
156        .fontSize($r('sys.float.ohos_id_text_size_headline8'))
157        .fontColor($r('sys.color.ohos_id_color_text_primary'))
158        .fontWeight(FontWeight.Medium)
159        .textOverflow({ overflow: TextOverflow.Ellipsis })
160        .maxLines(1)
161        .textAlign(TextAlign.Start)
162        .lineHeight('28vp')
163        .height($r('app.float.id_item_height_max'))
164
165      this.MenuDivider()
166      this.MenuView($r('app.string.copy_phoneNumber'), MenuType.Copy)
167      this.MenuDivider()
168      this.MenuView($r('app.string.edit_beforeCall'), MenuType.EditBeforeCall)
169    }
170    .padding({ left: $r('app.float.id_card_margin_large'), right: $r('app.float.id_card_margin_large') })
171    .width(144)
172    .borderRadius($r('app.float.id_card_margin_xxl'))
173    .backgroundColor($r('sys.color.ohos_id_color_primary_contrary'))
174  }
175
176  @Builder MenuView(menuName, itemType) {
177    Row() {
178      Text(menuName)
179        .fontSize($r('sys.float.ohos_id_text_size_body1'))
180        .fontColor($r('sys.color.ohos_id_color_text_primary'))
181        .textAlign(TextAlign.Start)
182    }
183    .width('100%')
184    .padding({ left: $r('app.float.id_card_margin_large'), right: $r('app.float.id_card_margin_large') })
185    .height($r('app.float.id_item_height_mid'))
186    .backgroundColor($r('sys.color.ohos_id_color_primary_contrary'))
187    .onClick(() => {
188      switch (itemType) {
189        case MenuType.Copy:
190          this.mIndexPresenter.getCopy(JSON.parse(this.message).data);
191          break;
192        case MenuType.EditBeforeCall:
193          AppStorage.SetOrCreate('isRouterBack', true);
194          DialerPresenter.getInstance().editPhoneNumber(JSON.parse(this.message).data);
195          AppStorage.SetOrCreate<boolean>('showDialBtn', true);
196          break;
197      }
198    })
199  }
200
201  @Builder MenuDivider() {
202    Divider()
203      .color($r('sys.color.ohos_id_color_list_separator'))
204      .lineCap(LineCapStyle.Square)
205      .width('100%')
206      .padding({ left: $r('app.float.id_card_margin_large'), right: $r('app.float.id_card_margin_large') })
207  }
208
209  build() {
210    Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
211      Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Start }) {
212        Text(PhoneNumber.fromString(JSON.parse(this.message).data).format())
213          .fontSize($r('sys.float.ohos_id_text_size_body1'))
214          .fontWeight(FontWeight.Medium)
215          .textOverflow({ overflow: TextOverflow.Ellipsis })
216          .maxLines(2)
217        Row() {
218          Text(JSON.parse(this.message).labelName)
219            .fontSize($r('sys.float.ohos_id_text_size_body2'))
220            .fontColor($r('sys.color.ohos_id_color_text_tertiary'))
221            .fontWeight(FontWeight.Regular)
222            .visibility(StringUtil.isEmpty(JSON.parse(this.message).labelName) ? Visibility.None : Visibility.Visible)
223          Text(' - ')
224            .fontSize($r('sys.float.ohos_id_text_size_body2'))
225            .fontColor($r('sys.color.ohos_id_color_text_tertiary'))
226            .fontWeight(FontWeight.Regular)
227            .visibility(StringUtil.isEmpty(
228            JSON.parse(this.message)
229              .phoneAddress) ? Visibility.None : Visibility.Visible)
230          Text(JSON.parse(this.message).phoneAddress)
231            .fontSize($r('sys.float.ohos_id_text_size_body2'))
232            .fontColor($r('sys.color.ohos_id_color_text_tertiary'))
233            .fontWeight(FontWeight.Regular)
234        }
235        .margin({ top: $r('app.float.id_card_margin_sm') })
236      }
237      .flexShrink(1)
238
239      Row() {
240        if (call.hasVoiceCapability()) {
241          Button({ stateEffect: false }) {
242            if (!EnvironmentProp.isTablet()) {
243              Image($r('app.media.ic_public_phone'))
244                .objectFit(ImageFit.Contain)
245                .height($r('app.float.id_card_image_small'))
246                .width($r('app.float.id_card_image_small'))
247                .fillColor(this.haveSimCard ? $r('sys.color.ohos_id_color_connected')
248                                            : $r('sys.color.ohos_id_color_tertiary'))
249            } else {
250              Image($r('app.media.ic_public_phone_filled'))
251                .objectFit(ImageFit.Contain)
252                .height($r('app.float.id_card_image_small'))
253                .width($r('app.float.id_card_image_small'))
254                .fillColor(this.haveSimCard ? $r('sys.color.ohos_id_color_connected')
255                                            : $r('sys.color.ohos_id_color_tertiary'))
256            }
257          }
258          .backgroundColor(Color.White)
259          .margin({ right: $r('app.float.id_card_margin_max') })
260        }
261
262        Button({ stateEffect: false }) {
263          if (!EnvironmentProp.isTablet()) {
264            Image($r('app.media.ic_public_message'))
265              .objectFit(ImageFit.Contain)
266              .height($r('app.float.id_card_image_small'))
267              .width($r('app.float.id_card_image_small'))
268          } else {
269            Image($r('app.media.ic_public_message_filled'))
270              .objectFit(ImageFit.Contain)
271              .height($r('app.float.id_card_image_small'))
272              .width($r('app.float.id_card_image_small'))
273          }
274        }
275        .backgroundColor(Color.White)
276        .onClick(() => {
277          this.mPresenter.sendMessage(JSON.parse(this.message).num,
278          JSON.parse(this.message).data, this.mPresenter.contactForm.display_name);
279        })
280      }
281      .flexShrink(0)
282    }
283    .onClick(() => {
284      let phoneNum: string = JSON.parse(this.message).num;
285      if (!this.haveSimCard) {
286        HiLog.i(TAG, 'No SIM card!');
287        //TODO Pop-up window for dialing without a SIM card
288        PhoneNumber.fromString(phoneNum).isDialEmergencyNum().then((res) => {
289          this.isEmergencyNum = res;
290          if (!this.isEmergencyNum) {
291            HiLog.i(TAG, 'Is not Emergency Phone Number!');
292            promptAction.showToast({
293              message: $r('app.string.no_simCardDailog'),
294              duration: 2000,
295              bottom:'60%'
296            });
297            return;
298          } else {
299            HiLog.i(TAG, 'No SIM card, but is Emergency Phone Number');
300            PhoneNumber.fromString(phoneNum).dial();
301          }
302        })
303      } else if (this.haveMultiSimCard) {
304        this.selectSimBuilder.title = $r('app.string.contacts_call_number', phoneNum);
305        this.selectSimBuilder.callback = (value) => {
306          PhoneNumber.fromString(phoneNum).dial({
307            accountId: value,
308          });
309        }
310        this.selectSimBuilder.lastSimId = this.mPresenter.lastUsedSlotId;
311        let spnList = AppStorage.Get<Array<string | Resource>>('spnList');
312        for (var index = 0; index < spnList.length; index++) {
313          this.selectSimBuilder.multiSimCardItems[index].name = spnList[index];
314        }
315        this.selectSimBuilder.controller?.open();
316      } else {
317        PhoneNumber.fromString(phoneNum).dial();
318      }
319    })
320    .width('100%')
321    .height($r('app.float.id_item_height_max'))
322    .bindContextMenu(this.MenuBuilder, ResponseType.LongPress)
323  }
324}