//
//  LLQ_SettingController.swift
//  PrivateBrowser
//
//  Created by MAC on 2022/6/10.
//

import Foundation
import UIKit
import MessageUI
import MBProgressHUD

class LLQ_SettingController : LLQ_BaseViewController, UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate {
    var emailController: MFMailComposeViewController?

    enum Style {
        case privacy
        case terms
        case feedback
        var imageName: String{
            switch self {
            case .privacy:
                return "setting_privacy"
            case .feedback:
                return "setting_feedback"
            case .terms:
                return "setting_terms"
            }
        }
        var name: String{
            switch self {
            case .privacy:
                return "Privacy Policy"
            case .feedback:
                return "feedback"
            case .terms:
                return "User terms"
            }
        }
    }
    
    var datas: [Style] = [.privacy, .terms, .feedback]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.set()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        self.setNavigationBarLucent()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        self.resetNavigationBar()
    }
    private func set(){
        self.title = "Setting"
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LLQ_HomeSettingsCell") as! LLQ_HomeSettingsCell
        cell.icon.image = UIImage.init(named: self.datas[indexPath.row].imageName)
        cell.label.text = self.datas[indexPath.row].name
        return cell
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let style = self.datas[indexPath.row]
        switch style {
        case .privacy:
            self.showPolicy()
            break
        case .terms:
            self.showTerms()
            break
        case .feedback:
            self.toEmailFeedback()
            break
        }
    }
}

extension LLQ_SettingController {
    func toEmailFeedback(){
        if self.emailController != nil {
            self.emailController?.mailComposeDelegate = nil
            self.emailController = nil
        }
        self.emailController = MFMailComposeViewController.init()
        self.emailController?.mailComposeDelegate = self
        if MFMailComposeViewController.canSendMail() {
            self.emailController?.setToRecipients([SUPPORT_EMAIL_ADDRESS])
//            LLQ_ScannerLog("[Feedback] email show")
            LLQ_RootViewController.now.topPresentedVC().present(self.emailController!, animated: true, completion: nil)
        } else {
            self.showToast(text: "Please set up an Email Account in order to send email to '\(SUPPORT_EMAIL_ADDRESS)'")
        }
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        self.emailController?.dismiss(animated: true, completion: {
//            LLQ_ScannerLog("[Feedback] close")
        })
    }
    
    func showTerms() {

        let vc = LLQ_WebViewController()
        vc.destination = .terms
        navigationController?.pushViewController(vc, animated: true)
    }

    func showPolicy() {

        let vc = LLQ_WebViewController()
        vc.destination = .policy
        navigationController?.pushViewController(vc, animated: true)
    }
    func showToast(text: String, onScreen: Bool = false){
        let toast = MBProgressHUD.init()
        if onScreen {
            let parentView = LLQ_APPConfigs.getWindow() as UIView
            parentView.addSubview(toast)
        } else {
            self.view.addSubview(toast)
        }
        toast.mode = .text
        toast.label.text = text
        toast.label.textColor = UIColor.white
        toast.removeFromSuperViewOnHide = true
        toast.show(animated: true)
        toast.hide(animated: true, afterDelay: 2)
        toast.bezelView.color = UIColor.init(hexString: "#000000", alpha: 0.9)
        toast.bezelView.style = .solidColor
        toast.label.font = UIFont.systemFont(ofSize: 15)
        toast.label.numberOfLines = 0
        toast.margin = 14
        toast.isUserInteractionEnabled = false
    }
    
}
