//
//  SettingViewController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/4/10.
//

import UIKit

class MPSideA_SettingViewController: MPSideA_BaseViewController {
    @IBOutlet weak var tableView: UITableView!{
        didSet{
            if #available(iOS 15.0, *) {
                tableView.sectionHeaderTopPadding = 0
            }
            tableView.rowHeight = 70*width
            tableView.dataSource = self
            tableView.delegate = self
            tableView.register(UINib(nibName: SettingTableViewCellID, bundle: nil), forCellReuseIdentifier: SettingTableViewCellID)
        }
    }
    private let SettingTableViewCellID = "MPSideA_SettingTableViewCell"
    private lazy var titles:[String] = ["About","Feedback","Share","Privacy Policy","Terms of Use"]
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    @IBAction func popClick(_ sender: UIButton) {
        navigationController?.popViewController(animated: true)
    }
}
//MARK: - tableView
extension MPSideA_SettingViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SettingTableViewCellID, for: indexPath) as! MPSideA_SettingTableViewCell
        cell.title = titles[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0://About
            let aboutVC = MPSideA_AboutViewController()
            navigationController?.pushViewController(aboutVC, animated: true)
        case 1://Feedback
            let alert = UIAlertController(title: "Feedback", message: "If you have any comments or suggestions, please contact us at the following e-mail address", preferredStyle: .actionSheet)
            let email = UIAlertAction(title: "support@musicoo.app", style: .default) { (_) in
                //将邮箱复制到剪切板中
                UIPasteboard.general.string = "support@musicoo.app"
                MP_HUD.text("Successfully copied the e-mail address to the clipboard", delay: 1.0, completion: nil)
            }
            alert.addAction(email)
            let cancel = UIAlertAction(title: "Cancel", style: .cancel)
            alert.addAction(cancel)
            present(alert, animated: true)
        case 2://Share
            //设置分享文本
            let text = App_Name
            //分享图片icon
            let image = UIImage(named: "ICON")
            //设置分享路径
            let url = URL(string: "https://musicoo.app/")
            let activityItems = [text,image as Any,url as Any]
            //弹出分享框
            let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities:nil)
            present(activityViewController, animated: true, completion: nil)
            //分享结束后的回调
            activityViewController.completionWithItemsHandler = { [weak self] ( activityType, isCompleted, returnedItems, error) -> Void in
                print(isCompleted ? "Share Success":"Share Failure")
            }
        case 3:
            MP_NetWorkManager.shared.requestNetworkPermission(oberve: self) {
                [weak self] in
                let privacyVC = MPSideA_PrivacyViewController()
                self?.navigationController?.pushViewController(privacyVC, animated: true)
            }
        default:
            MP_NetWorkManager.shared.requestNetworkPermission(oberve: self) {
                [weak self] in
                let serviceVC = MPSideA_ServiceViewController()
                self?.navigationController?.pushViewController(serviceVC, animated: true)
            }
        }
    }
}
