//
//  CLRoleCollectionCell.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/10/15.
//

import UIKit

class CLRoleCollectionCell: UICollectionViewCell {
    
    let cellWidth = (UIScreen.width - 30.0) / 2.0
    
    lazy var bookBgImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "role_book"))
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    lazy var fromImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "role_from"))
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    lazy var coverImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.backgroundColor = .red
        imgView.cornerRadius = 25.0
        return imgView
    }()
    
    lazy var topShadowImgView: UIImageView = {
        let image = UIImage(named: "role_top_bg")
        let stretchedImage = image?.stretchableImage(horizontalRatio: 0.5, verticalRatio: 0.5)
        let imgView = UIImageView(image: stretchedImage)
        return imgView
    }()
    
    lazy var bottomShadowImgView: UIImageView = {
        let image = UIImage(named: "role_bottom_bg")
        let stretchedImage = image?.stretchableImage(horizontalRatio: 0.5, verticalRatio: 0.5)
        let imgView = UIImageView(image: stretchedImage)
        return imgView
    }()
    
    lazy var starImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "role_star"))
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    lazy var sourceLab: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.boldSystemFont(ofSize: 15)
        lab.text = "9.5"
        lab.textColor = UIColor(hex: "#FFE100")
        return lab
    }()
    
    lazy var nameLab: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.boldSystemFont(ofSize: 14)
        lab.text = "Character · 18"
        lab.textColor = UIColor(hex: "#FFFFFF")
        return lab
    }()
    
    lazy var tagLab: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.boldSystemFont(ofSize: 10)
        lab.text = "# Sining ng Pagpapatalim / #xianxia / #swordsmanship"
        lab.textColor = .white
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var descLab: UILabel = {
        let lab = UILabel()
        lab.numberOfLines = 3
        lab.font = UIFont.systemFont(ofSize: 12)
        lab.text = "Once a prodigy, Lin Feng had his cultivation shattered and was cast out Once a prodigy, Lin Feng had his cultivation shattered and was cast out"
        lab.textColor = UIColor(hex: "#666666")
        return lab
    }()
    
    lazy var remindLab: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.boldSystemFont(ofSize: 10)
        lab.text = "[The Lsat Oracle of Kael]"
        lab.textColor = UIColor(hex: "#999999")
        return lab
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.clipsToBounds = true
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: data
    func setupData(desc: String) {
        descLab.text = desc
    }
    
    // MARK: subviews
    private func setupViews() {
        contentView.addSubview(coverImgView)

        contentView.addSubview(topShadowImgView)
        contentView.addSubview(bottomShadowImgView)
        
        contentView.addSubview(bookBgImgView)
        contentView.addSubview(fromImgView)
        
        contentView.addSubview(starImgView)
        contentView.addSubview(sourceLab)
        
        contentView.addSubview(nameLab)
        contentView.addSubview(tagLab)
        
        contentView.addSubview(descLab)
        contentView.addSubview(remindLab)
        
        coverImgView.snp.makeConstraints { make in
            make.width.equalTo(cellWidth).priority(999)
            make.height.equalTo(coverImgView.snp.width).multipliedBy(4.0 / 3.0)
            make.top.left.right.equalToSuperview()
        }
        
        topShadowImgView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(sourceLab.snp.bottom).offset(18)
        }
        
        bottomShadowImgView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(coverImgView.snp.bottom)
            make.top.equalTo(nameLab.snp.top).offset(-25)
        }
        
        bookBgImgView.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
        }
        
        fromImgView.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
        }
        
        sourceLab.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(12)
            make.right.equalToSuperview().inset(18)
        }
        
        starImgView.snp.makeConstraints { make in
            make.centerY.equalTo(sourceLab.snp.centerY)
            make.right.equalTo(sourceLab.snp.left).offset(-5)
        }
        
        tagLab.snp.makeConstraints { make in
            make.right.left.equalToSuperview().inset(10)
            make.bottom.equalTo(coverImgView.snp.bottom).offset(-10)
        }
        
        nameLab.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.bottom.equalTo(tagLab.snp.top).offset(-10)
        }
        
        descLab.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.top.equalTo(coverImgView.snp.bottom).offset(10)
        }
        
        remindLab.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.top.equalTo(descLab.snp.bottom).offset(5)
        }
    }
}
