/*
 * Copyright (C) 2026 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 { PullToRefresh, PullToRefreshConfigurator } from '@ohos/pulltorefresh'

@Entry
@Component
struct BindSheetPullRefresh {
  @State sheetVisible: boolean = false;
  @State useBindSheetMode: boolean = false;
  @State data: Array<string> = ['item 1', 'item 2', 'item 3', 'item 4', 'item 5'];
  // 嵌套滚动模式:仅交给 PullToRefresh 内部 Scroll,customList 内不可再绑定 scroller
  private nestedScrollScroller: Scroller = new Scroller();
  // 未配置模式:PullToRefresh 与 customList 内 List 共用同一 scroller
  private listModeScroller: Scroller = new Scroller();
  private defaultConfigurator: PullToRefreshConfigurator = new PullToRefreshConfigurator();
  private bindSheetConfigurator: PullToRefreshConfigurator = new PullToRefreshConfigurator()
    .setHasRefresh(false);

  build() {
    Column({ space: 16 }) {
      Button('未配置模式(原始 List)')
        .onClick(() => {
          this.useBindSheetMode = false;
          this.sheetVisible = true;
        })
      Button('开启嵌套滚动模式')
        .onClick(() => {
          this.useBindSheetMode = true;
          this.sheetVisible = true;
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .bindSheet(this.sheetVisible, this.sheetContent(), {
      height: SheetSize.MEDIUM,
      dragBar: true,
      onDisappear: () => {
        this.sheetVisible = false;
      }
    })
  }

  @Builder
  private sheetContent() {
    Column() {
      PullToRefresh({
        data: $data,
        scroller: this.useBindSheetMode ? this.nestedScrollScroller : this.listModeScroller,
        enableNestedScroll: this.useBindSheetMode,
        refreshConfigurator: this.useBindSheetMode ? this.bindSheetConfigurator : this.defaultConfigurator,
        customList: () => {
          if (this.useBindSheetMode) {
            this.sheetColumn();
          } else {
            this.sheetList();
          }
        },
        onLoadMore: () => {
          return new Promise<string>((resolve) => {
            setTimeout(() => {
              this.data = [...this.data, `more ${this.data.length}`];
              resolve('');
            }, 800);
          });
        },
        customLoad: null,
        customRefresh: null,
      })
    }
    .width('100%')
    .height('100%')
  }

  @Builder
  private sheetList() {
    List({ space: 12, scroller: this.listModeScroller }) {
      ForEach(this.data, (item: string) => {
        ListItem() {
          Text(item)
            .width('100%')
            .height(100)
            .fontSize(18)
            .textAlign(TextAlign.Center)
            .backgroundColor('#95efd2')
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#eeeeee')
    .divider({ strokeWidth: 1, color: 0x222222 })
    .edgeEffect(EdgeEffect.None)
  }

  @Builder
  private sheetColumn() {
    Column({ space: 12 }) {
      ForEach(this.data, (item: string) => {
        Text(item)
          .width('100%')
          .height(100)
          .fontSize(18)
          .textAlign(TextAlign.Center)
          .backgroundColor('#95efd2')
      })
    }
    .width('100%')
    .backgroundColor('#eeeeee')
  }
}