package com.lukouguoji.hangban.activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.core.widget.NestedScrollView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.hangban.R
import com.lukouguoji.hangban.adapt.HangBanQueryInfoListAdapter
import com.lukouguoji.hangban.model.HangBanQueryInfo
import com.lukouguoji.hangban.viewModel.HangBanQueryInfoViewModel
import com.lukouguoji.module_base.BaseActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants

@Deprecated("旧版航班查询详情，仅被废弃的 HangBanQueryActivity 调用，实际不可达")
@Route(path = ARouterConstants.ACTIVITY_URL_HANG_BAN_QUERY_INFO)
class HangBanQueryInfoActivity : BaseActivity(), View.OnClickListener {
    private lateinit var viewModel: HangBanQueryInfoViewModel

    private lateinit var scrollView: NestedScrollView
    private lateinit var gncQueryInfoList: RecyclerView
    private lateinit var adapter: HangBanQueryInfoListAdapter
    private val queryInfoAdapterList = arrayListOf<HangBanQueryInfo>()

    @JvmField
    @Autowired
    var id: Int = -9999

    private fun initView() {
        viewModel = ViewModelProvider(this).get(HangBanQueryInfoViewModel::class.java)

        scrollView = findViewById(R.id.scrollView)

        val layoutManager = GridLayoutManager(this, 2)
        adapter = HangBanQueryInfoListAdapter(this, queryInfoAdapterList)
        gncQueryInfoList = findViewById(R.id.gncQueryInfoList)
        gncQueryInfoList.adapter = adapter
        gncQueryInfoList.layoutManager = layoutManager

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hang_ban_query_info)
        setBackArrow("航班查询详情")
        initView()

        viewModel.queryFlightByIdHangBan(id)
        viewModel.queryFlightByIdHangBanObserver.observe(this) {
            val status = it.getString("status")
            if (Constant.Result.succ == status) {
                viewModel.wayBillInfo = it.getJSONObject("data")
                //航班日期 fdate -> 2022-06-08
                adapter.append(HangBanQueryInfo(1, "航班日期", viewModel.wayBillInfo.getString("fdate") ?: ""))
                //航班号 fno -> MU2021
                adapter.append(HangBanQueryInfo(1, "航班号", viewModel.wayBillInfo.getString("fno") ?: ""))
                //航程 range -> HFE-PEK
                adapter.append(HangBanQueryInfo(1, "航程", viewModel.wayBillInfo.getString("range") ?: ""))
                //经停 jtz -> null
                adapter.append(HangBanQueryInfo(1, "经停", viewModel.wayBillInfo.getString("jtz") ?: ""))
                //计划到达 scheduledArrival -> 2022-06-08 22:00:00
                adapter.append(HangBanQueryInfo(1, "计划到达", viewModel.wayBillInfo.getString("scheduledArrival") ?: ""))
                //计划起飞 scheduledTackOff -> 2022-06-08 20:00:00
                adapter.append(HangBanQueryInfo(1, "计划起飞", viewModel.wayBillInfo.getString("scheduledTackOff") ?: ""))
                //预计到达  estimatedArrival -> 2022-06-08 22:30:00
                adapter.append(HangBanQueryInfo(1, "预计到达", viewModel.wayBillInfo.getString("estimatedArrival") ?: ""))
                //预计起飞 estimatedTakeOff -> 2022-06-08 21:00:00
                adapter.append(HangBanQueryInfo(1, "预计起飞", viewModel.wayBillInfo.getString("estimatedTakeOff") ?: ""))
                //实际到达 actualArrival -> null
                adapter.append(HangBanQueryInfo(1, "实际到达", viewModel.wayBillInfo.getString("actualArrival") ?: ""))
                //实际起飞 actualTakeOff -> null
                adapter.append(HangBanQueryInfo(1, "实际起飞", viewModel.wayBillInfo.getString("actualTakeOff") ?: ""))
                //地区类型 countryType -> 国内
                adapter.append(HangBanQueryInfo(1, "地区类型", viewModel.wayBillInfo.getString("countryType") ?: ""))
                //服务类型 serviceType -> 客机
                adapter.append(HangBanQueryInfo(1, "服务类型", viewModel.wayBillInfo.getString("serviceType") ?: ""))
                //航班状态 flightStatus -> null
                adapter.append(HangBanQueryInfo(1, "航班状态", viewModel.wayBillInfo.getString("flightStatus") ?: ""))
                //飞行状态 portCode
                adapter.append(HangBanQueryInfo(1, "飞行状态", viewModel.wayBillInfo.getString("portCode") ?: ""))
                //延误原因 delayFreeText -> null
                adapter.append(HangBanQueryInfo(1, "延误原因", viewModel.wayBillInfo.getString("delayFreeText") ?: ""))
                //机型 aircraftCode -> 319
                adapter.append(HangBanQueryInfo(1, "机型", viewModel.wayBillInfo.getString("aircraftCode") ?: ""))
                //机号 registration -> null
                adapter.append(HangBanQueryInfo(1, "机号", viewModel.wayBillInfo.getString("registration") ?: ""))

            } else {
                //无数据
            }
        }

    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                else -> {
                    Toast.makeText(this, "未找到对应操作", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}