//
//  YHSelectApplicantGroupCell.swift
//  galaxy
//
//  Created by edy on 2024/8/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import AttributedString

class YHSelectApplicantInfo {
    var name: String = ""
    var isSelect: Bool = false
    
    init(name: String, isSelect: Bool) {
        self.name = name
        self.isSelect = isSelect
    }
}

class YHSelectApplicantGroupCell: UITableViewCell {

    static let cellReuseIdentifier = "YHSelectApplicantGroupCell"

    lazy var selecters:[YHSelectApplicantInfo] = {
        return [YHSelectApplicantInfo(name: "李小龙", isSelect: true),
                YHSelectApplicantInfo(name: "成龙", isSelect: false),
                YHSelectApplicantInfo(name: "甄子丹", isSelect: true),
                YHSelectApplicantInfo(name: "吉田双阴", isSelect: false),]
    }()
    
    lazy var whiteContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = kCornerRadius6
        return view
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .separatorColor
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        let a: ASAttributedString = .init("*", .font(UIFont.PFSC_M(ofSize: 17)),.foreground(UIColor.failColor))
        let b: ASAttributedString = .init("请您确认第一批赴港信息", .font(UIFont.PFSC_M(ofSize: 17)),.foreground(UIColor.mainTextColor))
        label.attributed.text = a+b
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.textColor = .mainTextColor
        label.text = "选择第一批赴港激活签证的申请人"
        return label
    }()
    
    lazy var selectGroupView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(hex: 0xF8F9FB)
        view.layer.cornerRadius = kCornerRadius3
        return view
    }()
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 16
        
        let collectView = UICollectionView(frame:.zero, collectionViewLayout: layout)
        collectView.delegate = self
        collectView.dataSource = self
        collectView.backgroundColor = .clear
        // 注册自定义单元格
        collectView.register(YHSelectApplicantCell.self, forCellWithReuseIdentifier: YHSelectApplicantCell.cellReuseIdentifier)
        return collectView
    }()
    
    lazy var optionsView: YHInfoItemOptionView = {
        let view = YHInfoItemOptionView(frame: .zero)
        view.titleGap = 22.0
        view.title = "赴港办证时间"
        view.setOptions(["具体日期", "具体时段"])
        view.selectIndex = 0
        view.selectBlock = {
            index in
        }
        return view
    }()
    
    lazy var timeSelectView: YHInfoItemSelectSheetView = {
        let view = YHInfoItemSelectSheetView(frame: .zero)
        view.isShowDetailOneline = true
        view.title = "具体日期"
        view.detail = "2024-03-14 ～ 2024-03-14 "
        return view
    }()
    
    lazy var locationSelectView: YHInfoItemSelectSheetView = {
        let view = YHInfoItemSelectSheetView(frame: .zero)
        view.title = "港府办证地点"
        view.detail = "九龙办事处(长沙湾) "
        return view
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        contentView.addSubview(whiteContentView)
        whiteContentView.addSubview(titleLabel)
        whiteContentView.addSubview(lineView)
        whiteContentView.addSubview(detailLabel)
        whiteContentView.addSubview(selectGroupView)
        selectGroupView.addSubview(collectionView)
        whiteContentView.addSubview(optionsView)
        whiteContentView.addSubview(timeSelectView)
        whiteContentView.addSubview(locationSelectView)

        whiteContentView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(0)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.left.equalTo(18)
            make.right.equalTo(-18)
        }
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(16)
            make.left.equalTo(18)
            make.right.equalTo(-18)
        }
        
        selectGroupView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(detailLabel.snp.bottom).offset(12)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(40+16)
            make.top.equalTo(14)
            make.bottom.equalTo(-14)
        }
        
        optionsView.snp.makeConstraints { make in
            make.top.equalTo(selectGroupView.snp.bottom).offset(16)
            make.left.equalTo(18)
            make.right.equalTo(0)
        }
        
        timeSelectView.snp.makeConstraints { make in
            make.top.equalTo(optionsView.snp.bottom).offset(0)
            make.left.equalTo(18)
            make.right.equalTo(-18)
        }
        
        locationSelectView.snp.makeConstraints { make in
            make.top.equalTo(timeSelectView.snp.bottom).offset(0)
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.bottom.equalToSuperview()
        }
    }

}

extension YHSelectApplicantGroupCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout  {
    
        // 返回单元格数量
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            
            return selecters.count
        }
        
        // 返回每个单元格的大小
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            
            return CGSize(width: floorl(collectionView.width/3.0), height: 20)
        }
        
        // 返回自定义单元格
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: YHSelectApplicantCell.cellReuseIdentifier, for: indexPath) as! YHSelectApplicantCell
            if 0 <= indexPath.item && indexPath.item < selecters.count {
                let applicant = selecters[indexPath.item]
                cell.updateApplicantInfo(applicant)
            }
            return cell
        }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if 0 <= indexPath.item && indexPath.item < selecters.count {
            let text = selecters[indexPath.item]
        }
    }
}
