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';

@Entry
@ComponentV2
struct DownloadHistory {
  @Local currentIndex: number = 0;
  tabController: TabsController = new TabsController();
  titles: Array<string> = ['本月', '上月'];

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

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

  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)
    } else {
      return Math.trunc(lastDate.getTime() / 1000)
    }
  }

  build() {
    Column() {
      TitleBar({ title: '本月下载' })

      Stack({ alignContent: Alignment.TopStart }) {
        Tabs({ barPosition: BarPosition.Start, controller: this.tabController }) {
          TabContent() {
            DownloadHistoryItemPage({type: 0})
          }

          TabContent() {
            DownloadHistoryItemPage({type: 1})
          }
        }
        .scrollable(false)
        /*.onSelected((index: number) => {
          this.currentIndex = index;
        })*/

        Row({ space: 40 }) {
          ForEach(this.titles, (title: string, index) => {
            this.tab(title, index);
          })
          Blank().layoutWeight(1)
          Row() {
            Image($r('app.media.ic_clear_record')).width(16).height(16)
            Text('全部清空').fontColor($r('app.color.color_80ffffff')).fontSize(12).margin({ left: 4 })
          }
          .onClick(() => {
            TipDialog.show(this.getUIContext(), {title: '提示', content: '确定清空记录？', callback: {
              confirm: () => {
                this.viewModel.deleteHistoryList(`${this.getStartTime()}`, `${this.getEndTime()}`)
              }
            }})
          })
        }.padding({ left: 16, right: 16 })
        .margin({ top: 10 })
      }.layoutWeight(1)
    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.window_background'))
  }

  @Builder
  tab(tabName: string, tabIndex: number) {
    Row() {
      Text(tabName)
        .fontSize(this.currentIndex === tabIndex ? 17 : 14)
        .fontWeight(this.currentIndex === tabIndex ? FontWeight.Medium : FontWeight.Regular)
        .lineHeight(24)
        .fontColor(tabIndex === this.currentIndex ? $r("app.color.color_466afd") : $r('app.color.color_50ffffff'))
    }
    .width('auto')
    .height('auto')
    .onClick(() => {
      this.tabController.changeIndex(tabIndex);
      this.currentIndex = tabIndex;
    })
  }
}