package com.pan_american.android.ui.workspace

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
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.CustomAlertDialog
import com.pan_american.android.databinding.FragmentWorkspaceBinding
import com.pan_american.android.ui.customer_resource.company_customer.MarketCustomerActivity
import com.pan_american.android.ui.customer_resource.market_sales_revenue.MarketSalesRevenueActivity
import com.pan_american.android.ui.customer_resource.related_invitee.RelatedInviteeActivity
import com.pan_american.android.ui.efficiency_tools.address_book.AddressBookActivity
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.financial_module.accounts_receivable_report.AccountsReceivableReportActivity
import com.pan_american.android.ui.financial_module.collection_bill.CollectionBillActivity
import com.pan_american.android.ui.financial_module.daily_payment.DailyPaymentListActivity
import com.pan_american.android.ui.financial_module.expense_approval.ExpenseApprovalActivity
import com.pan_american.android.ui.financial_module.payment_refund.PaymentRefundActivity
import com.pan_american.android.ui.group_airplane_ticket.airplane_payment_insert.AirplaneTicketPaymentActivity
import com.pan_american.android.ui.group_common.bill_management.BillManagementActivity
import com.pan_american.android.ui.group_common.commission_confirm.CommissionConfirmActivity
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_invite_official.official_visits.InviteVisitsActivity
import com.pan_american.android.ui.group_management.entry_and_exit_fee_detail.EntryAndExitFeeGroupListActivity
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.insurance_payment_insert.InsurancePaymentInsertActivity
import com.pan_american.android.ui.group_visa.visa_payment_insert.VisaPaymentInsertActivity
import com.pan_american.android.ui.login.LoginActivity
import com.pan_american.android.ui.personnel_module.materials_operate.MaterialOperateActivity
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

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

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        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.GROUP_PAYMENT -> {
//                    binding.groupPayment.visibility = View.VISIBLE
//                    binding.groupPayment.setOnClickListener(this)
//                }

                //财务模块
                OASystem.DAILY_PAYMENT -> {
                    if (OASystem.authorization(OASystem.DAILY_PAYMENT, OASystem.VIEW)) {
                        if (binding.financialModule.visibility == View.GONE) {
                            binding.financialModule.visibility = View.VISIBLE
                        }
                        binding.dailyPayment.visibility = View.VISIBLE
                        binding.dailyPayment.setOnClickListener(this)
                    }
                }

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

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

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

                //人事模块
                OASystem.MATERIAL_APPLICATION -> {
                    if (OASystem.authorization(OASystem.MATERIAL_APPLICATION, OASystem.VIEW)) {
                        if (binding.personnelModule.visibility == View.GONE) {
                            binding.personnelModule.visibility = View.VISIBLE
                        }
                        binding.materialOperate.visibility = View.VISIBLE
                        binding.materialOperate.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.OFFICIAL_VISITS -> {
                    if (OASystem.authorization(OASystem.OFFICIAL_VISITS, OASystem.VIEW)) {
                        if (binding.groupInviteOfficial.visibility == View.GONE) {
                            binding.groupInviteOfficial.visibility = View.VISIBLE
                        }
                        binding.officialVisits.visibility = View.VISIBLE
                        binding.officialVisits.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.ENTRY_AND_EXIT_FEE_DETAIL -> {
                    if (OASystem.authorization(OASystem.ENTRY_AND_EXIT_FEE_DETAIL, OASystem.VIEW)) {
                        if (binding.groupManager.visibility == View.GONE) {
                            binding.groupManager.visibility = View.VISIBLE
                        }
                        binding.entryAndExitFeeDetail.visibility = View.VISIBLE
                        binding.entryAndExitFeeDetail.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.MARKET_SALES_REVENUE -> {
                    if (OASystem.authorization(OASystem.MARKET_SALES_REVENUE, OASystem.VIEW)) {
                        if (binding.customerResource.visibility == View.GONE) {
                            binding.customerResource.visibility = View.VISIBLE
                        }
                        binding.marketSalesRevenue.visibility = View.VISIBLE
                        binding.marketSalesRevenue.setOnClickListener(this)
                    }
                }

                OASystem.RELATED_INVITEE -> {
                    if (OASystem.authorization(OASystem.RELATED_INVITEE, OASystem.VIEW)) {
                        if (binding.customerResource.visibility == View.GONE) {
                            binding.customerResource.visibility = View.VISIBLE
                        }
                        binding.relatedInvitee.visibility = View.VISIBLE
                        binding.relatedInvitee.setOnClickListener(this)
                    }
                }

                //团组公共模块
                OASystem.BILL_MANAGEMENT -> {
                    if (OASystem.authorization(OASystem.BILL_MANAGEMENT, OASystem.VIEW)) {
                        if (binding.groupCommonModel.visibility == View.GONE) {
                            binding.groupCommonModel.visibility = View.VISIBLE
                        }
                        binding.billManagement.visibility = View.VISIBLE
                        binding.billManagement.setOnClickListener(this)
                    }
                }

                OASystem.COMMISSION_CONFIRM -> {
                    if (OASystem.authorization(OASystem.BILL_MANAGEMENT, OASystem.VIEW)) {
                        if (binding.groupCommonModel.visibility == View.GONE) {
                            binding.groupCommonModel.visibility = View.VISIBLE
                        }
                        binding.commissionConfirm.visibility = View.VISIBLE
                        binding.commissionConfirm.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)
                    }
                }
            }
        }

        if (OASystem.userInfo.userId == 21 || OASystem.userInfo.userId == 22) {
            if (binding.financialModule.visibility == View.GONE) {
                binding.financialModule.visibility = View.VISIBLE
            }
            binding.expenseApproval.visibility = View.VISIBLE
            binding.expenseApproval.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.accountReceivableReport.id -> {
                val intent = Intent(OASystem.context, AccountsReceivableReportActivity::class.java)
                startActivity(intent)
            }

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

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

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

            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.officialVisits.id -> {
                val intent = Intent(OASystem.context, InviteVisitsActivity::class.java)
                startActivity(intent)
            }

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

            binding.entryAndExitFeeDetail.id -> {
                val intent = Intent(OASystem.context, EntryAndExitFeeGroupListActivity::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.marketSalesRevenue.id -> {
                val intent = Intent(OASystem.context, MarketSalesRevenueActivity::class.java)
                startActivity(intent)
            }

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

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

            binding.commissionConfirm.id -> {
                val intent = Intent(OASystem.context, CommissionConfirmActivity::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.userIcon.setOnClickListener {

            val userMenuPopView = View.inflate(OASystem.context, R.layout.popup_user_menu, null)
            popupWindow = PopupWindow(userMenuPopView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)

            showPopupWindow {

                val logoutView = userMenuPopView.findViewById<TextView>(R.id.logout)

                logoutView.setOnClickListener {

                    CustomAlertDialog.Builder().apply {
                        setTitle(resources.getString(R.string.alert))
                        setMessage(resources.getString(R.string.logout_confirm_hint))
                        setNegativeButtonAndListener(resources.getString(R.string.cancel)) { dialog, _ ->
                            dialog.dismiss()
                        }
                        setPositiveButtonAndListener(resources.getString(R.string.confirm)) { dialog, _ ->

                            dialog.dismiss()

                            val intent = Intent(OASystem.context, LoginActivity::class.java)

                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)

                            startActivity(intent)
                        }
                    }.show()

                }

                popupWindow.showAtLocation(binding.root, Gravity.BOTTOM, 0, 0)
            }
        }

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

        binding.expenseApproval.setOnClickListener {
            val intent = Intent(OASystem.context, ExpenseApprovalActivity::class.java)
            startActivity(intent)
        }
    }

}