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

import UIKit
import SnapKit
import NEConversationUIKit

class YHMySettingViewController: YHBaseViewController, ConstraintRelatableTarget {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style:.plain)
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        }
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.register(YHMySettingCell.self, forCellReuseIdentifier: YHMySettingCell.cellReuseIdentifier)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var logoutBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("退出登录".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_M(ofSize: 16)
        btn.setTitleColor(UIColor(hexString:"#313131"), for: .normal)
        btn.backgroundColor = .contentBkgColor
        btn.layer.cornerRadius = 6.0
        btn.addTarget(self, action: #selector(logout), for: .touchUpInside)
        return btn
    }()
    
    lazy var items: [PersonalModuleItem] = {
        return [PersonalModuleItem(title: "调整设置".local, type:.changeSettings),
                PersonalModuleItem(title: "深色模式".local, type:.darkMode),
                PersonalModuleItem(title: "个人信息收集清单".local, type:.infoCollectList),
                PersonalModuleItem(title: "第三方信息共享清单".local, type:.infoShareList),
                PersonalModuleItem(title: "关于我们".local, type:.aboutUs)]
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
    }
    
    func initView() {
        
        self.gk_navTitle = "设置".local
        self.view.backgroundColor = .white
        view.addSubview(tableView)
        view.addSubview(logoutBtn)
        
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(k_Height_NavigationtBarAndStatuBar)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(logoutBtn.snp.top).offset(-30)
        }
        logoutBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(48.0)
            make.bottom.equalToSuperview().offset(-30)
        }
    }
    
    @objc func logout() {
        YHLoginManager.shared.clearUserInfo()
        NotificationCenter.default.post(name: YhConstant.YhNotification.didLogoutSuccessNotifiction, object: nil)
        YHHUD.flash(message: "退出成功")
        self.navigationController?.popViewController(animated: true)
    }
}

extension YHMySettingViewController : 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: YHMySettingCell.cellReuseIdentifier, for: indexPath) as! YHMySettingCell
        let item : PersonalModuleItem = items[indexPath.row];
        cell.titleLabel.text = item.title
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 53.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if (indexPath.row >= items.count) { return }
        let item = items[indexPath.row]
        if item.type == .aboutUs {
           let vc = YHAboutUsViewController()
           self.navigationController?.pushViewController(vc)
        }
    }
}

