package com.miya.fastcashier.log

import android.app.Activity
import android.app.Fragment
import android.content.Intent
import android.os.Bundle
import androidx.annotation.StyleRes
import com.miya.fastcashier.R

/**
 * 作者：Leon
 * 时间：2017/3/20 16:57
 */
class LFilePicker {

    private var mActivity: Activity? = null
    private var mFragment: Fragment? = null
    private var mTitle: String? = null
    private var mTitleColor: String? = null
    private var theme = R.style.LFileTheme
    private var mTitleStyle = R.style.LFileToolbarTextStyle
    private var mBackgroundColor: String? = null
    private var mBackStyle = 0
    private var mRequestCode = 0
    private var mMutilyMode = true
    private var mChooseMode = true
    private var mAddText: String? = null
    private var mIconStyle = 0
    private var mFileTypes: Array<String>? = null
    private var mNotFoundFiles: String? = null
    private var mMaxNum = 0
    private var mStartPath: String? = null
    private var mIsGreater = true //是否大于
    private var mFileSize: Long = 0
    private var mFileName: String? = null

    /**
     * 绑定Activity
     *
     * @param activity
     * @return
     */
    fun withActivity(activity: Activity?): LFilePicker {
        mActivity = activity
        return this
    }

    /**
     * 绑定Fragment
     *
     * @param fragment
     * @return
     */
    fun withFragment(fragment: Fragment?): LFilePicker {
        mFragment = fragment
        return this
    }

    /**
     * 设置主标题
     *
     * @param title
     * @return
     */
    fun withTitle(title: String?): LFilePicker {
        mTitle = title
        return this
    }

    /**
     * 设置辩题颜色
     *
     * @param color
     * @return
     */
    @Deprecated("")
    fun withTitleColor(color: String?): LFilePicker {
        mTitleColor = color
        return this
    }

    /**
     * 设置主题
     *
     * @param theme
     * @return
     */
    fun withTheme(@StyleRes theme: Int): LFilePicker {
        this.theme = theme
        return this
    }

    /**
     * 设置标题的颜色和字体大小
     *
     * @param style
     * @return
     */
    fun withTitleStyle(@StyleRes style: Int): LFilePicker {
        mTitleStyle = style
        return this
    }

    /**
     * 设置背景色
     *
     * @param color
     * @return
     */
    fun withBackgroundColor(color: String?): LFilePicker {
        mBackgroundColor = color
        return this
    }

    /**
     * 请求码
     *
     * @param requestCode
     * @return
     */
    fun withRequestCode(requestCode: Int): LFilePicker {
        mRequestCode = requestCode
        return this
    }

    /**
     * 设置返回图标
     *
     * @param backStyle
     * @return
     */
    fun withBackIcon(backStyle: Int): LFilePicker {
        mBackStyle = 0 //默认样式
        mBackStyle = backStyle
        return this
    }

    /**
     * 设置选择模式，默认为true,多选；false为单选
     *
     * @param isMutily
     * @return
     */
    fun withMutilyMode(isMutily: Boolean): LFilePicker {
        mMutilyMode = isMutily
        return this
    }

    /**
     * 设置多选时按钮文字
     *
     * @param text
     * @return
     */
    fun withAddText(text: String?): LFilePicker {
        mAddText = text
        return this
    }

    /**
     * 设置文件夹图标风格
     *
     * @param style
     * @return
     */
    fun withIconStyle(style: Int): LFilePicker {
        mIconStyle = style
        return this
    }

    fun withFileFilter(arrs: Array<String>): LFilePicker {
        mFileTypes = arrs
        return this
    }

    /**
     * 没有选中文件时的提示信息
     *
     * @param notFoundFiles
     * @return
     */
    fun withNotFoundBooks(notFoundFiles: String?): LFilePicker {
        mNotFoundFiles = notFoundFiles
        return this
    }

    /**
     * 设置最大选中数量
     *
     * @param num
     * @return
     */
    fun withMaxNum(num: Int): LFilePicker {
        mMaxNum = num
        return this
    }

    /**
     * 设置选择上传的文件
     *
     * @param fileName
     * @return
     */
    fun withFileName(fileName: String?): LFilePicker {
        mFileName = fileName
        return this
    }

    /**
     * 设置初始显示路径
     *
     * @param path
     * @return
     */
    fun withStartPath(path: String?): LFilePicker {
        mStartPath = path
        return this
    }

    /**
     * 设置选择模式，true为文件选择模式，false为文件夹选择模式，默认为true
     *
     * @param chooseMode
     * @return
     */
    fun withChooseMode(chooseMode: Boolean): LFilePicker {
        mChooseMode = chooseMode
        return this
    }

    /**
     * 设置文件大小过滤方式：大于指定大小或者小于指定大小
     *
     * @param isGreater true：大于 ；false：小于，同时包含指定大小在内
     * @return
     */
    fun withIsGreater(isGreater: Boolean): LFilePicker {
        mIsGreater = isGreater
        return this
    }

    /**
     * 设置过滤文件大小
     *
     * @param fileSize
     * @return
     */
    fun withFileSize(fileSize: Long): LFilePicker {
        mFileSize = fileSize
        return this
    }

    fun start() {
        if (mActivity == null && mFragment == null) {
            throw RuntimeException("You must pass Activity or Fragment by withActivity or withFragment or withSupportFragment method")
        }
        val intent = initIntent()
        val bundle = bundle
        intent.putExtras(bundle)
        if (mActivity != null) {
            mActivity!!.startActivityForResult(intent, mRequestCode)
        } else {
            mFragment!!.startActivityForResult(intent, mRequestCode)
        }
    }

    private fun initIntent(): Intent {
        val intent: Intent
        intent = if (mActivity != null) {
            Intent(mActivity, LFilePickerActivity::class.java)
        } else {
            Intent(mFragment!!.activity, LFilePickerActivity::class.java)
        }
        return intent
    }

    private val bundle: Bundle
        private get() {
            val paramEntity = ParamEntity()
            paramEntity.title = mTitle
            paramEntity.theme = theme
            paramEntity.titleColor = mTitleColor
            paramEntity.titleStyle = mTitleStyle
            paramEntity.backgroundColor = mBackgroundColor
            paramEntity.backIcon = mBackStyle
            paramEntity.isMutilyMode = mMutilyMode
            paramEntity.addText = mAddText
            paramEntity.iconStyle = mIconStyle
            paramEntity.fileTypes = mFileTypes
            paramEntity.notFoundFiles = mNotFoundFiles
            paramEntity.maxNum = mMaxNum
            paramEntity.isChooseMode = mChooseMode
            paramEntity.path = mStartPath
            paramEntity.fileSize = mFileSize
            paramEntity.fileName = mFileName
            paramEntity.isGreater = mIsGreater
            val bundle = Bundle()
            bundle.putSerializable("param", paramEntity)
            return bundle
        }
}