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

import UIKit

class YHHomeKingKongBlockView: UIView {
    static let viewH : CGFloat = (KScreenWidth - 20.0 * 2 - 10.0 * 3) / 4.0
    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var arrData : [YHHomeKingKongBlockModel] = [
        YHHomeKingKongBlockModel(type: "3", iconName: "kingkong_sfys", title: "身份优势"),
        YHHomeKingKongBlockModel(type: "4", iconName: "kingkong_yqyl", title: "邀请有礼"),
        YHHomeKingKongBlockModel(type: "5", iconName: "kingkong_khxs", title: "客户心声"),
        YHHomeKingKongBlockModel(type: "6", iconName: "kingkong_xgbs", title: "香港办事"),
    ]
}

private extension YHHomeKingKongBlockView {
    func initView() {
        let itemGap : CGFloat = 10.0
        var offsetX : CGFloat = 0
        for (index,item) in arrData.enumerated() {
            let view = YHHomeKingKongBlockItem()
            view.dataModel = item
            addSubview(view)
            view.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.left.equalTo(offsetX)
                make.width.height.equalTo(YHHomeKingKongBlockView.viewH)
            }
            offsetX = offsetX + YHHomeKingKongBlockView.viewH + itemGap
            view.tag = index
            let tap = UITapGestureRecognizer(target: self, action: #selector(tapView(gestureRecognizer:)))
            view.addGestureRecognizer(tap)
        }
    }
    
    
    @objc func tapView(gestureRecognizer:UITapGestureRecognizer) {
        if let tag = gestureRecognizer.view?.tag {
            switch tag {
            case 0:
                //身份优势
                let vc = YHStatusAdvantageVC()
                self.parentViewController?.navigationController?.pushViewController(vc)
            case 1:
                //邀请有礼
                tapShareForGiftView()
            case 2:
                //客户心声
                let vc = YHOtherServiceViewController()
                vc.classID = 5
                self.parentViewController?.navigationController?.pushViewController(vc)
            case 3:
                //香港办事
                let vc = YHHKEventCenterViewController()
                self.parentViewController?.navigationController?.pushViewController(vc)
            default:
                break
            }
        }
    }
    
    func tapShareForGiftView() {
        //判断是否登录成功
        if YHLoginManager.shared.isLogin() {
            let vc = YHInvitationWithGiftsViewController()
            self.parentViewController?.navigationController?.pushViewController(vc)
        } else {
            YHOneKeyLoginManager.shared.oneKeyLogin()
            YHLoginManager.shared.loginSuccessActionBlock = {[weak self] in
                guard self != nil else { return }
                let vc = YHInvitationWithGiftsViewController()
                self?.parentViewController?.navigationController?.pushViewController(vc)
                YHLoginManager.shared.loginSuccessActionBlock = nil
            }
        }
    }
}
