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

@Entry
@Component
struct TestCacheDataPage {
  @State cacheUpLimit: number = 0;
  @State currentNum: number = 0;
  @State currentSize: number = 0;
  @State currentWidth: number = 200
  @State currentHeight: number = 200
  @State markersLimitText: string = getContext(this).resourceManager.getStringSync($r('app.string.memory'))
  @State markersNumText: string = getContext(this).resourceManager.getStringSync($r('app.string.memory'))
  @State markersSizeText: string = getContext(this).resourceManager.getStringSync($r('app.string.memory'))
  @State ImageKnifeOption: ImageKnifeOption = {
    loadSrc: '',
    objectFit: ImageFit.Contain,
    onLoadListener: {
      onLoadFailed: (err) => {
        console.error('Load Failed Reason: ' + err);
      },
      onLoadSuccess: (data) => {
        return data;
      },
    },
    border: { radius: 50 }
  }

  aboutToAppear(): void {
    ImageKnife.getInstance().initFileCache(getContext(this), 256, 256 * 1024 * 1024, 'ImageKnifeCache1')
  }

  build() {
    Column() {

      ImageKnifeComponent(
        { imageKnifeOption: this.ImageKnifeOption })
        .height(this.currentHeight)
        .width(this.currentWidth)
        .margin({ top: 10 })

      Button($r('app.string.load_memory'))
        .onClick(() => {
          this.ImageKnifeOption = {
            loadSrc: PageViewModel.getMenus()[0],
            objectFit: ImageFit.Contain,
            writeCacheStrategy: CacheStrategy.Memory,
            border: { radius: 50 },
          }
        })
      Button($r('app.string.load_disk'))
        .onClick(() => {
          this.ImageKnifeOption = {
            loadSrc: PageViewModel.getMenus()[1],
            objectFit: ImageFit.Contain,
            writeCacheStrategy: CacheStrategy.File,
            border: { radius: 50 },
          }
        })
      Text($r('app.string.cur_cache_limit', this.markersLimitText, this.cacheUpLimit))
        .fontSize(20)
        .margin({ bottom: 8 });
      Text($r('app.string.cur_cache_image_num', this.markersNumText, this.currentNum))
        .fontSize(20)
        .margin({ bottom: 8 });
      Text($r('app.string.cur_cache_size', this.markersSizeText, this.currentSize)).fontSize(20).margin({ bottom: 20 });

      Button($r('app.string.get_cur_memory_limit')).onClick(() => {
        let result = ImageKnife.getInstance().getCacheLimitSize(CacheStrategy.Memory);
        this.markersLimitText = getContext(this).resourceManager.getStringSync($r('app.string.memory'))
        if (result) {
          this.cacheUpLimit = result / (1024 * 1024);
        } else {
          this.cacheUpLimit = 0;
        }
      }).margin({ bottom: 8 });
      Button($r('app.string.get_img_number_of_cache')).onClick(() => {
        let result = ImageKnife.getInstance().getCurrentCacheNum(CacheStrategy.Memory);
        this.markersNumText = getContext(this).resourceManager.getStringSync($r('app.string.memory'))
        if (result) {
          this.currentNum = result;
        } else {
          this.currentNum = 0;
        }
      }).margin({ bottom: 8 });
      Button($r('app.string.get_cur_memory_size')).onClick(() => {
        let result = ImageKnife.getInstance().getCurrentCacheSize(CacheStrategy.Memory);
        this.markersSizeText = getContext(this).resourceManager.getStringSync($r('app.string.memory'))
        if (result) {
          this.currentSize = result / (1024 * 1024);
        } else {
          this.currentSize = 0;
        }
      }).margin({ bottom: 8 });

      Button($r('app.string.get_cur_disk_limit')).onClick(() => {
        let result = ImageKnife.getInstance().getCacheLimitSize(CacheStrategy.File);
        this.markersLimitText = getContext(this).resourceManager.getStringSync($r('app.string.disk'))
        if (result) {
          this.cacheUpLimit = result / (1024 * 1024);
        } else {
          this.cacheUpLimit = 0;
        }
      }).margin({ bottom: 8 });
      Button($r('app.string.get_img_number_of_disk')).onClick(() => {
        let result = ImageKnife.getInstance().getCurrentCacheNum(CacheStrategy.File);
        this.markersNumText = getContext(this).resourceManager.getStringSync($r('app.string.disk'))
        if (result) {
          this.currentNum = result;
        } else {
          this.currentNum = 0;
        }
      }).margin({ bottom: 8 });
      Button($r('app.string.get_cur_disk_size')).onClick(() => {
        let result = ImageKnife.getInstance().getCurrentCacheSize(CacheStrategy.File);
        this.markersSizeText = getContext(this).resourceManager.getStringSync($r('app.string.disk'))
        if (result) {
          this.currentSize = result / (1024 * 1024);
        } else {
          this.currentSize = 0;
        }
      }).margin({ bottom: 8 });
    }
    .height('100%').width('100%').margin({ top: 50 })
  }
}