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

import UIKit
import SwifterSwift


class YHContractMainItemStatusCell: UITableViewCell {
    static let cellReuseIdentifier = "YHContractMainItemStatusCell"
    static let cellHeight :CGFloat = 97.0
    
    var dataModel : YHContactMainItemStatusModel? {
        didSet {
            updateUI()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    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")
    }
    
    private lazy var iconImageV : UIImageView = {
        let nextImgV = UIImageView()
        nextImgV.image = UIImage(named: "service_center_sfsq_xxws_icon")
        nextImgV.contentMode = .scaleAspectFill
        return nextImgV
    }()
    
    private lazy var itemNameLable : UILabel = {
        let lable = UILabel()
        lable.textColor = UIColor(red: 0.133, green: 0.133, blue: 0.133, alpha: 1)
        lable.font = UIFont(name: "PingFangSC-Medium", size: 17)
        lable.text = "信息完善"
        return lable
    }()
    
    
    private lazy var goUnfinishHoldView : UIView = {
        let view = UIView()
        view.layer.cornerRadius = 6
        view.clipsToBounds = true
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [UIColor(red: 0.302, green: 0.624, blue: 0.973, alpha: 1),UIColor(red: 0.231, green: 0.525, blue: 0.969, alpha: 1)].map(\.cgColor)
        gradientLayer.locations = [0,1].map { NSNumber(value: $0) }
        gradientLayer.startPoint = CGPoint(x: 0.12, y: 0.25)
        gradientLayer.endPoint = CGPoint(x: 0.38, y: 0.25)
        view.layer.addSublayer(gradientLayer)
        
        let lable = UILabel()
        lable.font = kFont(size: 14)
        lable.text = "去完善"
        lable.textAlignment = .center
        lable.textColor = .white
        view.addSubview(lable)
        lable.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        return view
    }()
    
    
    
    private lazy var gofinishHoldView : UIView = {
        let view = UIView()
        return view
    }()
    
}

extension YHContractMainItemStatusCell {
    func updateUI() {
        iconImageV.image = UIImage(named: dataModel?.iconName ?? "")
        itemNameLable.text = dataModel?.itemName ?? ""
        
        gofinishHoldView.isHidden = !(dataModel?.isFinished ?? false)
        goUnfinishHoldView.isHidden = dataModel?.isFinished ?? false
    }
    
    func setupUI() {
        let holdView = UIView()
        holdView.backgroundColor = .white
        holdView.layer.cornerRadius = 12
        holdView.clipsToBounds = true
        
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.addSubview(holdView)
        holdView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        
        holdView.addSubview(iconImageV)
        iconImageV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(26)
            make.width.height.equalTo(34)
        }
        
        
        holdView.addSubview(itemNameLable)
        itemNameLable.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconImageV.snp.right).offset(14)
            make.height.equalTo(24)
            make.width.equalTo(80)
        }
        
        
        holdView.addSubview(goUnfinishHoldView)
        goUnfinishHoldView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
            make.height.equalTo(34)
            make.width.equalTo(76)
        }
        
        
        gofinishHoldView.isHidden = true
        holdView.addSubview(gofinishHoldView)
        gofinishHoldView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
            make.width.equalTo(82)
        }
        
        let nextImgV = UIImageView()
        nextImgV.image = UIImage(named: "service_center_right_next_icon")
        nextImgV.contentMode = .scaleAspectFill
        gofinishHoldView.addSubview(nextImgV)
        nextImgV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
            make.width.height.equalTo(20)
            
        }
        
        let statusLable = UILabel(text: "已完成")
        statusLable.textAlignment = .center
        statusLable.font = kFont(name: "PingFang SC", size: 14)
        statusLable.textColor = .black
        gofinishHoldView.addSubview(statusLable)
        statusLable.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(nextImgV.snp.left).offset(-2)
            make.height.equalTo(20)
            make.width.equalTo(42)
        }
        
        
        let statusIcon = UIImageView()
        statusIcon.image = UIImage(named: "service_center_finish_icon")
        statusIcon.contentMode = .scaleAspectFill
        gofinishHoldView.addSubview(statusIcon)
        statusIcon.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(statusLable.snp.left).offset(-4)
            make.width.height.equalTo(14)
        }
    }
    
}
