package com.dskj.trustlend;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.azhon.basic.base.BaseActivity;
import com.dskj.trustlend.api.Api;
import com.dskj.trustlend.api.BaseObserver;
import com.dskj.trustlend.api.Result;
import com.dskj.trustlend.bean.UserBean;
import com.dskj.trustlend.databinding.ActivityHomeBinding;
import com.dskj.trustlend.databinding.ActivityMainBinding;
import com.dskj.trustlend.dialog.FaqsDialog;
import com.dskj.trustlend.fragment.FaqsFragment;
import com.dskj.trustlend.fragment.HomeFragment;
import com.dskj.trustlend.fragment.IndexFragment;
import com.dskj.trustlend.fragment.MeFragment;
import com.dskj.trustlend.fragment.StatusFragment;
import com.dskj.trustlend.home.MainViewModel;
import com.dskj.trustlend.utils.GsonUtils;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

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

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity  extends BaseActivity<MainViewModel, ActivityHomeBinding> {
    private List<Fragment> mList;

    private HomeFragment homeFragment;
//    private FaqsFragment faqsFragment;
//    private IndexFragment indexFragment;
    private StatusFragment statusFragment;
    private MeFragment meFragment;
    @Override
    protected int initLayout() {
        return R.layout.activity_home;
    }

    @SuppressLint("WrongConstant")
    @Override
    protected void initView() {
//        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

//        setStatusBarColorW(this,R.color.color_f7e9ff,false);
        //显示Navigation完整的item的图标和文字
        dataBinding.navView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
//        adjustNavigationIcoSize(dataBinding.navView);
        getWindow().setNavigationBarColor(getColor(R.color.color_f7e9ff));
        initNav();
        initList();
        dataBinding.helpV.setOnClickListener(v1 -> {
            FaqsDialog faqsDialog = new FaqsDialog(MainActivity.this);
            faqsDialog.setRightButton(v -> {
                faqsDialog.dismiss();
            });
            faqsDialog.show();
        });
        getUserInfo();
    }

    private void getUserInfo() {
        Api.getInstance().userProfile1()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Result>() {

                    @Override
                    public void onSuccess(Result feedbackResp) {

                    }

                    @Override
                    public void onError(int code, String msg) {

                    }
                });
    }
    /**
     * 和ViewPager2联动
     */

    private void initNav() {
        dataBinding.navView.setItemIconTintList(null);

        dataBinding.navView.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.navigation_home:
                    //smoothScroll 置false是为了取消动画
                    if(dataBinding.viewPager.getCurrentItem() !=0) {
                        dataBinding.viewPager.setCurrentItem(0, false);
                    }
                    break;

                case R.id.navigation_attack:
                    if(dataBinding.viewPager.getCurrentItem() !=1) {
                        dataBinding.viewPager.setCurrentItem(1, false);
                    }

                    break;
                case R.id.navigation_me:
                    if(dataBinding.viewPager.getCurrentItem() !=2) {
                        dataBinding.viewPager.setCurrentItem(2,false);
                    }
                    break;
            }
            return true;
        });
    }


    /***
     * viewpager的事件操作
     */
    private void initList() {
        mList=new ArrayList<>();
        mList.add(homeFragment=new HomeFragment());
//        mList.add(faqsFragment=new FaqsFragment());
//        mList.add(indexFragment=new IndexFragment());
        mList.add(statusFragment=new StatusFragment());
        mList.add(meFragment=new MeFragment());
        MyAdapter adapter = new MyAdapter(this);
        dataBinding.viewPager.setAdapter(adapter);
        dataBinding.viewPager.setOffscreenPageLimit(3);
        dataBinding.viewPager.setUserInputEnabled(false);


    }


    /**
     * viewpager的适配器
     */
    public class MyAdapter extends FragmentStateAdapter {

        public MyAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @Override
        public Fragment createFragment(int position) {

            return mList.get(position);
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }


    @Override
    protected void initData() {

    }

    @Override
    protected MainViewModel initViewModel() {
        return ViewModelProviders.of(this).get(MainViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }
}