//
//  YHHomeViewCoontroller.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/3.
//  Copyright © 2024 www.davidhuang.com. All rights reserved.
//

import Foundation
import GKNavigationBarSwift

class YHHomeViewController: YHBaseViewController {
    //model 及网络请求
    lazy var homeReqVM : YHHomePageViewModel = {
        return YHHomePageViewModel()
    }()
    
    //头部背景图片
    lazy var headBkgImagV: UIImageView = {
        let imagV = UIImageView()
        imagV.image = UIImage(named: "home_head_bkg")
        imagV.contentMode = .scaleAspectFill
        return imagV
    }()
    
    //自定义导航栏
    lazy var homeNavBarView: UIView = {
        let view1 = UIView()
        view1.backgroundColor = .clear
        view1.isUserInteractionEnabled = true
        
        let icon = UIImageView(image: UIImage(named: "home_nav_icon"))
        icon.contentMode = .scaleAspectFill
        view1.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.height.equalTo(26)
            make.width.equalTo(120)
        }
        
        
        let btnMsg = UIButton(type: .custom)
        btnMsg.setImage(UIImage(named: "home_nav_msg"), for: .normal)
        view1.addSubview(btnMsg)
        btnMsg.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
            make.height.width.equalTo(34)
        }
        btnMsg.addTarget(self, action: #selector(tapSearch), for: .touchUpInside)
        
        
        let btnSearch = UIButton(type: .custom)
        btnSearch.setImage(UIImage(named: "home_nav_search"), for: .normal)
        btnSearch.addTarget(self, action: #selector(tapSearch), for: .touchUpInside)
        view1.addSubview(btnSearch)
        btnSearch.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(btnMsg.snp.left).offset(-12)
            make.height.width.equalTo(34)
        }
        
        return view1
    }()
    
    
    var homeTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 100
        tableView.register(YHHomeCustomCell.self, forCellReuseIdentifier: YHHomeCustomCell.cellReuseIdentifier)
        return tableView
    }()
    
    
    
    
    
    // MARK: - 生命周期方法
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.UI
        initView()
        
        //2.网络请求
        requestData()
        
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
}

// MARK: - 私有方法
extension YHHomeViewController {
    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 = true
        
        view.addSubview(headBkgImagV)
        headBkgImagV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(307)
        }
        
        
        view.addSubview(homeNavBarView)
        homeNavBarView.layer.zPosition = 1
        homeNavBarView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_safeAreaInsetsTop())
            make.left.right.equalToSuperview()
            make.height.equalTo(48)
        }
        
        homeTableView.delegate = self
        homeTableView.dataSource = self
        view.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.top.equalTo(homeNavBarView.snp.bottom).offset(5)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    @objc func tapSearch() {
        printLog("*")
        
        navigationController?.pushViewController(YHDavidTestViewController())
        
        
        
    }
}

// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHHomeViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell0 = tableView.dequeueReusableCell(withIdentifier: YHHomeCustomCell.cellReuseIdentifier, for: indexPath) as! YHHomeCustomCell
//        
//        cell0.setData(["A","B","B","B","B","B","B","B","B","B","B","B","B","B","B"])
//            
//        return cell0
        
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        printLog("点击了 tableView Cell \(indexPath.row)")
    }
    
}
