package com.pan_american.android.ui.group_management.group_info

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseFragment
import com.pan_american.android.base.ListAdapter
import com.pan_american.android.data.model.common.entity.Selector
import com.pan_american.android.databinding.FragmentGroupInfoSearchBinding

class GroupInfoSearchFragment : BaseFragment<FragmentGroupInfoSearchBinding>() {

    private var groupTypeList = ArrayList<Selector>()

    private var groupTypeId = -1

    private var groupStatusId = -1

    private var inStack = false

    //当前界面的数据保存
    private var groupName = ""

    private var clientName = ""

    private var clientUnit = ""

    private var visitDate = ""

    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?, bundle: Bundle?
    ) = FragmentGroupInfoSearchBinding.inflate(layoutInflater, container, false)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //准备groupTypeList
        groupTypeList.add(Selector().apply {
            id = 0
            name = resources.getString(R.string.all)
        })

        for (item in OASystem.groupType) {
            groupTypeList.add(item)
        }
    }

    override fun onStart() {
        super.onStart()

        initViews()
        initEvents()
    }

    override fun onStop() {
        super.onStop()

        //保存当前界面的数据
        groupName = binding.groupName.text.toString()
        clientName = binding.customerName.text.toString()
        clientUnit = binding.customerDepartment.text.toString()
        visitDate = binding.visitDate.text.toString()

        inStack = true
    }

    override fun initViews() {

        if (inStack) {

            if (groupTypeId != 0) {
                for (item in OASystem.groupType) {
                    if (item.id == groupTypeId) {
                        binding.groupType.text = item.name
                    }
                }
            }

            if (groupStatusId != 0) {
                for (item in OASystem.groupStatus) {
                    if (item.id == groupStatusId) {
                        binding.groupState.text = item.name
                    }
                }
            }

            binding.groupName.setText(groupName)
            binding.customerName.setText(clientName)
            binding.customerDepartment.setText(clientUnit)
            binding.visitDate.text = visitDate
        }

    }

    override fun initEvents() {

        binding.groupType.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(groupTypeList)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = data.name

                            if (groupTypeId == data.id) {
                                setTextColor(ResourcesCompat.getColor(resources, R.color.text_color_blue, null))
                            } else {
                                setTextColor(ResourcesCompat.getColor(resources, R.color.text_color, null))
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick =
                    object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                        override fun onItemClick(position: Int) {
                            groupTypeList[position].apply {
                                binding.groupType.text = name
                                groupTypeId = id
                            }

                            popupWindow.dismiss()
                        }
                    }
            }
        }

        binding.groupState.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.groupStatus)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = data.name

                            if (groupStatusId == data.id) {
                                setTextColor(ResourcesCompat.getColor(resources, R.color.text_color_blue, null))
                            } else {
                                setTextColor(ResourcesCompat.getColor(resources, R.color.text_color, null))
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick =
                    object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                        override fun onItemClick(position: Int) {
                            OASystem.groupStatus[position].apply {
                                binding.groupState.text = name
                                groupStatusId = id
                            }

                            popupWindow.dismiss()
                        }
                    }
            }
        }

        binding.visitDate.setOnClickListener {
            showDatePicker(resources.getString(R.string.visit_date), binding.visitDate) {}
        }

        binding.commit.setOnClickListener {
            //点击查询隐藏软键盘
            val inputMethodManager =
                requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            if (inputMethodManager.isActive) {
                inputMethodManager.hideSoftInputFromWindow(
                    requireActivity().window.decorView.windowToken, 0
                )
            }

            val groupInfoListFragment = GroupInfoListFragment()

            val bundle = Bundle().apply {
                putInt("group_type", groupTypeId)
                putInt("group_status", groupStatusId)
                putString("group_name", binding.groupName.text.toString())
                putString("client_name", binding.customerName.text.toString())
                putString("customer_department", binding.customerDepartment.text.toString())
                putString("visit_date", binding.visitDate.text.toString())
            }

            groupInfoListFragment.arguments = bundle

            parentFragmentManager.beginTransaction().setCustomAnimations(
                R.anim.slide_right_in,
                R.anim.slide_left_out,
                R.anim.slide_left_in,
                R.anim.slide_right_out
            ).addToBackStack(null).replace(R.id.group_information_container, groupInfoListFragment)
                .commit()
        }
    }
}