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

import UIKit


class YHIdentityCardCell: UITableViewCell {
    typealias InformationBlock = (_ type: YHCardType, _ image: UIImage, _ isleft: Bool) -> ()
    var informationBlock: InformationBlock?
    var centerView: UIView!
    var titleLabel: UILabel!
    var exampleButton: UIButton!
    var frontLabel: UILabel!
    var backSurfaceLabel: UILabel!
    var frontImageButton: UIButton!
    var backSurfaceImageButton: UIButton!
    var frontDeleteButton: UIButton!
    var backSurfaceDeleteButton: UIButton!
    var activityIndicator: UIActivityIndicatorView!
    var viewModel: YHFamilyMemberViewModel! {
        didSet {
            if type == .identity {
                if viewModel.mainModel.certificates.cnIdentityCard.imgFront.count != 0 {
                    self.viewModel.getPublicImageUrl(viewModel.mainModel.certificates.cnIdentityCard.imgFront) {[weak self] success, error in
                        guard let self = self else { return }
                        guard let url = success else { return }
                        self.frontImageButton.kf.setBackgroundImage(with: URL(string: success), for: .normal)
                        self.frontDeleteButton.isHidden = false
                    }
                }
                if viewModel.mainModel.certificates.cnIdentityCard.imgBack.count != 0 {
                    self.viewModel.getPublicImageUrl(viewModel.mainModel.certificates.cnIdentityCard.imgBack) {[weak self] success, error in
                        guard let self = self else { return }
                        guard let url = success else { return }
                        self.backSurfaceImageButton.kf.setBackgroundImage(with: URL(string: success), for: .normal)
                        self.backSurfaceDeleteButton.isHidden = false
                    }
                }
            } else {
                if viewModel.mainModel.certificates.hkMacaoPass.imgFront.count != 0 {
                    self.viewModel.getPublicImageUrl(viewModel.mainModel.certificates.hkMacaoPass.imgFront) {[weak self] success, error in
                        guard let self = self else { return }
                        guard let url = success else { return }
                        self.frontImageButton.kf.setBackgroundImage(with: URL(string: success), for: .normal)
                        self.frontDeleteButton.isHidden = false
                    }
                }
                if viewModel.mainModel.certificates.hkMacaoPass.imgBack.count != 0 {
                    self.viewModel.getPublicImageUrl(viewModel.mainModel.certificates.hkMacaoPass.imgBack) {[weak self] success, error in
                        guard let self = self else { return }
                        guard let url = success else { return }
                        self.backSurfaceImageButton.kf.setBackgroundImage(with: URL(string: success), for: .normal)
                        self.backSurfaceDeleteButton.isHidden = false
                    }
                }
            }
        }
    }
    var type: YHCardType = .identity {
        didSet {
            switch type {
            case .identity:
                titleLabel.text = "中国身份证(选填)"
                frontLabel.text = "身份证人像面"
                backSurfaceLabel.text = "身份证国徽面"
            case .passport:
                titleLabel.text = "港澳通行证(选填)"
                frontLabel.text = "港澳通行证正面"
                backSurfaceLabel.text = "港澳通行证反面"
                
            }
        }
    }

    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() {
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        centerView = {
            let view = UIView()
            view.backgroundColor = .white
            view.layer.cornerRadius = kCornerRadius6
            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(-16)
        }
        
        titleLabel = {
            let label = UILabel()
            label.font = UIFont.PFSC_M(ofSize: 17)
            label.textColor = UIColor.mainTextColor
            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.brandMainColor.withAlphaComponent(0.08)
            button.titleLabel?.font = UIFont.PFSC_M(ofSize: 10)
            button.contentHorizontalAlignment = .center
            button.setTitle("示例模板", for: .normal)
            button.setTitleColor( UIColor.brandMainColor, 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.separatorColor
            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 = UIFont.PFSC_R(ofSize: 12)
            label.textColor = UIColor.subTextColor
            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 = UIFont.PFSC_R(ofSize: 12)
            label.textColor = UIColor.subTextColor
            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(frontImageClick), 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)
        }
        
        frontDeleteButton = {
            let button = UIButton(type: .custom)
            button.setBackgroundImage(UIImage(named: "service_card_delete"), for: .normal)
            button.addTarget(self, action: #selector(frontDeleteClick), for: .touchUpInside)
            button.isHidden = true
            return button
        }()
        centerView.addSubview(frontDeleteButton)
        frontDeleteButton.snp.makeConstraints { make in
            make.centerX.equalTo(frontImageButton.snp.right)
            make.centerY.equalTo(frontImageButton.snp.top)
            make.height.equalTo(21)
            make.width.equalTo(21)
        }
        
        backSurfaceImageButton = {
            let button = UIButton(type: .custom)
            button.setBackgroundImage(UIImage(named: "service_card_back"), for: .normal)
            button.addTarget(self, action: #selector(backSurfaceImageClick), 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)
        }

        backSurfaceDeleteButton = {
            let button = UIButton(type: .custom)
            button.setBackgroundImage(UIImage(named: "service_card_delete"), for: .normal)
            button.addTarget(self, action: #selector(backSurfaceDeleteClick), for: .touchUpInside)
            button.isHidden = true
            return button
        }()
        centerView.addSubview(backSurfaceDeleteButton)
        backSurfaceDeleteButton.snp.makeConstraints { make in
            make.centerX.equalTo(backSurfaceImageButton.snp.right)
            make.centerY.equalTo(backSurfaceImageButton.snp.top)
            make.height.equalTo(21)
            make.width.equalTo(21)
        }
        
        activityIndicator = {
            let view = UIActivityIndicatorView()
            view.style = .medium
            return view
        }()
        centerView.addSubview(activityIndicator)
        activityIndicator.snp.makeConstraints { make in
            make.center.equalTo(backSurfaceImageButton.snp.center)
            make.height.equalTo(25)
            make.width.equalTo(25)
        }
    }
    
    @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)
    }
    
    @objc func frontImageClick() {
        if type == .identity {
            if viewModel.mainModel.certificates.cnIdentityCard.imgFront.count != 0 {
                return
            }
        } else {
            if viewModel.mainModel.certificates.hkMacaoPass.imgFront.count != 0 {
                return
            }
        }
        YHImagePickerView.show() {[weak self] image in
            guard let self = self else { return }
            self.activityIndicator.snp.remakeConstraints { make in
                make.center.equalTo(self.frontImageButton.snp.center)
                make.height.equalTo(25)
                make.width.equalTo(25)
            }
            self.activityIndicator.startAnimating()
            self.viewModel.uploadImage(image) {[weak self] success, error in
                guard let self = self else { return }
                guard let url = success else { return }
                self.viewModel.updateModel(self.type, isFront: true, url: url)
                self.frontImageButton.setBackgroundImage(image, for: .normal)
                self.frontDeleteButton.isHidden = false
                self.viewModel.getPublicImageUrl(url) {[weak self] success, error in
                    guard let self = self else { return }
                    guard let url = success else { return }
                    if self.type == .identity {
                        self.viewModel.requestCnIDCardMessage(url) {[weak self] success, error in
                            guard let self = self else { return }
                            self.activityIndicator.stopAnimating()
                            if success {
                            } else {
                                YHHUD.flash(message: error?.errorMsg ?? "")
                            }
                        }
                    } else {
                        self.viewModel.requestHkIDCardMessage(url) {[weak self] success, error in
                            guard let self = self else { return }
                            self.activityIndicator.stopAnimating()
                            if success {
                                
                            } else {
                                YHHUD.flash(message: error?.errorMsg ?? "")
                            }
                        }
                    }
                }
            }
//            if let block = self.informationBlock {
//                block(self.type, image, false)
//            }
        }
    }
    
    @objc func backSurfaceImageClick() {
        if type == .identity {
            if viewModel.mainModel.certificates.cnIdentityCard.imgBack.count != 0 {
                return
            }
        } else {
            if viewModel.mainModel.certificates.hkMacaoPass.imgBack.count != 0 {
                return
            }
        }
        YHImagePickerView.show() {[weak self] image in
            guard let self = self else { return }
            self.activityIndicator.snp.remakeConstraints { make in
                make.center.equalTo(self.backSurfaceImageButton.snp.center)
                make.height.equalTo(25)
                make.width.equalTo(25)
            }
            self.activityIndicator.startAnimating()
            self.viewModel.uploadImage(image) {[weak self] success, error in
                guard let self = self else { return }
                guard let url = success else { return }
                self.backSurfaceImageButton.setBackgroundImage(image, for: .normal)
                self.viewModel.updateModel(self.type, isFront: false, url: url)
                self.backSurfaceDeleteButton.isHidden = false
                self.viewModel.getPublicImageUrl(url) {[weak self] success, error in
                    guard let self = self else { return }
                    guard let url = success else { return }
                    if self.type == .identity {
                        self.viewModel.requestCnIDCardMessage(url) {[weak self] success, error in
                            guard let self = self else { return }
                            self.activityIndicator.stopAnimating()
                            if success {
                                
                            } else {
                                YHHUD.flash(message: error?.errorMsg ?? "")
                            }
                        }
                    } else {
                        self.viewModel.requestHkIDCardMessage(url) {[weak self] success, error in
                            guard let self = self else { return }
                            self.activityIndicator.stopAnimating()
                            if success {
                                
                            } else {
                                YHHUD.flash(message: error?.errorMsg ?? "")
                            }
                        }
                    }
                }
            }
//            if let block = self.informationBlock {
//                block(self.type, image, true)
//            }
        }
    }
    
    func updataData() {
        titleLabel.text = "中国身份证(选填)"
        frontLabel.text = "身份证人像面"
        backSurfaceLabel.text = "身份证国徽面"
    }
    
    @objc func frontDeleteClick() {
        viewModel.updateModel(self.type, isFront: true, url: "")
        frontDeleteButton.isHidden = true
        frontImageButton.setBackgroundImage(UIImage(named: "service_card_front"), for: .normal)
    }
    
    @objc func backSurfaceDeleteClick() {
        viewModel.updateModel(self.type, isFront: false, url: "")
        backSurfaceDeleteButton.isHidden = true
        backSurfaceImageButton.setBackgroundImage(UIImage(named: "service_card_back"), for: .normal)
    }
}
