package com.xwkj.jcjcgz.mvp.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xwkj.jcjcgz.R;
import com.xwkj.jcjcgz.di.component.DaggerMainComponent;
import com.xwkj.jcjcgz.mvp.contract.MainContract;
import com.xwkj.jcjcgz.mvp.presenter.MainPresenter;
import com.xwkj.jcjcgz.mvp.ui.fragment.GrzxFragment;
import com.xwkj.jcjcgz.mvp.ui.fragment.GztFragment;
import com.xwkj.jcjcgz.mvp.ui.fragment.RwzxFragment;
import com.xwkj.jcjcgz.mvp.ui.fragment.Test1Fragment;
import com.zkjc.common.base.ZkjcBaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/09/2024 10:24
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MainActivity extends ZkjcBaseActivity<MainPresenter> implements MainContract.View {

    @BindView(R.id.fl_container)
    FrameLayout fl_container;
    @BindView(R.id.ptsy)
    FrameLayout ptsy;
    @BindView(R.id.xqgk)
    FrameLayout xqgk;
    @BindView(R.id.grzx)
    FrameLayout grzx;
    @BindView(R.id.ll_main_tab)
   public LinearLayout ll_main_tab;
    private Test1Fragment homeFragment;
    private GztFragment xqGkFragment;
    private GrzxFragment myFragment;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setListener();
        onClickListener.onClick(ll_main_tab.getChildAt(0));
    }



    private void setListener() {
        //所有孩子的数量
        int childCount = ll_main_tab.getChildCount();
        System.out.println("孩子的数量+" + childCount);
        for (int i = 0; i < childCount; i++) {
            FrameLayout childAt = (FrameLayout) ll_main_tab.getChildAt(i);
            childAt.setOnClickListener(onClickListener);
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = 0;
            index = ll_main_tab.indexOfChild(v);

            //修改底部tab的ui
            changeUi(index);
            changeFragment(index);
        }
    };

    private void changeFragment(int index) {
        if (index == 0) {
            setTabSelection(0);
        } else if (index == 1) {
            setTabSelection(1);
        } else if (index == 2) {
            setTabSelection(2);
        }
    }

    /**
     * 修改底部tab的按键
     * g改变index对应的孩子的状态 包括这个孩子控件中的多有控件的状态（可用 不可用）
     *
     * @param index
     */
    public void changeUi(int index) {
        //Toast.makeText(this,index+"",Toast.LENGTH_SHORT).show();

        int childCount = ll_main_tab.getChildCount();
        for (int i = 0; i < childCount; i++) {
            //判断i是否与index相同
            //相同不可用状态enable=FALSE
            if (i == index) {
                ll_main_tab.getChildAt(i).setEnabled(false);
                //每一个item控件都需要切换状态
                setEnable(ll_main_tab.getChildAt(i), false);
            } else {
                ll_main_tab.getChildAt(i).setEnabled(true);
                //每一个item控件都需要切换状态
                setEnable(ll_main_tab.getChildAt(i), true);
            }
        }

    }

    /**
     * 将每个item的控件一起改变状态
     *
     * @param item
     * @param b
     */
    private void setEnable(View item, boolean b) {
        item.setEnabled(b);
        if (item instanceof ViewGroup) {
            int childCount = ((ViewGroup) item).getChildCount();
            for (int i = 0; i < childCount; i++) {
                setEnable(((ViewGroup) item).getChildAt(i), b);
            }
        }
    }

    @OnClick({R.id.ptsy, R.id.xqgk, R.id.grzx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ptsy:
                setTabSelection(0);
                break;
            case R.id.xqgk:
                setTabSelection(1);
                break;
            case R.id.grzx:
                setTabSelection(2);
                break;
        }
    }

    public void setTabSelection(int index) {

        // 开启一个Fragment事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，
        hideFragments(transaction);
        switch (index) {


            case 0:


                if (homeFragment == null) {
                    homeFragment = new Test1Fragment();
                    transaction.add(R.id.fl_container, homeFragment);

                } else {
                    transaction.show(homeFragment);
                }


                break;

            case 1:
               // ToastUtils.show("功能开发中");
                // warningCenterFragment = (WarningCenterFragment) getSupportFragmentManager().findFragmentByTag(WarningCenterFragment.TAG);
                if (xqGkFragment == null) {
                    xqGkFragment = new GztFragment();
                    transaction.add(R.id.fl_container, xqGkFragment);

                } else {
                    transaction.show(xqGkFragment);
                }
                break;
            case 2:
                // warningCenterFragment = (WarningCenterFragment) getSupportFragmentManager().findFragmentByTag(WarningCenterFragment.TAG);
                if (myFragment == null) {
                    myFragment = new GrzxFragment();
                    transaction.add(R.id.fl_container, myFragment);

                } else {
                    transaction.show(myFragment);
                }
                break;

        }
        transaction.commit();
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {

        //  super.onSaveInstanceState(outState);
    }

    /**
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {

        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (xqGkFragment != null) {
            transaction.hide(xqGkFragment);
        }

        if (myFragment != null) {
            transaction.hide(myFragment);
        }


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

}
