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

class ArrayElement {
  public src: string = '';
  public w: number = 0;
  public h: number = 0;
}

@Entry
@Component
struct TestListImageKnifeCallbackPage {
  private wid: number = 200;
  private hig: number = 200;
  private dataArray: string[] = PageViewModel.getDataIs()
  private data: Array<ArrayElement> = [];

  aboutToAppear(): void {
    for (let i = 0; i < this.dataArray.length; i++) {
      let element: ArrayElement = {
        src: this.dataArray[i],
        w: this.wid -(i*5),
        h: this.hig -(i*5)
      }
      this.data.push(element);
    }
  }

  build() {
    List({ space: 3 }) {
      ForEach(this.data, (item: ArrayElement) => {
        ListItem() {
          ImageKnifeComponent({
            imageKnifeOption:
            {
              loadSrc: item.src,
              objectFit: ImageFit.Contain,
              onLoadListener: {
                onLoadStart: (data) => {
                  console.log('listCache start:{ url:' + data?.imageKnifeOption.loadSrc +
                    ',componentWidth:' + data?.componentWidth +
                    ',componentHeight:' + data?.componentHeight +
                    '},' + JSON.stringify(data?.imageKnifeData))
                },
                onLoadFailed: (res, req) => {
                  console.log('listCache onLoadFailed:res:' + res + ';' + JSON.stringify(req?.imageKnifeData))
                },
                onLoadSuccess: (data, imageData,req) => {
                  console.log('listCache onLoadSuccess:' + JSON.stringify(req?.imageKnifeData))
                },
                onLoadCancel: (res, req) => {
                  console.log('listCache onLoadCancel:res:' + res + ';' + JSON.stringify(req?.imageKnifeData))
                }
              },
              border: { radius: 50 },
            }
          }).height(item.w).width(item.h)
        }.width('100%')
      }, (item: ArrayElement,index) => (item.src+index))
    }.width('100%').height('100%')
  }
}