//
//  LNStarScoreView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/14.
//

import Foundation
import UIKit
import SnapKit


enum LNStarType {
    case normal
    case whiteBorder
    
    var emptyIc: UIImage {
        switch self {
        case .normal: .icStar
        case .whiteBorder: .icStarWhiteBorder
        }
    }
    
    var fillIc: UIImage {
        switch self {
        case .normal: .icStarFill
        case .whiteBorder: .icStarWhiteBorderFill
        }
    }
}


class LNStarScoreView: UIView {
    private let grayIc = UIImageView()
    private let fillIc = UIImageView()
    private let fillView = UIView()
    var curType: LNStarType = .normal {
        didSet {
            grayIc.image = curType.emptyIc
            fillIc.image = curType.fillIc
        }
    }
    
    var score: Double = 0.0 {
        didSet {
            guard oldValue != score else { return }
            let fixed = score.bounded(min: 0.0, max: 1.0)
            fillView.snp.remakeConstraints { make in
                make.leading.top.bottom.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(fixed)
            }
        }
    }
    var icSize: CGFloat? {
        didSet {
            if let icSize {
                grayIc.snp.updateConstraints { make in
                    make.width.height.equalTo(icSize)
                }
                fillIc.snp.updateConstraints { make in
                    make.width.height.equalTo(icSize)
                }
            } else {
                grayIc.snp.updateConstraints { make in
                    make.width.equalTo(grayIc.image!.size.width)
                    make.height.equalTo(grayIc.image!.size.height)
                }
                fillIc.snp.updateConstraints { make in
                    make.width.equalTo(fillIc.image!.size.width)
                    make.height.equalTo(fillIc.image!.size.height)
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNStarScoreView {
    private func setupViews() {
        grayIc.image = curType.emptyIc
        addSubview(grayIc)
        grayIc.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(grayIc.image!.size.width)
            make.height.equalTo(grayIc.image!.size.height)
        }
        
        fillView.clipsToBounds = true
        addSubview(fillView)
        fillView.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(score)
        }
        
        fillIc.image = curType.fillIc
        fillView.addSubview(fillIc)
        fillIc.snp.makeConstraints { make in
            make.leading.centerY.equalToSuperview()
            make.width.equalTo(fillIc.image!.size.width)
            make.height.equalTo(fillIc.image!.size.height)
        }
    }
}


#if DEBUG

import SwiftUI

struct LNStarScoreViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNStarScoreView()
        view.icSize = 60
        view.curType = .whiteBorder
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        view.score = 0.8
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNStarScoreViewPreview()
})
#endif

