import { EmptyView, PageStatus } from '../../../../view/EmptyView';
import { VideoRecordItemView } from '../../../../view/RecordItemView';
import { AppUtil, PermissionUtil } from '@pura/harmony-utils';
import { ToastUtils } from '../../../../utils/ToastUtils';
import { MediaRecordEntity } from '../../../../entity/MediaRecordEntity';
import { EventConstants } from '../../../../common/EventConstants';
import { MediaAction, MediaManager, MediaType } from '../../../../manager/MediaManager';
import { TipDialog } from '../../../../dialog/TipDialog';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { LocalMediaManager } from '../../../../manager/LocalMediaManager';

@ComponentV2
export struct VideoRecordPage {
  @Local mediaList: Array<MediaRecordEntity> = [];
  @Local rowCount: number = 1;
  @Local isCheckAll: boolean = false

  aboutToAppear(): void {
    this.refresh()
    this.initObserver()
  }

  initObserver() {
    AppUtil.getContext().eventHub.on(EventConstants.MediaActionEvent, (type: MediaType, action: MediaAction) => {
      if (type === MediaType.VIDEO) {
        if (action === MediaAction.CLEAR && this.mediaList.length > 0) {
          this.deleteAll()
        } else {
          this.refresh()
        }
      }
    })

    AppUtil.getContext().eventHub.on(EventConstants.RecordRefreshEvent, () => {
      this.refresh()
    })
  }

  async refresh() {
    this.mediaList = await MediaManager.getVideoList()
    this.rowCount = this.computeRowCount(this.mediaList)
  }

  deleteAll() {
    TipDialog.show(this.getUIContext(), {
      title: '提示', content: '确定清空视频？', callback: {
        confirm: () => {
          let uriArray = new Array<string>()
          this.mediaList.forEach((item) => {
            uriArray.push(item.uri!!)
          })
          photoAccessHelper.MediaAssetChangeRequest.deleteAssets(AppUtil.getContext(), uriArray)
            .then(() => {
              LocalMediaManager.deleteAllVideos()
              AppUtil.getContext().eventHub.emit(EventConstants.MediaActionEvent, MediaType.VIDEO, MediaAction.DELETE)
              ToastUtils.show('删除成功')
            })
            .catch(() => {
              ToastUtils.show('删除失败, 请到相册中手动删除')
            })
        }
      }
    })
  }

  computeRowCount(list: Array<MediaRecordEntity>): number {
    if (list.length > 8) {
      return 3;
    } else if (list.length >= 3 && list.length <= 8) {
      return 2;
    } else {
      return 1;
    }
  }

  build() {
    Stack() {
      Column() {
        Grid() {
          ForEach(this.mediaList, (item: MediaRecordEntity, index) => {
            GridItem() {
              VideoRecordItemView({ media: item, rowCount: this.rowCount })
            }
          })
        }
        .scrollBar(BarState.Off)
        .columnsTemplate(this.rowCount === 1 ? '1fr' : this.rowCount === 2 ? '1fr 1fr' : '1fr 1fr 1fr')
        .rowsGap(10)
        .columnsGap(10)
        .margin({ left: 16, right: 16, bottom: 15 })
        .layoutWeight(1)
      }

      EmptyView({
        status: this.mediaList.length > 0 ? PageStatus.GONE : PageStatus.NO_DATA,
        noDataImage: $r('app.media.ic_empty_data'),
        noDataText: '暂无数据'
      })
    }
  }
}