import { AppUtil, DateUtil } from '@pura/harmony-utils';
import { EventConstants } from '../../../../common/EventConstants';
import { TipDialog } from '../../../../dialog/TipDialog';
import { TitleBar } from '../../../../view/TitleBar';
import { DownloadHistoryViewModel } from '../../../../viewModel/DownloadHistoryViewModel';
import { DownloadHistoryItemPage } from './DownloadHistoryItemPage';
import { CommonModifier } from '@kit.ArkUI';

@Entry
@ComponentV2
struct DownloadHistory {
  @Local tabBarModifier: CommonModifier = new CommonModifier()
  @Local currentIndex: number = 0;

  private tabController: TabsController = new TabsController();
  private titles: Array<string> = ['本月', '上月'];

  private viewModel: DownloadHistoryViewModel = new DownloadHistoryViewModel(this.getUIContext())

  @Monitor('viewModel.deleteHistory')
  onDeleteDownloadHistory(monitor: IMonitor) {
    AppUtil.getContext().eventHub.emit(EventConstants.DownloadHistoryRefreshEvent, this.currentIndex)
  }

  aboutToAppear(): void {
    this.tabBarModifier.align(Alignment.Start)
  }

  getStartTime(): number {
    let nowDate = new Date(DateUtil.getNowYear(), DateUtil.getNowMonth() - 1, 1)
    let lastDate = new Date(DateUtil.getNowYear(), DateUtil.getNowMonth() - 2, 1)
    if (this.currentIndex === 0) {
      return Math.trunc(nowDate.getTime() / 1000)
    } else {
      return Math.trunc(lastDate.getTime() / 1000)
    }
  }

  getEndTime(): number {
    let nowDate = new Date(DateUtil.getNowYear(), DateUtil.getNowMonth(), 1)
    let lastDate = new Date(DateUtil.getNowYear(), DateUtil.getNowMonth() - 1, 1)
    if (this.currentIndex === 0) {
      return Math.trunc(nowDate.getTime() / 1000) - 1
    } else {
      return Math.trunc(lastDate.getTime() / 1000) - 1
    }
  }

  build() {
    Column() {
      TitleBar({ title: '提取记录' })

      Stack({ alignContent: Alignment.TopEnd }) {
        Tabs({ barPosition: BarPosition.Start, controller: this.tabController, barModifier: this.tabBarModifier }) {
          TabContent() {
            DownloadHistoryItemPage({type: 0})
          }
          .tabBar(this.tabBuilder(this.titles[0], 0))

          TabContent() {
            DownloadHistoryItemPage({type: 1})
          }
          .tabBar(this.tabBuilder(this.titles[1], 1))
        }
        .scrollable(false)
        .barMode(BarMode.Scrollable)
        .onSelected((index: number) => {
          this.currentIndex = index;
        })

        Row() {
          Image($r('app.media.ic_clear_record')).width(16).height(16)
          Text('全部清空').fontColor('#6C7084').fontSize(12).margin({ left: 4 })
        }
        .onClick(() => {
          TipDialog.show(this.getUIContext(), {title: '提示', content: '确定清空记录？', callback: {
            confirm: () => {
              this.viewModel.deleteHistoryList(`${this.getStartTime()}`, `${this.getEndTime()}`)
            }
          }})
        })
        .height(50)
        .margin({ top: 3, right: 16 })
      }.layoutWeight(1)
    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.window_background'))
  }

  @Builder
  tabBuilder(title: string, targetIndex: number) {
    Stack() {
      if (this.currentIndex === targetIndex) {
        Image($r('app.media.ic_tab_indicator')).width(27).height(11).margin({ top: 13 })
      }
      Text(title)
        .fontColor(this.currentIndex === targetIndex ? $r("app.color.color_212226") : $r('app.color.color_5c5f6c'))
        .fontSize(this.currentIndex === targetIndex ? 16 : 14)
        .fontWeight(this.currentIndex === targetIndex ? FontWeight.Medium : FontWeight.Regular)
    }
    .width('auto')
    .height(50)
    .margin({ left: targetIndex === 0 ? 16 : 40 })
  }
}