/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */
package smartrefresh.toprefresh

import ohos.state_macro_manage.*
import ohos.system_date_time.*
import std.sync.*
import std.time.*
import ohos.state_manage.*
import std.math.*
import smartrefresh.utils.*

@Component
public class FlyRefreshHeader {
    @Link
    var model: SmartRefreshForFlyRefreshModel
    var settings: RenderingContextSettings = RenderingContextSettings(antialias: true)
    var context: CanvasRenderingContext2D = CanvasRenderingContext2D(settings)
    @State
    var ready: Bool = false
    var mScale: Float64 = 0.0
    protected var mScaleX: Float64 = 1.0
    protected var mScaleY: Float64 = 1.0
    protected var mTrunk: Path2D = Path2D();
    protected var mBranch: Path2D = Path2D();
    @State
    var x: Float64 = 0.0
    @State
    var y: Float64 = 0.0
    @State
    var angle: Float32 = 0.0
    @State
    var isClick: Bool = true
    @State
    var isAdd: Bool = false
    @State
    var initHeight: Float64 = 0.0
    var pullRefreshImage1: AppResource = @r(app.media.bg1)
    var pullRefreshImage2: AppResource = @r(app.media.bg2)
    var pullRefreshImage3: AppResource = @r(app.media.bg3)
    var pullRefreshImage4: AppResource = @r(app.media.tree1)
    var pullRefreshImage5: AppResource = @r(app.media.tree2)
    var pullRefreshImage6: AppResource = @r(app.media.tree3)
    var pullRefreshImage7: AppResource = @r(app.media.tree4)
    var pullRefreshImage8: AppResource = @r(app.media.tree5)
    var i: Int64 = 0

    protected override func aboutToAppear() {
        initHeight = model.initHeaderHeight
        model.flyRefreshHeaderIsShow = true
    }

    protected override func aboutToDisappear() {
        model.refreshCallback()
    }

    func build() {
        Flex(FlexParams(direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start)) {
            Text("${changeStatus(model)}").visibility(Visibility.None)
            Stack(Alignment.Bottom) {
                Image(pullRefreshImage1).width(100.percent).height(
                    150.0 + (binaryOperation(model.getOffset() > 1.0, model.getOffset(), 1.0) - 1.0) * 150.0).objectFit(
                    ImageFit.Fill)
                Stack() {
                    Image(pullRefreshImage4).width(20).height(30).objectFit(ImageFit.Contain).position(x: 140.0,
                        y: -10.0 + (binaryOperation(model.getOffset() > 1.0, model.getOffset(), 1.0) - 1.0) * 20.0)
                    Image(pullRefreshImage5).width(20).height(40).objectFit(ImageFit.Contain).position(x: 165.0,
                        y: -25.0 + (binaryOperation(model.getOffset() > 1.0, model.getOffset(), 1.0) - 1.0) * 20.0)
                    Image(pullRefreshImage2).width(100.percent).height(100.percent).objectFit(ImageFit.Fill)
                }.width(100.percent).height(
                    120.0 + (binaryOperation(model.getOffset() > 1.0, model.getOffset(), 1.0) - 1.0) * 120.0)
                Stack() {
                    Image(pullRefreshImage7).width(10).height(50).objectFit(ImageFit.Fill).position(x: 380, y: -43)
                    Image(pullRefreshImage6).width(12).height(65).objectFit(ImageFit.Fill).position(x: 410, y: -58)
                    Image(pullRefreshImage8).width(10).height(50).objectFit(ImageFit.Fill).position(x: 440, y: -45)
                    Image(pullRefreshImage3).width(100.percent).height(100.percent).objectFit(ImageFit.Fill)
                }.width(100.percent).height(
                    80.0 + (binaryOperation(model.getOffset() > 1.0, model.getOffset(), 1.0) - 1.0) * 10.0)
                FlyRefreshHeaderAircraft(modelParam: model)
            }.width(100.percent).height(100.percent)
        }.backgroundColor(Color(0x33AAFF)).width(100.percent).height(initHeight)
    }

    func binaryOperation(condition: Bool, trueValue: Float64, falseValue: Float64): Float64 {
        if (condition) {
            return trueValue
        }
        return falseValue
    }

    func changeStatus(model: SmartRefreshForFlyRefreshModel) {
        if (model.refreshState == RefreshStateEnum.TO_REFRESH || model.refreshState == RefreshStateEnum.NONE) { //下拉中
            initHeight = binaryOperation(model.headerHeight > model.initHeaderHeight, model.headerHeight,
                model.initHeaderHeight)
        } else if (model.refreshState == RefreshStateEnum.REFRESHING) { //刷新中
            initHeight = model.initHeaderHeight
        }
    }
}