/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { ImageKnifeComponent, ImageKnife, ImageKnifeOption } from '@ohos/libraryimageknife'
import { photoAccessHelper } from '@kit.MediaLibraryKit'

@Entry
@Component
struct SingleCustomPage {
  @State imageKnifeOption: ImageKnifeOption = {
    loadSrc: $r('app.media.startIcon'),
    placeholderSrc: $r('app.media.loading'),
    customGetImage: custom
  }
  @State imageKnifeOption1: ImageKnifeOption = {
    loadSrc: $r('app.media.startIcon'),
    placeholderSrc: $r('app.media.loading'),
    customGetImage: custom
  }
  @State imageKnifeOption2: ImageKnifeOption = {
    loadSrc: $r('app.media.startIcon'),
    placeholderSrc: $r('app.media.loading'),
    customGetImage: custom
  }

  getResourceString(res: Resource) {
    return getContext().resourceManager.getStringSync(res.id)
  }

  getFileImage() {
    let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
    photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    photoSelectOptions.maxSelectNumber = 1;
    let uris: Array<string> = [];
    let photoViewPicker = new photoAccessHelper.PhotoViewPicker();
    photoViewPicker.select(photoSelectOptions)
      .then((result) => {
        uris = result.photoUris;
        this.imageKnifeOption1.loadSrc = uris[0]
      })
  }

  build() {
    Column() {
      Button(this.getResourceString($r('app.string.Custom_network_download')) + ' https://').onClick(() => {
        this.imageKnifeOption.loadSrc = 'https://www.baidu.com'
      })
      ImageKnifeComponent({
        imageKnifeOption: this.imageKnifeOption
      }).width(200)
        .height(200)
      Button(this.getResourceString($r('app.string.Custom_network_download')) + ' file://').onClick(() => {
        this.getFileImage()
      })
      ImageKnifeComponent({
        imageKnifeOption: this.imageKnifeOption1
      }).width(200)
        .height(200)
      Button(this.getResourceString($r('app.string.Custom_network_download')) + ' data/').onClick(() => {
        ImageKnife.getInstance()
          .preLoadCache('https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658')
          .then((cachePath) => {
            this.imageKnifeOption2.loadSrc = cachePath
          })
      })
      ImageKnifeComponent({
        imageKnifeOption: this.imageKnifeOption2
      }).width(200)
        .height(200)
    }
    .width('100%')
    .height('100%')
  }
}

@Concurrent
async function custom(context: Context, src: string | PixelMap | Resource,
  headers?: Record<string, Object>): Promise<ArrayBuffer | undefined> {
  console.info('ImageKnife::  custom download:' + src)
  // 举例写死从本地文件读取,也可以自己请求网络图片
  let buffer = context.resourceManager.getMediaContentSync($r('app.media.pngSample').id).buffer as ArrayBuffer
  return buffer
}