//
//  YHMainViewController.swift
//  galaxy
//
//  Created by EDY on 2023/12/20.
//

import UIKit
import SnapKit
import NEConversationUIKit

struct PersonalModuleItem {
    var icon: String?
    var title: String
    init(icon: String? = nil, title: String) {
        self.icon = icon
        self.title = title
    }
}

class YHMyViewController: YHBaseViewController, ConstraintRelatableTarget {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style:.plain)
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        }
        tableView.tableHeaderView = headerView
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.register(YHPersonalCenterCell.self, forCellReuseIdentifier: YHPersonalCenterCell.cellReuseIdentifier)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var items: [PersonalModuleItem] = {
        return [PersonalModuleItem(icon: "mine_item_msg", title: "我的消息".local),
                PersonalModuleItem(icon: "mine_item_progress", title: "我的进度".local),
                PersonalModuleItem(icon: "mine_item_scoreCenter", title: "积分中心".local),
                PersonalModuleItem(icon: "mine_item_recommend", title: "推荐有礼".local),
                PersonalModuleItem(icon: "mine_item_poster", title: "我的海报".local),
                PersonalModuleItem(icon: "mine_item_code", title: "我的测评码".local),
                PersonalModuleItem(icon: "mine_item_appoint", title: "预约银河湾".local),]
    }()
    
    lazy var headerView: YHUserInfoHeaderView = {
        let view = YHUserInfoHeaderView.createView()
        view.settingBlock = {
            [weak self] in
            guard let self = self else { return }
            let vc = YHMySettingViewController()
            self.navigationController?.pushViewController(vc)
        }
        return view
    }()
    
    private lazy var bgImgView:UIImageView = {
        let img = UIImageView(image: UIImage(named: "mine_header_bg"))
        return img
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
    }
    
    func initView() {
        
        gk_navigationBar.isHidden = true
        view.addSubview(bgImgView)
        view.addSubview(tableView)
        
        bgImgView.snp.makeConstraints { make in
            make.left.right.top.equalTo(view)
            make.height.equalTo(bgImgView.snp.width).multipliedBy(YHUserInfoHeaderView.bgRatio)
        }
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
    }
}

extension YHMyViewController : UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: YHPersonalCenterCell.cellReuseIdentifier, for: indexPath) as! YHPersonalCenterCell
        let item : PersonalModuleItem = items[indexPath.row];
        cell.updateItem(item: item)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 52
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if (indexPath.row >= items.count) { return }
        let vc = UINavigationController(rootVC: YHPhoneLoginViewController())
        vc.modalPresentationStyle = .custom
        self.navigationController?.present(vc, animated: true)
//        let vc = YHFamilyMemberFormVC(orderId: 133971)
//        self.navigationController?.pushViewController(vc)
        return

        
        let item: PersonalModuleItem = items[indexPath.row];
        
        if item.title == "关于我们" {
            let vc = YHAboutUsViewController()
            self.navigationController?.pushViewController(vc)
            
        } else if item.title == "我的设置" {
            
            let account = "duyu"
            let token = "123456"
            let vc = ConversationController()
            self.navigationController?.pushViewController(vc)
            
            IMKitClient.instance.loginIM(account, token) { error in
                if let err = error {
                    print("login error in app : ", err.localizedDescription)
                }else {
                    let _ = NEAtMessageManager.instance
                   
                }
            }
            
            
            
        } else {
            let vc = YHLoginViewController()
            self.navigationController?.pushViewController(vc)
        }

    }
}
