/*
 * 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 TestRemoveCache {
  @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');
  @State url: string = '';

  build() {
    Column() {
      Flex() {
        Button($r('app.string.Preloading_GIF')).onClick(() => {
          this.imageKnifeOption.loadSrc = PageViewModel.getGifMenus()[0];
          this.url =  PageViewModel.getGifMenus()[0];
        })
          .margin({left:10})
        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');
            })
        })
          .margin({left:10})
        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');
            })
        })
          .margin({left:10})
      }

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

      Flex() {
        Button($r('app.string.Delete_all_caches')).onClick(() => {
          ImageKnife.getInstance()
            .removeAllMemoryCache()
          ImageKnife.getInstance()
            .removeAllFileCache()
        })
          .margin({left:5})
        Button($r('app.string.Delete_all_memory_caches')).onClick(() => {
          ImageKnife.getInstance()
            .removeAllMemoryCache()
        })
          .margin({left:5})
        Button($r('app.string.Delete_all_file_caches')).onClick(() => {
          ImageKnife.getInstance()
            .removeAllFileCache()
        })
          .margin({left:5})
      }.margin({ top: 10 })

      Flex() {
        Button($r('app.string.Delete_all_custom_memory_caches')).onClick(() => {
          ImageKnife.getInstance()
            .removeMemoryCache(this.url)
        })
          .margin({left:20})
        Button($r('app.string.Delete_all_custom_file_caches')).onClick(() => {
          ImageKnife.getInstance()
            .removeFileCache(this.url)
        })
          .margin({left:20})
      }.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%')
  }
}