//
//  SearchResultStateView.swift
//  ColorfulWallpaper
//
//  Created by nkl on 2024/9/11.
//

import Foundation
import UIKit

protocol PageStatePlaceHolder {
    var placeHolderImageName: String? { get }
    var placeHolderMessage: String? { get }
}

enum PagePlaceholderType: PageStatePlaceHolder {
    case empty
    case nonet
    case noMusic
    case noDownload
    
    var placeHolderImageName: String? {
        switch self {
        case .noMusic, .noDownload:
            return "ic_empty"
        case .nonet:
            return "ic_empty"
        default:
            return nil
        }
    }
    
    var placeHolderMessage: String? {
        switch self {
        case .noMusic, .noDownload:
            return "No Music".localized()
        case .nonet:
            return "Network error, please check.".localized()
        default:
            return "No Data".localized()
        }
    }
}


class SearchResultStateView: UIView {
    var state: PagePlaceholderType = .empty {
        didSet {
            iconView.image = UIImage(named: state.placeHolderImageName ?? "")
            titleLabel.text = state.placeHolderMessage
            reloadButton.isHidden = (state != .nonet)
        }
    }

    lazy var iconView: UIImageView = .init(image: UIImage(named: "ic_empty"))
    lazy var titleLabel: UILabel = {
        let lab = UILabel()
        lab.font = .systemFont(ofSize: 14)
        lab.textColor = .white
        return lab
    }()

    lazy var reloadButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Reload".localized(), for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14)
        btn.customCornerRadius = 20
        btn.backgroundColor = .hexColor("#6EF4F4")
        return btn
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .hexColor("#222222")
        addChildren()
        makeConstraints()
    }

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

    func addChildren() {
        addSubview(iconView)
        addSubview(titleLabel)
        addSubview(reloadButton)
    }

    func makeConstraints() {
        iconView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(iconView.snp.bottom).offset(12)
            make.centerX.equalToSuperview()
        }
        reloadButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.width.equalTo(80)
            make.height.equalTo(40)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
}
