package com.gh.gamecenter.libao;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckedTextView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.gh.gamecenter.ConcernActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.base.fragment.BaseFragment_TabLayout;
import com.gh.gamecenter.common.eventbus.EBReuse;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.common.utils.SensorsBridge;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.gamecenter.databinding.FragmentLibaoWrapperBinding;
import com.gh.gamecenter.eventbus.EBUISwitch;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * @author CsHeng
 * @Date 17/06/2017
 * @Time 10:30 AM
 */

public class LibaoFragment extends BaseFragment_TabLayout {

    public static final String EB_LIBAOACTIVITY_TAG = "LibaoActivity";
    public static final String LIBAO_OPENPAGE = "openPage";
    public static final String LIBAO_CLOSEPAGE = "closePage";

    private MenuItem mManageMenu;

    private FragmentLibaoWrapperBinding mBinding;
    private Libao1Fragment mLibao1Fragment;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_libao_wrapper;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBinding = FragmentLibaoWrapperBinding.bind(view);
    }

    @Override
    protected void initFragmentList(List<Fragment> fragments) {
        mLibao1Fragment = new Libao1Fragment();
        fragments.add(mLibao1Fragment);
        fragments.add(new Libao2Fragment());
        fragments.add(new Libao3Fragment());
    }

    @Override
    protected void initTabTitleList(List<String> tabTitleList) {
        tabTitleList.add(getString(R.string.libao_newest));
        tabTitleList.add(getString(R.string.libao_concern));
        tabTitleList.add(getString(R.string.libao_chunhaoxiang));
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Activity) {
            ((Activity) context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!mIsFromTabWrapper) {
            setNavigationTitle(R.string.title_libao);
            initMenu(R.menu.menu_manage);
            mManageMenu = getItemMenu(R.id.layout_menu_manage);
            ((TextView) mManageMenu.getActionView().findViewById(R.id.manageTv)).setText("管理");
            mManageMenu.getActionView().findViewById(R.id.manageTv).setVisibility(View.GONE);
        }
    }

    @Override
    public void onMenuItemClick(MenuItem menuItem) {
        super.onMenuItemClick(menuItem);
        if (menuItem.getItemId() == R.id.layout_menu_manage) {
            Intent intent = ConcernActivity.getIntent(getContext(), mEntrance + "+(礼包中心:关注)");
            startActivityForResult(intent, Libao2Fragment.CONCERN_CHANGE_REQUEST);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.postDelayed(() -> EventBus.getDefault().post(new EBUISwitch(EB_LIBAOACTIVITY_TAG, 0)), 100);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBUISwitch busNine) {
        if (EB_LIBAOACTIVITY_TAG.equals(busNine.getFrom())) {
            if (busNine.getPosition() == 1) {
                EventBus.getDefault().post(new EBUISwitch(EB_LIBAOACTIVITY_TAG, mCheckedIndex));
            }
        }
    }

    // 页面切换事件（查看历史礼包的页面）
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBReuse reuse) {
        if (LIBAO_OPENPAGE.equals(reuse.getType())) {
            mBinding.fragmentTabRl.setVisibility(View.GONE);
            mBinding.libaoHistoryTitle.setVisibility(View.VISIBLE);
            mBinding.libaoHistoryTitle.setTextColor(ContextCompat.getColor(getContext(), com.gh.gamecenter.common.R.color.hint));
            mViewPager.setScrollable(false);
        } else if (LIBAO_CLOSEPAGE.equals(reuse.getType())) {
            mBinding.fragmentTabRl.setVisibility(View.VISIBLE);
            mBinding.libaoHistoryTitle.setVisibility(View.GONE);
            mViewPager.setScrollable(true);
        }
    }

    @Override
    public void onPageSelected(int position) {
        MtaHelper.onEvent("我的光环_新", "礼包中心", mTabTitleList.get(position) + "Tab");
        SensorsBridge.trackEvent("GameGiftCenterTabSelected", "tab_name", mTabTitleList.get(position));
        EventBus.getDefault().post(new EBUISwitch(EB_LIBAOACTIVITY_TAG, position));
        if (!mIsFromTabWrapper) {
            mManageMenu.getActionView().findViewById(R.id.manageTv).setVisibility(position == 1 ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void onTouchEvent(MotionEvent event) {
        if (mLibao1Fragment != null) mLibao1Fragment.onTouchEvent(event);
    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        mBinding.fragmentTabRl.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_surface));
        mBinding.divider.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_divider));
        ExtensionsKt.updateStatusBarColor(requireActivity(), com.gh.gamecenter.common.R.color.black, com.gh.gamecenter.common.R.color.white);
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if (tab == null || tab.getCustomView() == null) continue;
            View tabTitle = tab.getCustomView().findViewById(R.id.tab_title);
            if (tabTitle instanceof CheckedTextView) {
                ((CheckedTextView) tabTitle).setTextColor(ContextCompat.getColor(requireContext(), tab.isSelected() ? com.gh.gamecenter.common.R.color.text_theme : com.gh.gamecenter.common.R.color.text_secondary));
            }
        }
    }
}
