//
//  MembershipProductView.swift
//  SwiftProject
//
//  Created by aaa on 2024/4/1.
//

import Foundation
import UIKit
//数据源模型
//MARK: - MemberShipModelItem
class MemberShipModelItem: NSObject {
    var title:String?
    var desc:String?
    var rightDesc:String?
    var isSelected:Bool = false
    var imageName_selected:String?
    var imageName_unSelected:String?
    var productIdentifier:String?//产品id
    init(title: String? = nil, desc: String? = nil, rightDesc: String? = nil, isSelected: Bool, imageName_selected: String? = nil, imageName_unSelected: String? = nil, productIdentifier: String? = nil) {
        self.title = title
        self.desc = desc
        self.rightDesc = rightDesc
        self.isSelected = isSelected
        self.imageName_selected = imageName_selected
        self.imageName_unSelected = imageName_unSelected
        self.productIdentifier = productIdentifier
    }
    
    //目前就3个
    class func getProductsIDs()->[String] {
        return ["VPC0000001","VPC0000002","VPC0000003"]
    }
    
}

//MARK: - MembershipProductView
typealias MembershipProductViewCallback = (_ item:MemberShipModelItem?)->()

class MembershipProductView: UIView ,UITableViewDataSource,UITableViewDelegate{
    var tableView:UITableView?
    var dataSource:[MemberShipModelItem] = []
    var selectedItem:MemberShipModelItem?
    let kMembershipViewCell_identifier = "kMembershipViewCell_identifier"
    var selectCallback:MembershipProductViewCallback?
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        self.addSubview(tableView!)
        tableView?.snp.makeConstraints({ make in
            make.left.top.right.bottom.equalToSuperview()
        })
        
        tableView?.delegate = self
        tableView?.dataSource = self
        tableView?.register(MembershipViewCell.classForCoder(), forCellReuseIdentifier: kMembershipViewCell_identifier)
        tableView?.separatorStyle = .none
        tableView?.isScrollEnabled = false
        tableView?.showsHorizontalScrollIndicator = false
        tableView?.backgroundColor = .clear
    }
    
    func setData(_ dataArr:[MemberShipModelItem]?,selectedItem:MemberShipModelItem?) {
        dataSource.removeAll()
        guard let ad = dataArr else {
            return
        }
        dataSource.append(contentsOf: ad)
        self.selectedItem = selectedItem
        
        tableView?.reloadData()
    }
    
    //MARK: - UITableViewDataSource,UITableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MembershipViewCell = tableView.dequeueReusableCell(withIdentifier: kMembershipViewCell_identifier, for: indexPath) as! MembershipViewCell
        let item = self.dataSource[indexPath.row]
        cell.setItemData(item)
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = self.dataSource[indexPath.row]
        if let si = selectedItem {
            si.isSelected = false
        }
        item.isSelected = true
        selectedItem = item
        tableView.reloadData()
        guard let callback = self.selectCallback else {
            return
        }
        callback(item)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
}

//MARK: - MembershipViewCell
class MembershipViewCell: UITableViewCell {
    var item:MemberShipModelItem?
    var bgView:UIView?
    var rightDescBtn:UIButton?
    var leftTitleLabel:UILabel?
    var leftDescLabel:UILabel?
    var bgImgView:UIImageView?
    var temMaskView:UIView?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        bgView = UIView()
        self.contentView.addSubview(bgView!)
        bgView?.layer.cornerRadius = 12
        bgView?.layer.masksToBounds = true
        
        bgView?.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(4)
            make.top.equalToSuperview().offset(7)
            make.bottom.equalToSuperview().offset(-7)
            make.right.equalToSuperview().offset(-4)
        }
        
        bgImgView = UIImageView()
        bgView?.addSubview(bgImgView!)
        bgImgView?.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        bgView?.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        self.backgroundColor = .clear
//
//        leftTitleLabel = UILabel()//价格label
//        bgView?.addSubview(leftTitleLabel!)
//        leftTitleLabel?.textColor = .black
//        leftTitleLabel?.font = UIFont.systemFont(ofSize: 16)
//        leftTitleLabel?.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(6)
//            make.top.equalToSuperview().offset(6)
//        }
//        
//        leftDescLabel = UILabel()
//        bgView?.addSubview(leftDescLabel!)
//        leftDescLabel?.textColor = .gray
//        leftDescLabel?.font = UIFont.systemFont(ofSize: 12)
//        leftDescLabel?.snp.makeConstraints {[weak self] make in
//            make.left.equalToSuperview().offset(6)
//            make.top.equalTo(self!.leftTitleLabel!.snp.bottom).offset(6)
//        }
//        
//        rightDescBtn = UIButton()
//        rightDescBtn?.isUserInteractionEnabled = false
//        self.contentView.addSubview(rightDescBtn!)
//        rightDescBtn?.layer.cornerRadius = 8
//        rightDescBtn?.layer.masksToBounds = true
//        
//        rightDescBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 12)
//        rightDescBtn?.setTitleColor(.black, for: .normal)
//        rightDescBtn?.snp.makeConstraints { make in
//            make.right.equalToSuperview().offset(0)
//            make.top.equalToSuperview().offset(0)
//            make.height.equalTo(30)
//            make.width.equalTo(120)
//        }
        
        temMaskView = UIView()
        temMaskView?.backgroundColor = UIColor(r: 19, g: 20, b: 20, a: 1)
        bgView?.addSubview(temMaskView!)
        temMaskView!.snp.makeConstraints { make in
            make.width.equalTo(120)
            make.height.equalTo(20)
            make.bottom.equalToSuperview().offset(-8)
            make.left.equalToSuperview().offset(100)
        }
    }
    
    func setItemData(_ item:MemberShipModelItem?) {
        
        self.item = item
        self.item?.addObserver(self, forKeyPath: "isSelected", context: nil)
//        rightDescBtn?.setTitle(item?.rightDesc, for: .normal)
//        leftTitleLabel?.text = item?.title
//        leftDescLabel?.text = item?.desc
        updateCellUI()
        
    }
    
    func updateCellUI(){
        guard let it = item else {
            return
        }
        if it.isSelected == true{
//            bgView?.backgroundColor = UIColor(r: 20, g: 20, b: 20, a: 1)
//            bgView?.layer.borderColor = UIColor.black.cgColor
            bgImgView?.image = UIImage(named: it.imageName_selected!)
        }
        else{
//            bgView?.backgroundColor = .lightGray
//            bgView?.layer.borderColor = UIColor.gray.cgColor
            bgImgView?.image = UIImage(named: it.imageName_unSelected!)
        }
    }
    
    
    
   
    
    deinit {
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
