package com.bc_chat.bc_base.ui;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.zhaohaoting.framework.abs.AbsV4Fragment;
import com.zhaohaoting.framework.abs.adapter.TabAdapter;
import com.zhaohaoting.framework.abs.contract.BaseContract;
import com.zhaohaoting.framework.abs.entity.Tag;
import com.zhaohaoting.framework.abs.presenter.AbsActivity;

import java.util.List;


/**
 * author：Created by ZHT on 2017/11/1 11:15
 * email：526309416@qq.com
 * desc：activity基类
 */
public abstract class BaseTabActivity<PRESENTER extends BaseContract.BasePresenter> extends AbsActivity<PRESENTER> {
    protected AppBarLayout appBarLayout;
    protected Toolbar actionBar;
    protected TextView titleView;
    //    public View view;
    public FrameLayout flToolbar;
    protected CoordinatorLayout coordinatorLayout;
    protected TabLayout tabLayout;
    protected View topLine;
    protected ViewPager viewPager;
    protected boolean isCustomTab = false;
    protected List<? extends AbsV4Fragment> fragmentList;
    protected List<? extends Tag> tabNames;

    public Bundle getSavedInstanceState() {
        return savedInstanceState;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.zhaohaoting.framework.R.layout.activity_tab_base);
        actionBar = findViewById(com.zhaohaoting.framework.R.id.toolbar);
        titleView = findViewById(com.zhaohaoting.framework.R.id.title);
        tabLayout = findViewById(com.zhaohaoting.framework.R.id.tabs);
        flToolbar = findViewById(com.zhaohaoting.framework.R.id.fl_toolbar);
        viewPager = findViewById(com.zhaohaoting.framework.R.id.view_pager);
//        flToolbar.setPadding(0, DensityUtil.getStatusBarHeight(this), 0, 0);
        coordinatorLayout = findViewById(com.zhaohaoting.framework.R.id.coordinator_layout);
        topLine = findViewById(com.zhaohaoting.framework.R.id.top_line);
        setSupportActionBar(actionBar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        actionBar.setTitle("");
        actionBar.setNavigationIcon(com.zhaohaoting.framework.R.mipmap.toolbar_back);
        actionBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });
        titleView.setText(getTitle());
        appBarLayout = findViewById(com.zhaohaoting.framework.R.id.appbar_layout);
        initialize();
        tabNames = getTabNames();
        fragmentList = getFragmentList();
        TabAdapter tabAdapter = new TabAdapter(fragmentManager, fragmentList, tabNames);
        viewPager.setAdapter(tabAdapter);
        if (!isCustomTab) {
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setTabsFromPagerAdapter(tabAdapter);
        } else {
            addCustomTab();
            tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    viewPager.setCurrentItem(tab.getPosition());
                }

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

                }

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

                }
            });
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i1) {

                }

                @Override
                public void onPageSelected(int position) {
                    tabLayout.setScrollPosition(position, 0f, true);
                }

                @Override
                public void onPageScrollStateChanged(int i) {

                }
            });
        }
    }

    protected void addCustomTab() {
        for (Tag tag : getTabNames()) {
            tabLayout.addTab(tabLayout.newTab().setCustomView(getTabItemView(tag)).setTag(tag.getId()));
        }

        int code = 0;
        if(code == 0){

        }
    }

    protected abstract View getTabItemView(Tag tag);

    protected abstract void initialize();

    protected abstract List<? extends Tag> getTabNames();

    protected abstract List<? extends AbsV4Fragment> getFragmentList();

}
