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

import UIKit
import SnapKit
import NEConversationUIKit

struct PersonalModuleItem {
    var icon: String
    var title: String
}

class YHMyViewController: YHBaseViewController, ConstraintRelatableTarget {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style:.plain)
        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_manager", title: "我的管家"),
                PersonalModuleItem(icon: "mine_manager", title: "我的订单"),
                PersonalModuleItem(icon: "mine_manager", title: "我的合同"),
                PersonalModuleItem(icon: "mine_manager", title: "我的进度"),
                PersonalModuleItem(icon: "mine_manager", title: "我的积分"),
                PersonalModuleItem(icon: "mine_manager", title: "我的设置"),
                PersonalModuleItem(icon: "mine_manager", title: "关于我们"),]
    }()
    
    lazy var headerView: UIView = {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 110))
        
        let iconImgView = UIImageView(image: UIImage(named:"mine_head_default"))
        view.addSubview(iconImgView)
        
        let nameLabel = UILabel()
        nameLabel.text = "智慧银河"
        nameLabel.textColor = UIColor(hexString:"#222222")
        nameLabel.textAlignment = NSTextAlignment.left
        nameLabel.font = UIFont.systemFont(ofSize: 20)
        view.addSubview(nameLabel)
        
        let mobileLabel = UILabel()
        mobileLabel.text = "133****0000"
        mobileLabel.textColor = UIColor(hexString:"#666666")
        mobileLabel.textAlignment = NSTextAlignment.left
        mobileLabel.font = UIFont.systemFont(ofSize: 13)
        view.addSubview(mobileLabel)
        
        iconImgView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(55)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImgView.snp.right).offset(10)
            make.right.equalToSuperview().offset(-16).priority(.low)
            make.bottom.equalTo(view.snp.centerY)
        }
        
        mobileLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImgView.snp.right).offset(10)
            make.right.equalTo(nameLabel)
            make.top.equalTo(view.snp.centerY)
        }
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
    }
    
    func initView() {
        
        self.title = "个人中心"
//        gk_navTitle = "个人中心"
//        gk_navBackgroundColor = .clear
//        gk_navShadowColor = .clear
        
        let bgImgView = UIImageView(image: UIImage(named: "mine_header"))
        view.addSubview(bgImgView)
        bgImgView.snp.makeConstraints { make in
            make.left.right.top.equalTo(self.view)
            make.height.equalTo( 435.0/750.0 * KScreenHeight)
        }
        
        view.addSubview(tableView);
        tableView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(view)
        }
        
        
    }
}

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 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)
        }

    }
}
