//
//  PlayDetailTopView.swift
//  ColorfulWallpaper
//
//  Created by nkl on 2024/9/13.
//

import Foundation
import UIKit

class PlayDetailTopView: UIView {
    lazy var backButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_dismiss"), for: .normal)
        return btn
    }()

    lazy var hStack: UIStackView = {
        let stack: UIStackView = .hStack
        stack.alignment = .center
        return stack
    }()

    lazy var timeButton: UIButton = {
        let button = UIButton()
        button.frame = CGRect(x: 0, y: 0, width: 100, height: 32)
        button.backgroundColor = .white.withAlphaComponent(0.2)
        button.customCornerRadius = 16
        button.setTitle("00:00:00", for: .normal)
        button.setTitleColor(.white.withAlphaComponent(0.5), for: .normal)
        button.setTitleColor(.white, for: .selected)
        button.titleLabel?.font = .systemFont(ofSize: 12)
        button.setImage(UIImage(named: "ic_clock_time"), for: .normal)
        button.setImage(UIImage(named: "ic_clock_time_selected"), for: .selected)
        let spacing: CGFloat = 10.0
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing / 2, bottom: 0, right: spacing / 2)
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2, bottom: 0, right: -spacing / 2)
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2, bottom: 0, right: spacing / 2)
        button.contentMode = .scaleAspectFit
        if  FitManager.isAr {
            button.semanticContentAttribute = .forceLeftToRight
        }

        return button
    }()

    lazy var moreButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "ic_nav_more"), for: .normal)
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addChildren()
        makeConstraints()
    }

    private func addChildren() {
        
        addSubview(backButton)
        addSubview(hStack)
        moreButton.isHidden = true
        hStack.addArrangedSubview(timeButton)
        hStack.addArrangedSubview(moreButton)
    }

    private func makeConstraints() {
        backButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.width.height.equalTo(44)
        }

        hStack.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
            make.height.equalToSuperview()
        }
        timeButton.snp.makeConstraints { make in
            make.width.equalTo(100)
            make.height.equalTo(32)
        }
        moreButton.snp.makeConstraints { make in
            make.width.height.equalTo(44)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
