package com.cr.pages

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.cr.common.CrPictureManager
import com.cr.common.CrFileManager
import com.cr.common.CrUnitManager
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.map.CaseModel
import com.cr.widget.CrImageBrowserWidget

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/6/15 16:06
 * 描述：案件上传主页面
 */
class FragmentUploadCaseMain : CrNavigationFragment(), View.OnClickListener {
    companion object {
        const val OPEN_ALBUM = 3  // define: 2023/6/16 相册
    }

    private var lblCaseName: TextView? = null // define: 2023/6/15 案件名称
    private var lblCaseImageCount: TextView? = null // define: 2023/6/15 案件照片数量
    private var lblCaseCreateDate: TextView? = null // define: 2023/6/15 案件创建日期
    private var lblCaseMediaName: TextView? = null // define: 2023/6/15 案件媒体点名称
    private var lblCaseMediaSize: TextView? = null // define: 2023/6/15 案件媒体大小
    private var btnEdit: LinearLayout? = null // define: 2023/6/15 编辑按钮
    private var btnPhoto: LinearLayout? = null // define: 2023/6/15 相册按钮
    private var btnUpload: LinearLayout? = null // define: 2023/6/15 上传按钮
    private var imageBrowser: CrImageBrowserWidget? = null // define: 2023/6/16 图片浏览控件

    private var joinCase: CaseModel? = null // define: 2023/6/15 关联的案件点

    /**
     * 初始化
     * @param inflater LayoutInflater
     * @param container ViewGroup?
     * @param savedInstanceState Bundle?
     * @return View?
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mainView = inflater.inflate(R.layout.frag_case_upload_main, null)
        self = this
        // todo: 2023/6/15 挂载控件
        joinControls()
        // todo: 2023/6/15 显示
        if (this.joinCase != null) {
            crSetJoinCase(this.joinCase!!)
        }
        return mainView
    }

    /**
     * 覆写挂载控件
     */
    override fun joinControls() {
        mainView?.let {
            lblCaseName = it.findViewById(R.id.case_name)
            lblCaseImageCount = it.findViewById(R.id.case_image_count)
            lblCaseCreateDate = it.findViewById(R.id.case_create_date)
            lblCaseMediaName = it.findViewById(R.id.case_media_name)
            lblCaseMediaSize = it.findViewById(R.id.case_media_size)

            btnEdit = it.findViewById(R.id.case_btn_edit)
            btnEdit?.setOnClickListener(this)

            btnPhoto = it.findViewById(R.id.case_btn_photo)
            btnPhoto?.setOnClickListener(this)

            btnUpload = it.findViewById(R.id.case_btn_upload)
            btnUpload?.setOnClickListener(this)

            imageBrowser = it.findViewById(R.id.case_image_browser)
        }
    }

    /**
     * 覆写点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when (view?.id) {
            // todo: 2023/6/15 编辑照片
            R.id.case_btn_edit -> {

            }
            // todo: 2023/6/15 打开相册
            R.id.case_btn_photo -> {
                Intent(Intent.ACTION_PICK).apply {
                    type = "image/*"
                    startActivityForResult(this, OPEN_ALBUM)
                }
            }
            // todo: 2023/6/15 上传
            R.id.case_btn_upload -> {

            }
        }
    }

    /**
     * 设置关联的案件点
     * @param joinCase CaseModel 案件点
     */
    fun crSetJoinCase(joinCase: CaseModel) {
        this.joinCase = joinCase
        // todo: 2023/6/15 设置显示内容
        lblCaseName?.text = this.joinCase?.name
        lblCaseImageCount?.text = String.format("取证照片[%s]张", this.joinCase?.imgArray?.size)
        lblCaseCreateDate?.text = this.joinCase?.date
        lblCaseMediaName?.text = ""
        lblCaseMediaSize?.text = ""
    }

    /**
     * 覆盖外部调用返回接受并处理
     * @param requestCode Int
     * @param resultCode Int
     * @param data Intent?
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == OPEN_ALBUM && resultCode == RESULT_OK) {
            var imagePath: String? = null
            imagePath = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                CrPictureManager.handleImageAfterKitKat(context!!, data!!)
            } else {
                CrPictureManager.handleImageBeforeKitKat(context!!, data!!)
            }
            if (imagePath != null) {
                // todo: 2023/6/16 转存文件
                var newFilePath =
                    String.format("%sUAV%s.jpg", CrUtil.IMAGE_PATH, CrUnitManager.toSystemDate())
                var res = CrFileManager.copyFile(imagePath, newFilePath)
                if (res) {
                    imageBrowser?.crAppendImage(newFilePath)
                } else {
                    showError("文件转存失败！")
                }
            } else {
                showError("选择文件异常！")
            }
        }
    }
}