/*
 * 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, CacheStrategy } from '@ohos/libraryimageknife'
import { PageViewModel } from './model/PageViewModel'

@Entry
@Component
struct TestIsUrlExist {
  @State imageKnifeOption: ImageKnifeOption = {
    loadSrc: $r('app.media.startIcon'),
    placeholderSrc: $r('app.media.loading'),
    errorholderSrc:$r('app.media.failed')
  }
  @State source: PixelMap | string | Resource = $r('app.media.startIcon')
  @State source1: PixelMap | string | Resource = $r('app.media.startIcon')

  build() {
    Column() {
      Flex() {
        Button($r('app.string.Preloading_GIF')).onClick(() => {
          this.imageKnifeOption.loadSrc =
            PageViewModel.getGifMenus()[0]
        })
        Button($r('app.string.Retrieve_GIF_from_memory')).onClick(() => {
          ImageKnife.getInstance()
            .getCacheImage(PageViewModel.getGifMenus()[0],
              CacheStrategy.Memory)
            .then((data) => {
              this.source = data !== undefined ? data.source : $r('app.media.startIcon')
            })
        })
        Button($r('app.string.Retrieve_GIF_from_disk')).onClick(() => {
          ImageKnife.getInstance()
            .getCacheImage(PageViewModel.getGifMenus()[0],
              CacheStrategy.File)
            .then((data) => {
              this.source1 = data !== undefined ? data.source : $r('app.media.startIcon')
            })
        })
      }

      Flex() {
        Button($r('app.string.Preloading_static_images')).onClick(() => {
          this.imageKnifeOption.loadSrc =
            PageViewModel.getMenus()[0]
        })
        Button($r('app.string.Retrieve_images_from_memory')).onClick(() => {
          ImageKnife.getInstance()
            .getCacheImage(PageViewModel.getMenus()[0],
              CacheStrategy.Memory)
            .then((data) => {
              this.source = data!.source
            })
        })
        Button($r('app.string.Retrieve_images_from_disk')).onClick(() => {
          ImageKnife.getInstance()
            .getCacheImage(PageViewModel.getMenus()[0],
              CacheStrategy.File)
            .then((data) => {
              this.source1 = data!.source
            })
        })
      }.margin({ top: 10 })

      ImageKnifeComponent({
        imageKnifeOption: this.imageKnifeOption
      }).width(200).height(200).margin({ top: 10 })
      Image(this.source).margin({ top: 10 })
        .width(200).height(200)
      Image(this.source1).margin({ top: 10 })
        .width(200).height(200)
    }
    .height('100%').width('100%')
  }
}