//
//  TSTimerView.swift
//  Girly
//
//  Created by 100Years on 2025/1/10.
//

class TSTimerView: TSBaseView {
    
    // 回调选中的时间
    var onTimeSelected: ((Int, Int) -> Void)?
    
    private var hour = 0
    private var minute = 0
    
    lazy var bottomView: UIView = {
        let bottomView = UIView()
        bottomView.backgroundColor = .cardBgColor
        return bottomView
    }()
    
    lazy var pickerVC: TimePickerViewController = {
        let pickerVC = TimePickerViewController()
        pickerVC.onTimeSelected = { [weak self] hour,minute in
            guard let self = self else { return }
            self.hour = hour
            self.minute = minute
        }
        return pickerVC
    }()
    
    lazy var closeBtn: UIButton = {
        let closeBtn = UIButton.createButton(image: UIImage(named: "navi_back_white")) { [weak self]  in
            guard let self = self else { return }
            onTimeSelected?(hour,minute)
            self.isHidden = true
        }
        return closeBtn
    }()

    override func creatUI() {
        
        contentView.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let cell1 = creatCell(subView: closeBtn)
        bottomView.addSubview(cell1)
        cell1.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(44)
        }
        
        bottomView.addSubview(pickerVC.view)
        pickerVC.view.snp.makeConstraints { make in
            make.top.equalTo(cell1.snp.bottom)
            make.bottom.equalTo(-34)
            make.leading.trailing.equalTo(0)
        }

        kDelayMainShort {
            self.bottomView.cornersRound(radius: 20, corner: [.topLeft,.topRight])
            self.pickerVC.setDefaultTime(hour: 1, minute: 30)
        }
        
    }
    
    func creatCell(subView:UIView) -> UIView{
        let view = UIView()
        view.addSubview(subView)
        subView.snp.makeConstraints { make in
            make.leading.equalTo(6)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(44)
        }
        return view
    }
}
