//
//  YHScoreItemCell.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/28.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import AttributedString

class YHScoreItemCell: UITableViewCell {
    static let cellReuseIdentifier = "YHScoreItemCell"
    
//    var dataModel : YHContactItemModel? {
//        didSet {
////            updateUI()
//        }
//    }
    var optionData : YHScoreSessionModel? {
        didSet {
            updateUI()
        }
    }
    
    private lazy var holdView : UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 12
        view.clipsToBounds = true
        return view
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    var titleView : YHScoreTitleView!
    private var scoreOptionsView1 : YHScoreOptionsView!
    private var scoreOptionsView2 : YHScoreOptionsView!
    private var scoreOptionsView3 : YHScoreOptionsView!
    private var scoreOptionsView4 : YHScoreOptionsView!
    
}

extension YHScoreItemCell {
    func setupUI() {
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        
        contentView.addSubview(holdView)
        holdView.snp.makeConstraints { make in
            make.top.right.left.equalToSuperview()
            make.bottom.equalToSuperview().offset(-14)
        }
        
        //
        let view1 = UIView()
        holdView.addSubview(view1)
        view1.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
        }
        
        titleView = YHScoreTitleView()
        holdView.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.height.equalTo(YHScoreTitleView.viewH)
        }
        
        
        let resultView = YHScoreResultView()
        holdView.addSubview(resultView)
        resultView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.left.equalTo(titleView.snp.left)
            make.right.equalTo(titleView.snp.right)
            make.height.equalTo(YHScoreTitleView.viewH)
        }
        
        scoreOptionsView1 = YHScoreOptionsView()
        holdView.addSubview(scoreOptionsView1)
        scoreOptionsView1.snp.makeConstraints { make in
            make.top.equalTo(resultView.snp.bottom)
            make.left.equalTo(titleView.snp.left)
            make.right.equalTo(titleView.snp.right)
        }
        
        scoreOptionsView2 = YHScoreOptionsView()
        holdView.addSubview(scoreOptionsView2)
        scoreOptionsView2.snp.makeConstraints { make in
            make.top.equalTo(scoreOptionsView1.snp.bottom)
            make.left.equalTo(titleView.snp.left)
            make.right.equalTo(titleView.snp.right)
        }
        
        scoreOptionsView3 = YHScoreOptionsView()
        holdView.addSubview(scoreOptionsView3)
        scoreOptionsView3.snp.makeConstraints { make in
            make.top.equalTo(scoreOptionsView2.snp.bottom)
            make.left.equalTo(titleView.snp.left)
            make.right.equalTo(titleView.snp.right)
        }
        
        scoreOptionsView4 = YHScoreOptionsView()
        holdView.addSubview(scoreOptionsView4)
        scoreOptionsView4.snp.makeConstraints { make in
            make.top.equalTo(scoreOptionsView3.snp.bottom)
            make.left.equalTo(titleView.snp.left)
            make.right.equalTo(titleView.snp.right)
            
            make.bottom.equalToSuperview()
        }
    }
    
    
    
    func updateUI() {
        guard let arrData = optionData?.options else {
            return
        }
        
        titleView.updateWith(optionData?.name, optionData?.score)
        
        for (index,item) in arrData.enumerated() {
            if index == 0 {
                scoreOptionsView1.dataModel = item
            } else if index == 1 {
                scoreOptionsView2.dataModel = item
            } else if index == 2 {
                scoreOptionsView3.dataModel = item
            } else if index == 3 {
                scoreOptionsView4.dataModel = item
            }
        }
    }
}
