//
//  YHUserInfoHeaderView.swift
//  galaxy
//
//  Created by edy on 2024/2/6.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHUserInfoHeaderView: UIView {
    
    var settingBlock:(()->Void)?
    var evaluateBlock:(()->Void)?
    var avatarClickBlock:(()->Void)?
    var nickNameClickBlock:(()->Void)?

    var nickName:String? {
        didSet {
            userNameLabel.text = nickName
        }
    }

    private lazy var avarImgView:UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "mine_head_default"))
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(didAvarImgViewTapped))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
    private lazy var bgImgView:UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "mine_header_bg"))
        return imgView
    }()
    
    private lazy var userNameLabel:UILabel = {
        let label = UILabel()
        label.text = "登录/注册".local
        label.textColor = UIColor(hexString:"##242424")
        label.textAlignment = NSTextAlignment.left
        label.font = UIFont.PFSC_B(ofSize: 21)
        label.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(didNickNameTapped))
        label.addGestureRecognizer(tap)
        return label
    }()
    
    private lazy var infoLabel:UILabel = {
        let label = UILabel()
        label.text = "点赞 0  收藏 0 最近浏览 0".local
        label.textColor = UIColor(hexString:"#949DB5")
        label.textAlignment = NSTextAlignment.left
        label.font = UIFont.PFSC_R(ofSize: 13)
        return label
    }()
    
    private lazy var evaluateView:UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var evaluateLabel:UILabel = {
        let label = UILabel()
        label.text = "香港身份20秒极速测评".local
        label.textColor = UIColor(hexString:"#242424")
        label.textAlignment = NSTextAlignment.left
        label.font = UIFont.PFSC_M(ofSize: 17)
        return label
    }()
    
    private lazy var evaluateBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("免费测评", for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_M(ofSize: 12)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = UIColor(hexString:"#242424")
        btn.layer.cornerRadius = 6.0
        btn.clipsToBounds = true
        btn.addTarget(self, action: #selector(didClickEvaluateBtn(btn:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var evaluateBgImgView:UIImageView = {
        // radio:750:623
        let img = UIImageView(image: UIImage(named: "mine_evaluate_bg"))
        return img
    }()
    
    private lazy var settingBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "mine_btn_setting"), for: .normal)
        btn.addTarget(self, action: #selector(didClickSettingBtn(btn:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var settingView:UIView = {
        let view = UIView()
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    static func createView() -> YHUserInfoHeaderView {
       let view = YHUserInfoHeaderView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height:310+k_Height_StatusBar))
       return view
    }
    
    private func createUI() {
            

        self.addSubview(bgImgView)
        self.addSubview(settingView)
        settingView.addSubview(settingBtn)
        
        self.addSubview(avarImgView)
        self.addSubview(userNameLabel)
        self.addSubview(infoLabel)
        
        self.addSubview(evaluateView)
        evaluateView.addSubview(evaluateBgImgView)
        evaluateView.addSubview(evaluateLabel)
        evaluateView.addSubview(evaluateBtn)
                
        bgImgView.snp.makeConstraints { make in
            make.left.right.equalTo(self)
            let topMargin = (k_Height_StatusBar > 20.0 ? 0 : -24)
            make.top.equalToSuperview().offset(topMargin)
            make.height.equalTo(312.0)
        }
        
        settingView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(k_Height_statusBar())
            make.height.equalTo(44.0)
        }
        
        settingBtn.snp.makeConstraints { make in
            make.width.height.equalTo(21.0)
            make.right.equalToSuperview().offset(-21)
            make.centerY.equalToSuperview()
        }
        
        avarImgView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 72, height: 72))
            make.top.equalTo(settingBtn.snp.bottom).offset(12)
            make.left.equalToSuperview().offset(30)
        }
        
        userNameLabel.snp.makeConstraints { make in
            make.left.equalTo(avarImgView)
            make.top.equalTo(avarImgView.snp.bottom).offset(16)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(25.0)
        }
        
        infoLabel.snp.makeConstraints { make in
            make.left.equalTo(avarImgView)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(15.0)
            make.top.equalTo(userNameLabel.snp.bottom).offset(10)
        }
        
        evaluateView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-7)
            make.height.equalTo(98)
            make.top.equalTo(infoLabel.snp.bottom).offset(17.0)
        }
        
        evaluateBgImgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        evaluateLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(32)
            make.top.equalToSuperview().offset(33)
            make.height.equalTo(20.0)
        }
        
        evaluateBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-35)
            make.centerY.equalTo(evaluateLabel)
            make.size.equalTo(CGSize(width: 72, height: 32))
        }
    }
    
    @objc func didClickEvaluateBtn(btn: UIButton) {
        
        if let evaluateBlock = evaluateBlock {
            evaluateBlock()
        }
    }
    
    @objc func didClickSettingBtn(btn: UIButton) {
        
        if let settingBlock = settingBlock {
            settingBlock()
        }
    }
    
    @objc func didAvarImgViewTapped() {
        if let avatarClickBlock = avatarClickBlock {
            avatarClickBlock()
        }
    }
    
    @objc func didNickNameTapped() {
        if let nickNameClickBlock = nickNameClickBlock {
            nickNameClickBlock()
        }
    }
}
