/*
 * Copyright (C) 2025 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 { CacheStrategy, ImageKnife, ImageKnifeComponent, ImageKnifeOption } from "@ohos/libraryimageknife"
import { CommonDataSource } from "./model/CommonDataSource"
import { PageViewModel } from "./model/PageViewModel"
import image from "@ohos.multimedia.image"

export enum ImageType {
  NETWORK = 'network',
  MEMORY = 'memory',
  LOCAL_KEY = 'FileCace'
}

@Observed
export class MessageIndex {
  msg: string

  constructor(msg: string) {
    this.msg = msg
  }
}

@Entry
@Component
struct TestGetCacheImageSync {
  @State hotCommendList: CommonDataSource<MessageIndex> = new CommonDataSource<MessageIndex>([])
  @State data: Array<MessageIndex> = []
  @State imageType: string = ImageType.NETWORK

  aboutToAppear(): void {
    for (let index = 0;index < 8;index++) {
      this.data.push(new MessageIndex(index.toString()))
    }
    this.hotCommendList.addData(this.hotCommendList.totalCount(),this.data)
  }

  build() {
    Column() {
      List() {
        LazyForEach(this.hotCommendList, (item: MessageIndex) => {
          ListItem() {
            ImageComponent({ imageType: this.imageType, num: item.msg })
          }.margin({ top: 5 })
          .height(72)
          .width('100%')
          .onClick(() => {
            item.msg += '00'
            this.hotCommendList.reloadData()
          })
        }, (item: MessageIndex) => JSON.stringify(item))
      }.layoutWeight(1)

      Button('network')
        .onClick(() => {
          this.imageType = ImageType.NETWORK
          this.getUIContext().getPromptAction().showToast({
            message: 'network'
          })
        })
      Button('FileCache')
        .onClick(() => {
          this.imageType = ImageType.LOCAL_KEY
          this.getUIContext().getPromptAction().showToast({
            message: 'FileCache'
          })
        })
    }
  }
}

@Component
struct ImageComponent {
  @State imgOption: ImageKnifeOption = new ImageKnifeOption()
  @Prop imageType: string
  @Prop num: string

  getLocalKey() {
    let cacheKey = 'localPixel'
    let data = ImageKnife.getInstance().getCacheImageSync(cacheKey, CacheStrategy.File)
    if (data) {
      this.imgOption.loadSrc = data.source
    } else {
      let unit = this.getUIContext().getHostContext()?.resourceManager.getMediaContentSync($r('app.media.pngSample'))
      let imageSource = image.createImageSource(unit?.buffer)
      let pixel = imageSource.createPixelMapSync()
      this.imgOption.loadSrc = pixel
      ImageKnife.getInstance().putCacheImage(cacheKey, pixel, CacheStrategy.File, { format: 'image/png', quality: 100 })
    }
  }

  aboutToAppear(): void {
    if (this.imageType === ImageType.NETWORK) {
      this.imgOption = {
        loadSrc: PageViewModel.getMenus()[0],
        placeholderSrc: $r('app.media.loading')
      }
    } else if (this.imageType === ImageType.LOCAL_KEY) {
      this.getLocalKey()
    }
  }

  build() {
    Row() {
      ImageKnifeComponent({
        imageKnifeOption: this.imgOption,
        syncLoad: true
      })
      Text(this.num)
    }
  }
}