//
//  YHUploadCertificateDetailVC.swift
//  galaxy
//
//  Created by davidhuangA on 2024/8/20.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import AttributedString

enum UploadCertificateDetailType : Int {
    case SmallWhiteNote
    case HongKongCard
}

class YHUploadCertificateDetailVC: YHBaseViewController {
    
    
    lazy var uploadImageVM: YHBaseViewModel = {
        let vm = YHBaseViewModel()
        return vm
    }()
    
    var detailType : UploadCertificateDetailType = .SmallWhiteNote
    lazy var topImgView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "xbt_tips")) //  343 90
        view.layer.cornerRadius = kCornerRadius6
        view.clipsToBounds = true
        return view
    }()
    
    
    lazy var whiteContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = kCornerRadius6
        view.clipsToBounds = true
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var titleTextView : UITextView  = {
        let lable0 = UITextView()
        lable0.textColor = UIColor.mainTextColor
        lable0.font = UIFont.PFSC_M(ofSize: 17)
        lable0.isScrollEnabled = false
        lable0.isEditable = false
        lable0.isSelectable = false
        return lable0
        
    }()
    
    private lazy var customLabel : UILabel = {
        let lable = UILabel(frame: CGRect(x: 0, y: 0, width: 48, height: 16))
        lable.text = "示例模板"
        lable.font = .PFSC_M(ofSize: 10)
        lable.backgroundColor = UIColor(hex:0x2F7EF6,transparency: 0.08)
        lable.textColor = .brandMainColor
        lable.layer.cornerRadius = kCornerRadius3
        lable.clipsToBounds = true
        lable.textAlignment = .center
        lable.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapExampleMudel))
        lable.addGestureRecognizer(tap)
        return lable
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .separatorColor
        return view
    }()
    
    
    private lazy var cardLabel : UILabel = {
        let lable = UILabel()
        lable.text = "小白条正面"
        lable.font = .PFSC_R(ofSize: 12)
        lable.textColor = UIColor.mainTextColor50
        lable.clipsToBounds = true
        lable.textAlignment = .center
        return lable
    }()
    
    
    private lazy var cardButton : YHCardButton = {
        let button = YHCardButton()
        button.setContent("card_upload", "点击上传")
        button.setBackgroundImage(UIImage(named: "service_cn_card_front"), for: .normal)
        button.addTarget(self, action: #selector(clickCardButton), for: .touchUpInside)
        button.layer.cornerRadius = kCornerRadius4
        button.clipsToBounds = true
        return button
    }()
    
    
    private lazy var delImageButton : UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "service_card_delete"), for: .normal)
        button.addTarget(self, action: #selector(clickDelImageBtn), for: .touchUpInside)
        button.isHidden = true
        return button
    }()
    
    private lazy var startDateItem : YHUploadCertificateDateItem = {
        let view = YHUploadCertificateDateItem()
        return view
    }()
    
    
    lazy var lineView2 : UIView = {
        let view = UIView()
        view.backgroundColor = .separatorColor
        return view
    }()
    
    private lazy var endDateItem : YHUploadCertificateDateItem = {
        let view = YHUploadCertificateDateItem()
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
}


private extension YHUploadCertificateDetailVC {
    @objc func clickDelImageBtn() {
        cardButton.setContent("card_upload", "点击上传")
        cardButton.setBackgroundImage(UIImage(named: "service_cn_card_front"), for: .normal)
        delImageButton.isHidden = true
    }
    
    @objc func tapExampleMudel() {
        if detailType == .SmallWhiteNote {
            let view = YHSmallWhiteNoteTemplateView.sheetView()
            view.show()
        } else if detailType == .HongKongCard {
            let view = YHHKCardTemplateView.sheetView()
            view.show()
        }
    }
    
    @objc func clickCardButton() {
        YHImagePickerView.show() {[weak self] image in
            guard let self = self else { return }
            self.cardButton.setContent("card_loading", "上传中")
            self.cardButton.startRotationAnimation()
            self.uploadImageVM.uploadImage(image) {[weak self] success, error in
                guard let self = self else { return }
                self.cardButton.stopRotationAnimation()
                self.cardButton.setContent("", "")
                self.delImageButton.isHidden = false
                
                guard let url = success else { return }
//                self.viewModel.updateModel(self.type, isFront: true, url: url)
                print(url)
                self.cardButton.setBackgroundImage(image, for: .normal)
                self.cardButton.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, isBack: 0) {[weak self] success, error in
//                            guard self != nil else { return }
//                        }
//                    } else {
//                        self.viewModel.requestHkIDCardMessage(url, isBack: 0) {[weak self] success, error in
//                            guard self != nil else { return }
//                        }
//                    }
//                }
            }
        }
        
    }
    
    func setupUI() {
        
        var navTitle = ""
        if detailType == .SmallWhiteNote {
            navTitle = "上传小白条"
            cardLabel.text = "小白条正面"
        } else if detailType == .HongKongCard {
            navTitle = "上传香港身份证"
            cardLabel.text = "香港居民身份证正面"
        }
        
        gk_navTitle = navTitle
        gk_navBarAlpha = 1.0
        gk_navBackgroundColor = .white
        view.backgroundColor = UIColor.contentBkgColor
        
       
        
        view.addSubview(whiteContentView)
        if detailType == .SmallWhiteNote {
            let topImgViewH : CGFloat = 90 * (KScreenWidth - 16 * 2) / 343.0
            view.addSubview(topImgView)
            topImgView.snp.makeConstraints { make in
                make.top.equalTo(k_Height_NavigationtBarAndStatuBar + kMargin)
                make.left.equalTo(kMargin)
                make.right.equalTo(-kMargin)
                make.height.equalTo(topImgViewH)
            }
            
            whiteContentView.snp.makeConstraints { make in
                make.left.equalTo(kMargin)
                make.right.equalTo(-kMargin)
                make.top.equalTo(topImgView.snp.bottom).offset(14)
                
            }
            
        } else if detailType == .HongKongCard {
            whiteContentView.snp.makeConstraints { make in
                make.top.equalTo(k_Height_NavigationtBarAndStatuBar + kMargin)
                make.left.equalTo(kMargin)
                make.right.equalTo(-kMargin)
            }
        }
        
        titleTextView.attributed.text = """
            \("*", .foreground(UIColor.failColor), .font(UIFont.PFSC_M(ofSize: 17)))\(" 张三的第一次赴港激活小白条 ", .foreground(UIColor.mainTextColor), .font(UIFont.PFSC_M(ofSize: 17)))\(.view(customLabel, .original(.center)))
            """
        
        
        
        whiteContentView.addSubview(titleTextView)
        titleTextView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-18)
            make.top.equalToSuperview().offset(kMargin)
        }
        
        
        whiteContentView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18)
            make.right.equalToSuperview().offset(-18)
            make.top.equalTo(titleTextView.snp.bottom).offset(12)
            make.height.equalTo(1)
        }
        
        whiteContentView.addSubview(cardLabel)
        cardLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18)
            make.top.equalTo(lineView.snp.bottom).offset(12)
            make.height.equalTo(20)
            make.width.equalTo(160)
        }
        
        
        if detailType == .SmallWhiteNote {
            whiteContentView.addSubview(cardButton)
            cardButton.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(18)
                make.top.equalTo(cardLabel.snp.bottom).offset(9)
                make.height.equalTo(99)
                make.width.equalTo(160)
                make.bottom.equalToSuperview().offset(-kMargin)
            }
        } else if detailType == .HongKongCard {
            whiteContentView.addSubview(cardButton)
            cardButton.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(18)
                make.top.equalTo(cardLabel.snp.bottom).offset(9)
                make.height.equalTo(99)
                make.width.equalTo(160)
            }
            
            whiteContentView.addSubview(startDateItem)
            startDateItem.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(18)
                make.right.equalToSuperview().offset(-18)
                make.top.equalTo(cardButton.snp.bottom).offset(32)
                make.height.equalTo(20)
            }
            startDateItem.update(title: "入境日期")
            
            
            
            whiteContentView.addSubview(lineView2)
            lineView2.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(18)
                make.right.equalToSuperview().offset(-18)
                make.top.equalTo(startDateItem.snp.bottom).offset(16)
                make.height.equalTo(1)
            }
            
            
            
            
            whiteContentView.addSubview(endDateItem)
            endDateItem.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(18)
                make.right.equalToSuperview().offset(-18)
                make.top.equalTo(lineView2.snp.bottom).offset(16)
                make.height.equalTo(20)
                make.bottom.equalToSuperview().offset(-kMargin)
            }
            endDateItem.update(title: "逗留截止")
            
        }
        
        
        
        whiteContentView.addSubview(delImageButton)
        delImageButton.snp.makeConstraints { make in
            make.centerX.equalTo(cardButton.snp.right)
            make.centerY.equalTo(cardButton.snp.top)
            make.height.equalTo(21)
            make.width.equalTo(21)
        }
        
    }
}
