//
//  YHPersonInformationViewController.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/29.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import GKNavigationBarSwift

class YHPersonInformMainViewController: YHBaseViewController {
    var canScroll : Bool = true
    
    var myCellHeight : CGFloat {
        get {
            return view.bounds.size.height - k_Height_NavigationtBarAndStatuBar
        }
    }
    
    let cellView: YHPersonInfoControllerHoldViewController = YHPersonInfoControllerHoldViewController()
    
    var homeTableView: YHGestureTableView = {
        let tableView = YHGestureTableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        return tableView
    }()
    
    
    var myHeadView : UIView = {
        let view = UIView(frame: CGRect(x: 16, y: 0, width: KScreenWidth - 16 * 2, height: 128))
        return view
    }()
    
    
    //头像
    private lazy var headImagV : UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "score_header_default"))
        imageV.contentMode = .scaleAspectFit
        return imageV
    }()
    
    //姓名
    private lazy var nameLable : UILabel = {
        let lable0 = UILabel(text: "DavidHuang")
        lable0.textColor = UIColor(hex: 0x222222)
        lable0.font = UIFont.PFSC_M(ofSize: 16)
        return lable0
    }()
    
    //电话号码
    private lazy var phoneLable : UILabel = {
        let lable0 = UILabel(text: "15818743775")
        lable0.textColor = UIColor(hex: 0x888F98)
        lable0.font = UIFont.PFSC_R(ofSize: 12)
        return lable0
    }()
    
    
    // MARK: - 生命周期方法
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(leaveTopOp), name:Notification.Name(rawValue: "leaveTop0"), object: nil)
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(beginDragging), name:Notification.Name(rawValue: "scrollViewWillBeginDragging0"), object: nil)
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(endDragging), name:Notification.Name(rawValue: "scrollViewDidEndDragging0"), object: nil)
        
        //1.UI
        initView()
        
        //2.网络请求
        requestData()
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        cellView.view.frame = CGRect(x: 0, y: 0, width: KScreenWidth, height: myCellHeight)
    }
}

// MARK: - 私有方法
extension YHPersonInformMainViewController {
    
    @objc func leaveTopOp() {
        canScroll = true
    }
    
    @objc func beginDragging() {
        homeTableView.isScrollEnabled = false
        canScroll = false
    }
    
    @objc func endDragging() {
        homeTableView.isScrollEnabled = true
        canScroll = true
    }
    
    private func requestData() {
//        self.homeReqVM.getHomeConfig { flag, error in
//            if flag == true {
//                //1. 调用model 刷新界面
//                YHHUD.flash(message: "数据已更新")
//            } else {
//                //2. 给出 错误提示
//                if let error = error {
//                    YHHUD.flash(.error(message: error.errorMsg))
//                }
//            }
//        }
    }
    
    private func initView()->Void {
        gk_navigationBar.isHidden = false
        gk_backStyle = .black
        gk_navTitle = "预览"
        gk_navBackgroundColor = .white
        gk_navTitleColor = UIColor(hex:0x0F1214)
        gk_navBarAlpha = 1
        
        
        
        homeTableView.delegate = self
        homeTableView.dataSource = self
        view.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.left.right.bottom.equalToSuperview()
        }
        
        homeTableView.tableHeaderView = myHeadView
        
        
        let headSubView = UIView()
        headSubView.backgroundColor = .clear
        headSubView.layer.cornerRadius = 12
        headSubView.clipsToBounds = true
        myHeadView.addSubview(headSubView)
        headSubView.snp.makeConstraints { make in
            make.top.equalTo(17)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(105)
        }
        
        let bkgImageV = UIImageView(image: UIImage(named: "person_info_head_bkg"))
        bkgImageV.contentMode = .scaleAspectFill
        headSubView.addSubview(bkgImageV)
        bkgImageV.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        
        
        // 头像 名称 电话号码
        headSubView.addSubview(headImagV)
        headImagV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(21)
            make.width.height.equalTo(42)
        }
        
        headSubView.addSubview(nameLable)
        nameLable.snp.makeConstraints { make in
            make.top.equalTo(headImagV.snp.top)
            make.left.equalTo(headImagV.snp.right).offset(12)
            make.height.equalTo(24)
            make.right.equalTo(-12)
        }
        
        headSubView.addSubview(phoneLable)
        phoneLable.snp.makeConstraints { make in
            make.top.equalTo(nameLable.snp.bottom)
            make.left.equalTo(headImagV.snp.right).offset(12)
            make.height.equalTo(20)
            make.right.equalTo(-12)
        }
        
        let iconImageV = UIImageView(image: UIImage(named: "person_info_head_icon"))
        iconImageV.contentMode = .scaleAspectFill
        headSubView.addSubview(iconImageV)
        iconImageV.snp.makeConstraints { make in
            make.top.bottom.right.equalToSuperview()
            make.width.equalTo(125)
        }
        
        
//        self.homeTableView.es.addPullToRefresh {
//            self.loadFirstItem()
//        }
        
//        self.homeTableView.es.startPullToRefresh()
//        self.homeTableView.es.addInfiniteScrolling {
//            self.loadFakeData()
//        }
    }
    
    private func loadFirstItem() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.homeTableView.es.stopPullToRefresh(ignoreDate: true,ignoreFooter: false)
            self.homeTableView.reloadData()
        }
    }
    
    private func loadFakeData() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.homeTableView.es.stopLoadingMore()
            self.homeTableView.reloadData()
        }
    }
}

// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHPersonInformMainViewController : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 0.001))
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 0.001))
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return myCellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cellView.view.frame = CGRect(x: 0, y: 0, width: cell.contentView.bounds.size.width, height: myCellHeight)
        cell.contentView.addSubview(cellView.view)
        
        cellView.view.backgroundColor = .orange//for test hjl
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        printLog("点击了 tableView Cell \(indexPath.row)")
    }
}

extension YHPersonInformMainViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let bottomCellOffset: CGFloat = homeTableView.rect(forSection: 0).origin.y
        if (scrollView.contentOffset.y >= bottomCellOffset) {
            scrollView.contentOffset = CGPointMake(0, bottomCellOffset)
            if canScroll == true {
                canScroll = false
                //传递参数到 子VC
                cellView.canScroll = true
            }
        }else{
            if canScroll == false {
                scrollView.contentOffset = CGPoint(x: 0, y: bottomCellOffset)
            }
        }
        self.homeTableView.showsVerticalScrollIndicator = false
    }
}
