package com.sjkj.appthreefloor_tsgz.activity;

import android.graphics.drawable.Drawable;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.tabs.TabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.activity.viewModel.EventCenterViewModel;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityEventDetailsBinding;
import com.sjkj.appthreefloor_tsgz.fragment.EventDetailsFragment;
import com.sjkj.appthreefloor_tsgz.fragment.EventMessageFragment;
import com.sjkj.appthreefloor_tsgz.fragment.EventNearByFragment;
import com.sjkj.base_lib.app.AppManager;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class EventDetailsActivity extends BaseActivity {
    private EventCenterViewModel viewModel;
    private ActivityEventDetailsBinding binding;
    private boolean isauthorization = false;
    private String[] fragmentNames = new String[]{"事件", "消息", "附近"};
    private List<Fragment> fragmentArrayList = new ArrayList<>();
    private EventDetailsFragment eventDetailsFragment = new EventDetailsFragment();
    private EventMessageFragment eventMessageFragment = new EventMessageFragment();
    private EventNearByFragment eventNearByFragment = new EventNearByFragment();

    private void requestPermissionsReadAndWrite() {
        XXPermissions.with(AppManager.getAppManager().currentActivity())
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            isauthorization = true;
                        } else {
                            ToastUtils.show("获取部分权限成功，但部分权限未正常授予");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.show("被永久拒绝授权，请手动授予权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(AppManager.getAppManager().currentActivity(), permissions);
                        } else {
                            ToastUtils.show("获取权限失败");
                        }
                    }
                });
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_event_details;
    }

    @Override
    public void initBinding() {
        binding = (ActivityEventDetailsBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(EventCenterViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }


    @Override
    public void initListener() {
        super.initListener();
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                viewModel.eventStatus.set(tab.getText().toString());
//                viewModel.eventStatusValue.set(tab.getTag().toString());
                binding.viewPager.setCurrentItem(tab.getPosition());
                if (tab.getText().toString().equals("事件")) {
                    getTitleView().setText("事件详情");
                } else {
                    getTitleView().setText(tab.getText().toString());
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }



    @Override
    public void initView() {
        super.initView();
        ImmersionBar.with(EventDetailsActivity.this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(R.color.white)
                .init();
        requestPermissionsReadAndWrite();

        // viewModel.eventCode.set(getIntent().getStringExtra("eventCode"));
        viewModel.eventId.set(getIntent().getStringExtra("id"));
        getTitleView().setText("事件详情");
        getTitleView().setTextColor(getResources().getColor(R.color.black));
        int drawableLeft = R.drawable.icon_back; // 替换为你的drawable资源ID
        Drawable drawable = ContextCompat.getDrawable(EventDetailsActivity.this, drawableLeft);
        if (drawable != null) {
            getBackView().setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null); // 使用Relative版本以支持RTL布局方向
        }
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.white));

        fragmentArrayList.add(eventDetailsFragment);
        fragmentArrayList.add(eventMessageFragment);
        fragmentArrayList.add(eventNearByFragment);
        binding.viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentArrayList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return fragmentNames[position];
            }
        });

        binding.tabLayout.setupWithViewPager(binding.viewPager);
    }

}
