//
//  YHMainInformationCardTableViewCell.swift
//  galaxy
//
//  Created by EDY on 2024/1/29.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

enum YHCardType: Int {
    case identity = 0
    case passport = 1
}

class YHMainInformationCardTableViewCell: UITableViewCell {

    var centerView: UIView!
    var titleLabel: UILabel!
    var exampleButton: UIButton!
    var frontLabel: UILabel!
    var backSurfaceLabel: UILabel!
    var frontImageButton: UIButton!
    var backSurfaceImageButton: UIButton!
    var type: YHCardType? {
        didSet {
            guard let type = type else { return }
            switch type {
            case .identity:
                titleLabel.text = "中国身份证(选填)"
                frontLabel.text = "身份证人像面"
                backSurfaceLabel.text = "身份证国徽面"
            case .passport:
                titleLabel.text = "港澳通行证(选填)"
                frontLabel.text = "港澳通行证正面"
                backSurfaceLabel.text = "港澳通行证反面"
                
            }
        }
    }
    
    var dataSource: YHSectionItemModel?{
        didSet {
            updataData()
        }
    }
    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
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    func setupUI() {
        backgroundColor = UIColor(hex: 0xe9e9e9)
        centerView = {
            let view = UIView()
            view.backgroundColor = .white
            view.layer.cornerRadius = 12
            return view
        }()
        contentView.addSubview(centerView)
        centerView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(7)
            make.bottom.equalTo(-7)
        }
        
        titleLabel = {
            let label = UILabel()
            label.font = kFont(size: 17)
            label.textColor = UIColor(hex:0x222222)
            return label
        }()
        centerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.top.equalTo(16)
            make.height.equalTo(20)
        }
        
        exampleButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor =  UIColor(hex:0x1f93ff).withAlphaComponent(0.12)
            button.titleLabel?.font = kBoldFont(size: 10)
            button.contentHorizontalAlignment = .center
            button.setTitle("示例模板", for: .normal)
            button.setTitleColor( UIColor(hex:0x0085ff), for: .normal)
            button.layer.cornerRadius = 4
            button.addTarget(self, action: #selector(exampleClick), for: .touchUpInside)
            return button
        }()
        centerView.addSubview(exampleButton)
        exampleButton.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right)
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.height.equalTo(14)
            make.width.equalTo(48)
        }
        
        let lineView = {
            let view = UIView()
            view.backgroundColor = UIColor(hex: 0xf0f0f0)
            return view
        }()
        centerView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.top.equalTo(52)
            make.height.equalTo(1)
            make.right.equalTo(-18)
        }
        
        frontLabel = {
            let label = UILabel()
            label.font = kFont(size: 12)
            label.textColor = UIColor(hex:0x888f98)
            label.textAlignment = .center
            return label
        }()
        centerView.addSubview(frontLabel)
        frontLabel.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(centerView.snp.centerX)
            make.top.equalTo(64)
            make.height.equalTo(20)
        }
        
        backSurfaceLabel = {
            let label = UILabel()
            label.font = kFont(size: 12)
            label.textColor = UIColor(hex:0x888f98)
            label.textAlignment = .center
            return label
        }()
        centerView.addSubview(backSurfaceLabel)
        backSurfaceLabel.snp.makeConstraints { make in
            make.right.equalTo(-18)
            make.left.equalTo(centerView.snp.centerX)
            make.top.equalTo(64)
            make.height.equalTo(20)
        }
        
        frontImageButton = {
            let button = UIButton(type: .custom)
            button.setBackgroundImage(UIImage(named: "service_card_front"), for: .normal)
            button.addTarget(self, action: #selector(exampleClick), for: .touchUpInside)
            return button
        }()
        centerView.addSubview(frontImageButton)
        frontImageButton.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.top.equalTo(96)
            make.height.equalTo(91)
            make.width.equalTo(147)
        }
        
        backSurfaceImageButton = {
            let button = UIButton(type: .custom)
            button.setBackgroundImage(UIImage(named: "service_card_back"), for: .normal)
            button.addTarget(self, action: #selector(exampleClick), for: .touchUpInside)
            return button
        }()
        centerView.addSubview(backSurfaceImageButton)
        backSurfaceImageButton.snp.makeConstraints { make in
            make.right.equalTo(-18)
            make.top.equalTo(96)
            make.height.equalTo(91)
            make.width.equalTo(147)
        }
    }
    
    @objc func exampleClick() {
        let view = YHCardExampleView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: KScreenHeight))
        view.type = type
        let window = UIApplication.shared.yhKeyWindow()
        window?.addSubview(view)
    }
    
    func updataData() {
        titleLabel.text = "中国身份证(选填)"
        frontLabel.text = "身份证人像面"
        backSurfaceLabel.text = "身份证国徽面"
    }
}
