import { BasicConstant, authStore } from '@itcast/basic'
import { commentModel } from '../model/VideoCommentModel'
import { CommentChildView } from '../components/CommentChildView'
import { router,promptAction } from '@kit.ArkUI';
import { hdHttp, HdResponse ,logger} from '@itcast/basic/Index'
import HashMap from '@ohos.util.HashMap';
import { BusinessError,emitter } from '@kit.BasicServicesKit';
import { rcp } from '@kit.RemoteCommunicationKit';

@Preview
@Component
export struct VideoDetailsComment {
  @Prop model:commentModel;
  @Prop videoCommentUuid:string;
  @State isShowMoreComment:boolean = false;

  deleteComment() {
    const updateDataUrl:string = BasicConstant.deleteComment;
    const postContent = new rcp.MultipartForm({
      'uuid': this.model.comment_uuid
    })
    const session = rcp.createSession();
    session.post(updateDataUrl, postContent)
      .then((response) => {
        if (response.statusCode == 200) {
          emitter.emit({
            eventId: 250529,
            priority: emitter.EventPriority.HIGH
          })
          promptAction.showToast({ message: '删除评论成功', duration: 1000 })
        }
      })
      .catch((err: BusinessError) => {
        console.error(`Response err: Code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err)}`);
      })
  }

  build() {
    Column() {
      Row() {
        Row({ space: 10 }) {
          Image(BasicConstant.urlHtml + this.model.photo)
            .alt($r('app.media.userPhoto_default'))
            .width(40).height(40)
            .borderRadius(4)
          Text(this.model.user_status == 1 ? this.model.name : '账号已注销')
            .fontSize(16).fontColor('#333333')
            .height('100%')
            .alignSelf(ItemAlign.Start)
        }
        .margin({ left: 10, right: authStore.getUser().uuid == this.model.user_uuid ? 35 : 65 })
        .layoutWeight(1)//权重自定义
        .justifyContent(FlexAlign.Start)

        if (authStore.getUser().uuid == this.model.user_uuid) {
          Image($r('app.media.video_comment_delete'))
            .width(20).height(20).margin({right:15})
            .onClick(()=>{
              promptAction.showDialog({ title: '温馨提示', message: '确定要删除评论？',
                buttons: [{ text: '取消', color: '#666666' }, { text: '确定', color: $r('app.color.main_color') }],
              })
                .then(data => {
                  if (data.index == 1) {
                    this.deleteComment();
                  }
                })
            })
        } else {
          Text('回复')
            .textAlign(TextAlign.Center)
            .width(50).height(25).margin({right:15})
            .borderRadius(10)
            .backgroundColor('#f4f4f4')
            .borderWidth(1).borderColor($r('app.color.main_color'))
            .fontSize(16).fontColor($r('app.color.main_color'))
            .onClick(() => {
              if (this.model.user_status === 1) {
                router.pushUrl({
                  params:{'replyName':this.model.name,'comment_id':this.videoCommentUuid,'comment':this.model.content},
                  url: 'pages/VideoPage/CommentReplyPage'
                })
              } else  {
                promptAction.showToast({ message: '该评论无法回复', duration: 1000 })
              }
            })
        }
      }.width('100%').height(40).margin({top:15}).alignItems(VerticalAlign.Center)
      Column() {
        Text(this.model.content)
          .fontSize(15).fontColor('#666666')
        Text(this.model.create_date)
          .margin({top:5})
          .fontSize(14)
          .fontColor('#999999')
      }.margin({left:50}).alignItems(HorizontalAlign.Start).width('calc(100% - 70vp)')

      Column() {
        if (this.model.childs.length > 3) {
          ForEach(this.model.childs.slice(0, this.isShowMoreComment ? this.model.childs.length : 3),
            (childModels: commentModel) => {
              ListItem() {
                CommentChildView({model:childModels});
              }
              .width('100%')
            })
          this.dynamicFooter((isShow: boolean) => {
            this.isShowMoreComment = isShow;
          });
        } else {
          ForEach(this.model.childs, (childModels: commentModel) => {
            CommentChildView({model:childModels});
          })
        }
      }
      .margin({left:50}).alignItems(HorizontalAlign.Start).width('calc(100% - 70vp)')

      Divider()
        .color(Color.White)
        .width('100%').strokeWidth(1).margin({top:5})
    }
  }

  // Footer构建器
  @Builder
  dynamicFooter(onClick: (isShow:boolean) => void ) {
    Row({space:5}) {
      Text(this.isShowMoreComment ? '--收起' : '--展开更多回复')
        .fontColor($r('app.color.main_color'))
        .fontSize(13)
      Image(this.isShowMoreComment?$r('app.media.closePinglun'):$r('app.media.openPinglun'))
        .width(15).height(15)
    }
    .width(200)
    .height(30)
    .justifyContent(FlexAlign.Start)
    .onClick(()=>onClick(!this.isShowMoreComment))
  }
}
