//
//  MusicEmptyView.swift
//  PhysicalWallPaper
//
//  Created by nkl on 2024/11/13.
//

import Foundation
import UIKit

enum MusicEmptyAlignment {
    case top
    case center
    case bottom
}

class MusicEmptyView: UIView {
    lazy var iconView: UIImageView = UIImageView(image: .icEmpty)

    lazy var titleLabel: UILabel = UILabel.simpleLabel(text: "No Music".localized(), font: .systemFont14, color: .black)

    lazy var jumpBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .hexColor("#6EF4F4")
        btn.setTitle("Go to Add".localized(), for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 16)
        btn.customCornerRadius = 25
        return btn
    }()

    lazy var containerView: UIView = .simpleView(color: .clear)

    var alignment: MusicEmptyAlignment = .top

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

    convenience init(title: String = "No Music".localized(),
                     icon: UIImage? = UIImage(named: "ic_empty"),
                     buttonTitle: String = "Go to Add".localized(),
                     algin: MusicEmptyAlignment = .top,
                     needButton: Bool = true
    ) {
        self.init(frame: .zero)
        titleLabel.text = title
        iconView.image = icon
        iconView.contentMode = .scaleAspectFill
        jumpBtn.setTitle(buttonTitle, for: .normal)
        alignment = algin
        jumpBtn.isHidden = !needButton

        backgroundColor = .clear
        addChildren()
        makeConstraints()
    }

    func addChildren() {
        addSubview(containerView)
        containerView.addSubview(iconView)
        containerView.addSubview(titleLabel)
        containerView.addSubview(jumpBtn)
    }

    func makeConstraints() {
        containerView.snp.makeConstraints { make in
            if alignment == .top {
                make.top.equalToSuperview().offset(100)
                make.centerX.equalToSuperview()
            } else if alignment == .center {
                make.center.equalToSuperview()
            } else if alignment == .bottom {
                make.bottom.equalToSuperview().offset(-100)
                make.centerX.equalToSuperview()
            }
        }

        iconView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.height.equalTo(100)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(iconView.snp.bottom).offset(20)
            make.centerX.equalTo(iconView)
        }

        jumpBtn.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalTo(250)
        }
    }

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