import { HMRouterMgr } from "@hadss/hmrouter"
import { router } from "@kit.ArkUI"
import { StringUtils } from "../../util/StringUtils"

@Component
export struct BaseTopBar{
  @State title:string|Resource=''
  // @State subtitle:string|null=null
  @State leftText:string|null=null
  @State leftImage:Resource = $r('app.media.icon_back3')

  @State rightText:string|null=null
  @State rightImage:Resource |null =null
  @State isBackImag:boolean  = true

  //回退
  leftClickEvent?:()=>void
  //右边按钮
  rightClickEvent?:()=>void

  build() {
    Row() {
      Row(){
        Image(this.leftImage)
          .width($r('app.float.default_24'))
          .height($r('app.float.default_24'))
          .visibility(this.isBackImag?Visibility.Visible:Visibility.None)

        Text(this.leftText)
          .fontColor($r('app.color.white'))
          .fontSize($r('app.float.text_size_14'))
          .visibility(StringUtils.isNullOrEmpty(this.leftText)?Visibility.None:Visibility.Visible)
      }
      .height("100%")
      .width($r('app.float.default_68'))
      .alignItems(VerticalAlign.Bottom)
      .padding({ left:$r('app.float.default_10') })
      .onTouch((event) => {
        if (event.type === TouchType.Down) {
          this.leftImage = $r('app.media.icon_back2');
        } else if (event.type === TouchType.Up) {
          this.leftImage = $r('app.media.icon_back3');
        }
      })
      .onClick(() => {
        if (!this.isBackImag)return
        if (this.leftClickEvent) {
          this.leftClickEvent()
        }
        HMRouterMgr.pop()
      })

      Text(this.title)
        .fontColor($r('app.color.white'))
        .fontSize($r('app.float.text_size_18'))
        .layoutWeight(1)
        .textAlign(TextAlign.Center)
        .margin({ bottom: $r('app.float.default_2'), left: $r('app.float.default_10') ,right: $r('app.float.default_10') })

      Row(){
        Image(this.rightImage)
          .width($r('app.float.default_24'))
          .height($r('app.float.default_24'))
          .visibility(this.rightImage == null?Visibility.None:Visibility.Visible)

        Text(this.rightText)
          .fontColor($r('app.color.white'))
          .fontSize($r('app.float.text_size_14'))
          .visibility(StringUtils.isNullOrEmpty(this.rightText)?Visibility.None:Visibility.Visible)

      }
      .alignItems(VerticalAlign.Bottom)
      .height("100%")
      .width($r('app.float.default_68'))
      .padding({ right: $r('app.float.default_10') })
      .onClick(() => {
        if (this.rightClickEvent) {
          this.rightClickEvent()
        }
      })
    }
    .alignItems(VerticalAlign.Bottom)
    .padding({ bottom: 10 })
    .width('100%')
    .backgroundColor($r('app.color.bull'))
    .height($r('app.float.default_88'))
  }
}
