import { runCatching, seconds } from '@polyvharmony/media-player-sdk';
import { VideoMore } from '../model/VideoMoreModel'
import { BasicConstant } from '@itcast/basic'
import { promptAction, router } from '@kit.ArkUI';
import { PLVMockMediaResourceData } from '../polyv/PLVMockMediaResourceData'
import {
  PLVMediaPlayerSingleVideoPageParam
} from 'media-player-common';
import HashMap from '@ohos.util.HashMap';
import { hdHttp, HdResponse ,logger} from '@itcast/basic/Index'
import { BusinessError } from '@kit.BasicServicesKit';
import { VideoDetailModel } from '../model/VideoDetailModel'
@Preview
@Component
export struct ItemCompVideo {
  @Prop item: VideoMore ;

  hashMap: HashMap<string, string> = new HashMap();
  @State heightss:Length=247
  aboutToAppear(): void {

  }


  build() {
    Column() {

      Image(BasicConstant.urlHtml+this.item.imgpath).width('100%').height(102)
        .objectFit(ImageFit.Fill)

            Text(this.item.name).maxLines(2).fontSize(15).fontColor('app.color.666666').textAlign(TextAlign.Start).height(36)
              .textOverflow({ overflow: TextOverflow.Ellipsis }).id('title').width('100%').margin({top:10}).padding({left:10,right:10})

            Row(){
              Text(this.item.public_name).fontSize(14)
                .fontColor($r('app.color.999999')).maxLines(1) .textOverflow({ overflow: TextOverflow.Ellipsis }).layoutWeight(1)
              Blank()
              Image($r('app.media.video_look')).width(18).height(11)
              Text(this.item.readnum+'').fontSize(14)
                .fontColor($r('app.color.999999'))
            }
            .padding({left:10,right:10,bottom:10})
            .width('100%')
            .margin({top:6})


    }.backgroundColor(Color.White)
    .borderRadius(5)
    .onClick(()=>{
      this.getVideoDetail(this.item.uuid)

    })
    .width('100%').clip(true)



  }

  getVideoDetail(video_uuid:string)
  {
    this.hashMap.clear();
    this.hashMap.set('video_uuid', video_uuid)
    hdHttp.httpReq<string>(BasicConstant.videoDetail,this.hashMap).then(async (res: HdResponse<string>) => {
      logger.info('Response videoDetail'+res);
      let json:VideoDetailModel = JSON.parse(res+'') as VideoDetailModel;
      this.goPLVMediaPlayerSingleVideoPage(json.video.polyv_uuid,video_uuid)

    }).catch((err: BusinessError) => {
      this.goPLVMediaPlayerSingleVideoPage('','')
    })
  }

  async goPLVMediaPlayerSingleVideoPage(vid:string,video_uuid:string)
  {
    const mediaResourcesResult = await runCatching(PLVMockMediaResourceData.getInstance().setupMediaResourcesFromLocal(vid))
    if (mediaResourcesResult.success === false) {
      promptAction.showToast({
        message: `'视频数据初始化失败': ${mediaResourcesResult.error}`,
        duration: seconds(3).toMillis()
      })
      return
    }
    const mediaResource = mediaResourcesResult.data[0]
    router.pushUrl({
      url:'pages/VideoPage/PLVMediaPlayerSingleVideoPage',
      params: {
        video_uuid:video_uuid,
        param:new PLVMediaPlayerSingleVideoPageParam(mediaResource)
      }
    })
  }



}