package com.pan_american.android.ui.workspace

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseFragment
import com.pan_american.android.databinding.FragmentWorkspaceBinding
import com.pan_american.android.ui.efficiency_tools.address_book.AddressBookActivity
import com.pan_american.android.ui.efficiency_tools.daily_payment.DailyPaymentListActivity
import com.pan_american.android.ui.efficiency_tools.exchange_tool.ExchangeToolActivity
import com.pan_american.android.ui.efficiency_tools.itinerary.ItineraryActivity
import com.pan_american.android.ui.group_airplane_ticket.airplane_payment_insert.AirplaneTicketPaymentActivity
import com.pan_american.android.ui.group_common.insurance_payment_insert.InsurancePaymentInsertActivity
import com.pan_american.android.ui.group_hotel.hotel_predetermine.HotelPredetermineActivity
import com.pan_american.android.ui.group_invite_official.invite_data.InviteDataActivity
import com.pan_american.android.ui.group_invite_official.invite_official_payment_insert.InviteOfficialPaymentInsertActivity
import com.pan_american.android.ui.group_management.group_info.GroupInfoActivity
import com.pan_american.android.ui.group_op.ground_convey_payment_insert.GroundConveyPaymentInsertActivity
import com.pan_american.android.ui.group_visa.visa_payment_insert.VisaPaymentInsertActivity
import com.pan_american.android.ui.resource_management.airplane_three_code.AirplaneThreeCodeActivity
import com.pan_american.android.ui.resource_management.car_resource.CarResourceActivity
import com.pan_american.android.ui.resource_management.guide_resource.GuideResourceActivity
import com.pan_american.android.ui.resource_management.hotel_resource.HotelResourceActivity
import com.pan_american.android.ui.resource_management.market_customer.MarketCustomerActivity

class WorkspaceFragment : BaseFragment<FragmentWorkspaceBinding>(), OnClickListener {

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

    override fun onStart() {
        super.onStart()

        initViews()
        initEvents()
    }

    override fun initViews() {

        when (OASystem.userInfo.sex) {
            0 -> {
                binding.userIcon.setImageResource(R.mipmap.icon_user_male)
            }

            1 -> {
                binding.userIcon.setImageResource(R.mipmap.icon_user_female)
            }
        }

        binding.userName.text = OASystem.userInfo.cnName
        binding.departmentName.text = OASystem.userInfo.depName
        binding.jobName.text = OASystem.userInfo.jobName

        //根据已有权限设置可见界面
        OASystem.authData.forEach {
            when (it.pageId) {

                //效率工具
                OASystem.ADDRESS_BOOK -> {
                    if (OASystem.authorization(OASystem.ADDRESS_BOOK, OASystem.VIEW)) {
                        binding.addressBook.visibility = View.VISIBLE
                        binding.addressBook.setOnClickListener(this)
                    }
                }

                OASystem.ITINERARY -> {
                    if (OASystem.authorization(OASystem.ITINERARY, OASystem.VIEW)) {
                        binding.itinerary.visibility = View.VISIBLE
                        binding.itinerary.setOnClickListener(this)
                    }
                }

                OASystem.DAILY_PAYMENT -> {
                    if (OASystem.authorization(OASystem.DAILY_PAYMENT, OASystem.VIEW)) {
                        binding.dailyPayment.visibility = View.VISIBLE
                        binding.dailyPayment.setOnClickListener(this)
                    }

                }

//                OASystem.GROUP_PAYMENT -> {
//                    binding.groupPayment.visibility = View.VISIBLE
//                    binding.groupPayment.setOnClickListener(this)
//                }

                //团组酒店
                OASystem.HOTEL_RESOURCE -> {
                    if (OASystem.authorization(OASystem.HOTEL_RESOURCE, OASystem.VIEW)) {
                        if (binding.groupHotel.visibility == View.GONE) {
                            binding.groupHotel.visibility = View.VISIBLE
                        }
                        binding.hotelResource.visibility = View.VISIBLE
                        binding.hotelResource.setOnClickListener(this)
                    }
                }

                OASystem.HOTEL_PREDETERMINE -> {
                    if (OASystem.authorization(OASystem.HOTEL_PREDETERMINE, OASystem.VIEW)) {
                        if (binding.groupHotel.visibility == View.GONE) {
                            binding.groupHotel.visibility = View.VISIBLE
                        }
                        binding.hotelPredetermine.visibility = View.VISIBLE
                        binding.hotelPredetermine.setOnClickListener(this)
                    }
                }

                //团组OP
                OASystem.GUIDE_RESOURCE -> {
                    if (OASystem.authorization(OASystem.GUIDE_RESOURCE, OASystem.VIEW)) {
                        if (binding.groupOp.visibility == View.GONE) {
                            binding.groupOp.visibility = View.VISIBLE
                        }
                        binding.guideResource.visibility = View.VISIBLE
                        binding.guideResource.setOnClickListener(this)
                    }
                }

                OASystem.CAR_RESOURCE -> {
                    if (OASystem.authorization(OASystem.CAR_RESOURCE, OASystem.VIEW)) {
                        if (binding.groupOp.visibility == View.GONE) {
                            binding.groupOp.visibility = View.VISIBLE
                        }
                        binding.carResource.visibility = View.VISIBLE
                        binding.carResource.setOnClickListener(this)
                    }

                }

                OASystem.GROUND_CONVEY_PAYMENT_INSERT -> {
                    if (OASystem.authorization(OASystem.GROUND_CONVEY_PAYMENT_INSERT, OASystem.VIEW)) {
                        if (binding.groupOp.visibility == View.GONE) {
                            binding.groupOp.visibility = View.VISIBLE
                        }
                        binding.groundConveyPaymentInsert.visibility = View.VISIBLE
                        binding.groundConveyPaymentInsert.setOnClickListener(this)
                    }
                }

                //团组商邀公务
                OASystem.INVITE_DATA -> {
                    if (OASystem.authorization(OASystem.INVITE_DATA, OASystem.VIEW)) {
                        if (binding.groupInviteOfficial.visibility == View.GONE) {
                            binding.groupInviteOfficial.visibility = View.VISIBLE
                        }
                        binding.inviteData.visibility = View.VISIBLE
                        binding.inviteData.setOnClickListener(this)
                    }
                }

                OASystem.INVITE_OFFICIAL_PAYMENT -> {
                    if (OASystem.authorization(OASystem.INVITE_OFFICIAL_PAYMENT, OASystem.VIEW)) {
                        if (binding.groupInviteOfficial.visibility == View.GONE) {
                            binding.groupInviteOfficial.visibility = View.VISIBLE
                        }
                        binding.inviteOfficialPayment.visibility = View.VISIBLE
                        binding.inviteOfficialPayment.setOnClickListener(this)
                    }
                }

                //团组经理主管
                OASystem.GROUP_INFORMATION -> {
                    if (OASystem.authorization(OASystem.GROUP_INFORMATION, OASystem.VIEW)) {
                        if (binding.groupManager.visibility == View.GONE) {
                            binding.groupManager.visibility = View.VISIBLE
                        }
                        binding.groupOperation.visibility = View.VISIBLE
                        binding.groupOperation.setOnClickListener(this)
                    }
                }

                //团组签证
                OASystem.VISA_PAYMENT_INSERT -> {
                    if (OASystem.authorization(OASystem.VISA_PAYMENT_INSERT, OASystem.VIEW)) {
                        if (binding.groupVisa.visibility == View.GONE) {
                            binding.groupVisa.visibility = View.VISIBLE
                        }
                        binding.visaPaymentInsert.visibility = View.VISIBLE
                        binding.visaPaymentInsert.setOnClickListener(this)
                    }
                }

                OASystem.INSURANCE_PAYMENT_INSERT -> {
                    if (OASystem.authorization(OASystem.INSURANCE_PAYMENT_INSERT, OASystem.VIEW)) {
                        if (binding.groupVisa.visibility == View.GONE) {
                            binding.groupVisa.visibility = View.VISIBLE
                        }
                        binding.insurancePaymentInsert.visibility = View.VISIBLE
                        binding.insurancePaymentInsert.setOnClickListener(this)
                    }
                }

                //资源管理 （暂）
                OASystem.MARKET_CUSTOMER -> {
                    if (OASystem.authorization(OASystem.MARKET_CUSTOMER, OASystem.VIEW)) {
                        if (binding.customerResource.visibility == View.GONE) {
                            binding.customerResource.visibility = View.VISIBLE
                        }
                        binding.companyCustomerResource.visibility = View.VISIBLE
                        binding.companyCustomerResource.setOnClickListener(this)
                    }
                }

                //团组机票
                OASystem.THREE_CODE_RESOURCE -> {
                    if (OASystem.authorization(OASystem.THREE_CODE_RESOURCE, OASystem.VIEW)) {
                        if (binding.groupAirplaneTicket.visibility == View.GONE) {
                            binding.groupAirplaneTicket.visibility = View.VISIBLE
                        }
                        binding.airplaneThreeCode.visibility = View.VISIBLE
                        binding.airplaneThreeCode.setOnClickListener(this)
                    }
                }
                OASystem.AIRPLANE_PAYMENT_INSERT -> {
                    if (OASystem.authorization(OASystem.AIRPLANE_PAYMENT_INSERT, OASystem.VIEW)) {
                        if (binding.groupAirplaneTicket.visibility == View.GONE) {
                            binding.groupAirplaneTicket.visibility = View.VISIBLE
                        }
                        binding.airplanePaymentInsert.visibility = View.VISIBLE
                        binding.airplanePaymentInsert.setOnClickListener(this)
                    }
                }
            }
        }
    }

    override fun onClick(view: View) {
        when (view.id) {
            binding.addressBook.id -> {
                val intent = Intent(OASystem.context, AddressBookActivity::class.java)
                startActivity(intent)
            }

            binding.itinerary.id -> {
                val intent = Intent(OASystem.context, ItineraryActivity::class.java)
                startActivity(intent)
            }

            binding.dailyPayment.id -> {
                val intent = Intent(OASystem.context, DailyPaymentListActivity::class.java)
                startActivity(intent)
            }

//            binding.groupPayment.id -> {
//
//            }

            binding.hotelResource.id -> {
                val intent = Intent(OASystem.context, HotelResourceActivity::class.java)
                startActivity(intent)
            }

            binding.hotelPredetermine.id -> {
                val intent = Intent(OASystem.context, HotelPredetermineActivity::class.java)
                startActivity(intent)
            }

            binding.guideResource.id -> {
                val intent = Intent(OASystem.context, GuideResourceActivity::class.java)
                startActivity(intent)
            }

            binding.carResource.id -> {
                val intent = Intent(OASystem.context, CarResourceActivity::class.java)
                startActivity(intent)
            }

            binding.groundConveyPaymentInsert.id -> {
                val intent = Intent(OASystem.context, GroundConveyPaymentInsertActivity::class.java)
                startActivity(intent)
            }

            binding.inviteData.id -> {
                val intent = Intent(OASystem.context, InviteDataActivity::class.java)
                startActivity(intent)
            }

            binding.inviteOfficialPayment.id -> {
                val intent =
                    Intent(OASystem.context, InviteOfficialPaymentInsertActivity::class.java)
                startActivity(intent)
            }

            binding.groupOperation.id -> {
                val intent = Intent(OASystem.context, GroupInfoActivity::class.java)
                startActivity(intent)
            }

            binding.visaPaymentInsert.id -> {
                val intent = Intent(OASystem.context, VisaPaymentInsertActivity::class.java)
                startActivity(intent)
            }

            binding.insurancePaymentInsert.id -> {
                val intent = Intent(OASystem.context, InsurancePaymentInsertActivity::class.java)
                startActivity(intent)
            }

            binding.companyCustomerResource.id -> {
                val intent = Intent(OASystem.context, MarketCustomerActivity::class.java)
                startActivity(intent)
            }

            binding.airplaneThreeCode.id -> {
                val intent = Intent(OASystem.context, AirplaneThreeCodeActivity::class.java)
                startActivity(intent)
            }

            binding.airplanePaymentInsert.id -> {
                val intent = Intent(OASystem.context, AirplaneTicketPaymentActivity::class.java)
                startActivity(intent)
            }
        }
    }

    override fun initEvents() {
        super.initEvents()

        //全展示，汇率换算
        binding.exchangeTool.setOnClickListener {
            val intent = Intent(OASystem.context, ExchangeToolActivity::class.java)
            startActivity(intent)
        }
    }

}