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

import UIKit

class YHPersonInfoCommonCell: UITableViewCell {
    
    static let cellReuseIdentifier = "YHPersonInfoCommonCell"
    
    var model : YHPersonInfoCellModel? {
        didSet{
            updateUI()
        }
    }
    
    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
    }
    
    
    private lazy var holdView : UIView = {
        let contryInfoView = YHPersonInfoSessionHoldView()
        return contryInfoView
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if holdView.superview != nil {
            holdView.snp.makeConstraints { make in
                make.top.equalTo(14)
                make.left.bottom.equalToSuperview()
                make.right.equalToSuperview().offset(-16)
            }
        }
    }
    
}

extension YHPersonInfoCommonCell {
    func updateUI() {
        for item in holdView.subviews {
            item.removeFromSuperview()
        }
        holdView.removeFromSuperview()
        
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.addSubview(holdView)
        holdView.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.left.bottom.equalToSuperview()
            make.width.equalTo(contentView.snp.width)
        }
        
        if model?.cellType == 0 {
            guard let model = model else { return }
            guard let arrData = model.cellData0,arrData.count > 0 else { return }
            
            
            let sessionView = YHPersonInfoSessionView()
            sessionView.updateTitle(title: model.cellTitle)
            holdView.addSubview(sessionView)
            sessionView.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.right.equalToSuperview().offset(-18)
                make.left.equalToSuperview().offset(18)
            }
            
            if arrData.count == 1 {
                let item = arrData[0]
                let cellView = YHPersonInfoCellView()
                cellView.update(title: item.name, subtitle: item.description)
                holdView.addSubview(cellView)
                cellView.snp.makeConstraints { make in
                    make.top.equalTo(sessionView.snp.bottom)
                    make.right.equalToSuperview().offset(-18)
                    make.left.equalToSuperview().offset(18)
                    make.bottom.equalToSuperview().offset(-21)
                }
            } else {
                var tmp : UIView = sessionView
                for(index,item) in arrData.enumerated() {
                    let cellView = YHPersonInfoCellView()
                    cellView.update(title: item.name, subtitle: item.description)
                    holdView.addSubview(cellView)
                    
                    if index == arrData.count - 1 {
                        cellView.snp.makeConstraints { make in
                            make.top.equalTo(tmp.snp.bottom)
                            make.right.equalToSuperview().offset(-18)
                            make.left.equalToSuperview().offset(18)
                            make.bottom.equalToSuperview().offset(-21)
                        }
                        
                    } else {
                        cellView.snp.makeConstraints { make in
                            make.top.equalTo(tmp.snp.bottom)
                            make.right.equalToSuperview().offset(-18)
                            make.left.equalToSuperview().offset(18)
                        }
                    }
                    tmp = cellView
                }
            }
        } 
        else if model?.cellType == 1 {
            
            guard let model = model else { return }
            guard let arrData = model.cellData1,arrData.count > 0 else { return }
            
            
            let sessionView = YHPersonInfoSessionView()
            sessionView.updateTitle(title: model.cellTitle)
            holdView.addSubview(sessionView)
            sessionView.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.right.equalToSuperview().offset(-18)
                make.left.equalToSuperview().offset(18)
            }
            
            var tmp : UIView = sessionView
            for (index0,item0) in arrData.enumerated() {
                let lable = UILabel()
                lable.text = item0.cellTitle
                lable.textColor = UIColor(hex: 0x222222)
                lable.font = UIFont.PFSC_M(ofSize: 14)
                holdView.addSubview(lable)
                lable.snp.makeConstraints { make in
                    make.top.equalTo(tmp.snp.bottom).offset(18)
                    make.left.equalTo(18)
                    make.right.equalTo(-18)
                    make.height.equalTo(17)
                }
                
                tmp = lable
                
                
                let arrData0 = item0.cellData
                if arrData0.count > 0 {
                    for(index,item) in arrData0.enumerated() {
                        let cellView = YHPersonInfoCellView()
                        cellView.update(title: item.name, subtitle: item.description)
                        holdView.addSubview(cellView)
                        
                        if index == 0 {
                            //第一个
                            cellView.snp.makeConstraints { make in
                                make.top.equalTo(tmp.snp.bottom).offset(10) //for test hjl
                                make.right.equalToSuperview().offset(-18)
                                make.left.equalToSuperview().offset(18)
                            }
                            
                        } else if index == arrData0.count - 1 {
                            //最后一个
                            
                            if index0 == arrData.count - 1 {
                                //最后一个cell
                                cellView.snp.makeConstraints { make in
                                    make.top.equalTo(tmp.snp.bottom)
                                    make.right.equalToSuperview().offset(-18)
                                    make.left.equalToSuperview().offset(18)
                                    make.bottom.equalToSuperview().offset(-21)
                                }
                            } else {
                                cellView.snp.makeConstraints { make in
                                    make.top.equalTo(tmp.snp.bottom)
                                    make.right.equalToSuperview().offset(-18)
                                    make.left.equalToSuperview().offset(18)
                                }
                                
                                
                                let bottomLine = UIView()
                                bottomLine.backgroundColor = UIColor(hex: 0xF0F0F0)
                                holdView.addSubview(bottomLine)
                                bottomLine.snp.makeConstraints { make in
                                    make.top.equalTo(cellView.snp.bottom).offset(28)
                                    make.right.equalToSuperview().offset(-18)
                                    make.left.equalToSuperview().offset(18)
                                    make.height.equalTo(0.5)
                                }
                                
                                tmp = bottomLine
                                continue
                            }
                            
                        } else {
                            //中间
                            cellView.snp.makeConstraints { make in
                                make.top.equalTo(tmp.snp.bottom)
                                make.right.equalToSuperview().offset(-18)
                                make.left.equalToSuperview().offset(18)
                            }
                        }
                        tmp = cellView
                    }
                }
            }
        }
        else if model?.cellType == 2 {
            
            guard let model = model else { return }
            guard let arrData = model.cellData2,arrData.count > 0 else { return }
            
            let sessionView = YHPersonInfoSessionView()
            sessionView.updateTitle(title: model.cellTitle)
            holdView.addSubview(sessionView)
            sessionView.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.right.equalToSuperview().offset(-18)
                make.left.equalToSuperview().offset(18)
            }
            
            var tmp : UIView = sessionView
            for (index0,item0) in arrData.enumerated() {
                let view = YHPersonInfoIdentificationCardView()
                view.dataModel = item0
                holdView.addSubview(view)
                if index0 == arrData.count - 1 {
                    view.snp.makeConstraints { make in
                        make.top.equalTo(tmp.snp.bottom)
                        make.right.equalToSuperview()
                        make.left.equalToSuperview()
                        
                        make.bottom.equalToSuperview()
                        
                        view.bottomLine.isHidden = true
                    }
                } else {
                    view.snp.makeConstraints { make in
                        make.top.equalTo(tmp.snp.bottom)
                        make.right.equalToSuperview()
                        make.left.equalToSuperview()
                    }
                    view.bottomLine.isHidden = false
                }
                tmp = view
            }
        }
    }
}
