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

import UIKit
import AttributedString
import Photos

class YHVisaPayTypeCell: UITableViewCell {
    
    static let cellReuseIdentifier = "YHVisaPayTypeCell"
    
    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 longPress: UILongPressGestureRecognizer = {
        return UILongPressGestureRecognizer(target: self, action: #selector(onLongPress(_:)))
    }()
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    
}

private extension YHVisaPayTypeCell {
    
    func saveImage(_ image: UIImage) {
        // 确保应用有权访问相册
        PHPhotoLibrary.requestAuthorization { status in
            if status == .authorized {
                // 保存图片到相册
                UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
                DispatchQueue.main.async {
                    YHHUD.flash(message: "保存成功")
                }
            } else {
                YHHUD.flash(message: "保存失败，请检查系统权限")
            }
        }
    }
    
    /// 长按
    @objc func onLongPress(_ press: UILongPressGestureRecognizer) {
        if press.state == .began {
            saveImage(UIImage(named: "visaPay_qr") ?? UIImage())
        }
    }
    
    func setupUI() {
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        contentView.addSubview(whiteContentView)
        
        whiteContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        //
        let label = UILabel()
        label.textColor = UIColor.mainTextColor
        label.font = UIFont.PFSC_M(ofSize:17)
        label.text = "港府官网缴费方式"
        
        whiteContentView.addSubview(label)
        label.snp.makeConstraints { make in
            make.top.equalTo(kMargin)
            make.left.equalTo(kMargin)
            make.height.equalTo(24)
        }
        
        let line = UIView()
        line.backgroundColor = .separatorColor
        whiteContentView.addSubview(line)
        
        line.snp.makeConstraints { make in
            make.top.equalTo(label.snp.bottom).offset(12)
            make.left.equalTo(kMargin)
            make.right.equalTo(-kMargin)
            make.height.equalTo(1)
        }
        
        
        let label1 = UILabel()
        label1.textColor = UIColor.mainTextColor
        label1.font = UIFont.PFSC_M(ofSize:14)
        label1.text = "方式一"
        
        whiteContentView.addSubview(label1)
        label1.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(kMargin)
            make.left.equalTo(kMargin)
            make.height.equalTo(20)
        }
        
        
        let label2 = UILabel()
        label2.textColor = UIColor.mainTextColor50
        label2.font = UIFont.PFSC_R(ofSize:12)
        label2.text = "点击下方链接，跳转浏览器，进入港府官网操作缴费"
        
        whiteContentView.addSubview(label2)
        label2.snp.makeConstraints { make in
            make.top.equalTo(label1.snp.bottom).offset(6)
            make.left.equalTo(kMargin)
            make.height.equalTo(17)
        }
        
        
        let holdView = UIView()
        holdView.backgroundColor = .separatorColor
        holdView.layer.cornerRadius = kCornerRadius4
        holdView.clipsToBounds = true
        whiteContentView.addSubview(holdView)
        
        holdView.snp.makeConstraints { make in
            make.top.equalTo(label2.snp.bottom).offset(6)
            make.left.equalTo(kMargin)
            make.right.equalTo(-kMargin)
            make.height.equalTo(66)
        }
        
        
        
        
        let link = UILabel()
        link.numberOfLines = 0
        link.textColor = UIColor.brandMainColor
        link.font = UIFont.PFSC_R(ofSize:12)
        let linkTxt : String = "https://www.gov.hk/tc/residents/immigration/nonpermanent/onlinepaymentforevisa.htm"
        
        let aa: ASAttributedString = .init(NSAttributedString(string: linkTxt), .font(UIFont.PFSC_R(ofSize: 12)),.foreground(UIColor.brandMainColor),.underline([.single], color: UIColor.brandMainColor),.action {
            if let url = URL(string: linkTxt) {
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                } else {
                    // 显示一个提示，告知用户无法打开链接
                    print("无法打开链接")
                }
            }
        })
        link.attributed.text = aa
        
        holdView.addSubview(link)
        link.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(kMargin)
            make.right.equalTo(-kMargin)
        }
        
        
        
        let line2 = UIView()
        line2.backgroundColor = .separatorColor
        whiteContentView.addSubview(line2)
        
        line2.snp.makeConstraints { make in
            make.top.equalTo(holdView.snp.bottom).offset(16)
            make.left.equalTo(kMargin)
            make.right.equalTo(-kMargin)
            make.height.equalTo(1)
        }
        
        let qrCode : UIImageView = UIImageView(image: UIImage(named: "visaPay_qr"))
        qrCode.contentMode = .scaleAspectFill
        qrCode.isUserInteractionEnabled = true
        qrCode.addGestureRecognizer(longPress)
        
        whiteContentView.addSubview(qrCode)
        qrCode.snp.makeConstraints { make in
            make.top.equalTo(line2.snp.bottom).offset(18)
            make.right.equalTo(-kMargin)
            make.height.width.equalTo(86)
            make.bottom.equalToSuperview().offset(-kMargin)
        }
        
        
        
        let label3 = UILabel()
        label3.textColor = UIColor.mainTextColor
        label3.font = UIFont.PFSC_M(ofSize:14)
        label3.text = "方式二"
        
        whiteContentView.addSubview(label3)
        label3.snp.makeConstraints { make in
            make.top.equalTo(qrCode.snp.top).offset(12)
            make.left.equalTo(kMargin)
            make.height.equalTo(20)
        }
        
        
        
        let label4 = UILabel()
        label4.numberOfLines = 0
        label4.lineBreakMode = .byWordWrapping
        label4.textColor = UIColor.mainTextColor50
        label4.font = UIFont.PFSC_R(ofSize:12)
        label4.text = "用其他设备直接扫码，或长按保存图片，通过微信识别打开"
        
        whiteContentView.addSubview(label4)
        label4.snp.makeConstraints { make in
            make.top.equalTo(label3.snp.bottom).offset(6)
            make.left.equalTo(kMargin)
            make.right.equalTo(qrCode.snp.left).offset(-kMargin)
        }
        
    }
}
