package com.zhilin.patrol.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.zhilin.patrol.R;
import com.zhilin.patrol.adapter.FragmentAdapter;
import com.zhilin.patrol.bean.LoginEntity;
import com.zhilin.patrol.bean.MyTaskEntity;
import com.zhilin.patrol.bean.PatrolRecordNumEntity;
import com.zhilin.patrol.http.HttpUrl;
import com.zhilin.patrol.tool.DialogUtil;
import com.zhilin.patrol.tool.IpUtils;
import com.zhilin.patrol.tool.JsonUtils;
import com.zhilin.patrol.ui.fragment.PatrolRecordFragment;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;
import q.rorbin.badgeview.QBadgeView;

/**
 * 巡查记录
 */
public class PatrolRecordActivity extends BaseActivity {

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    private ArrayList<String> mListTitle;
    private ArrayList<Fragment> mListFragment;
    private FragmentAdapter mAdapter;
    private LoginEntity.InfoListBean.UserInfoBean mUserInfo;
    private int nNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patrol_record);
        mUserInfo = (LoginEntity.InfoListBean.UserInfoBean) getIntent().getSerializableExtra("userInfo");
        nNum=getIntent().getIntExtra("num",0);
        ButterKnife.bind(this);
        setTitle("巡查记录");
//        request();
        init(null);
    }

    @Override
    public void request() {
        showLoadingView();
        String ipAddress = IpUtils.getIpAddress(this);
        OkHttpUtils.post()
                .url(HttpUrl.TASK_STATISTICS_URL)
                .addParams("userCode", mUserInfo.getUserCode())
                .addParams("userRole", mUserInfo.getUserRole() + "")
                .addParams("userIP", ipAddress)
                .build().execute(new Callback<PatrolRecordNumEntity>() {
            @Override
            public PatrolRecordNumEntity parseNetworkResponse(Response response, int id) throws Exception {
                String jsonStr = response.body().string();
                return (PatrolRecordNumEntity) JsonUtils.parseJson2Obj(jsonStr, PatrolRecordNumEntity.class);
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                showFailView();
                DialogUtil.showNetFailDialog(PatrolRecordActivity.this);
            }

            @Override
            public void onResponse(PatrolRecordNumEntity entity, int id) {
                showContentView();
                if (entity == null) {
                    DialogUtil.showErrDialog(PatrolRecordActivity.this, "返回信息为空");
                    return;
                }
                PatrolRecordNumEntity.InfoListBean list = entity.getInfoList();
                if (!"1000".endsWith(entity.getReturnCode())) {
                    DialogUtil.showErrDialog(PatrolRecordActivity.this, entity.getReturnMsg());
                    return;
                }
                init(entity.getInfoList());
            }
        });
    }

    private void init(PatrolRecordNumEntity.InfoListBean infoList) {
        if(nNum==123){
            mListTitle = new ArrayList<>();
            mListTitle.add("待审核");
            mListTitle.add("不通过");
            mListTitle.add("已通过");

            mTabLayout.setTabMode(TabLayout.MODE_FIXED);

            mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(0)));
            mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(1)));
            mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(2)));

            mListFragment = new ArrayList<>();

            mListFragment.add(PatrolRecordFragment.newInstance(-1));
            mListFragment.add(PatrolRecordFragment.newInstance(0));
            mListFragment.add(PatrolRecordFragment.newInstance(1));
            mAdapter = new FragmentAdapter(getSupportFragmentManager(), this, mListFragment, mListTitle);
            //设置适配器
            mViewPager.setAdapter(mAdapter);
            mViewPager.setOffscreenPageLimit(3);
            //tab加载viewPager
            mTabLayout.setupWithViewPager(mViewPager);
            if (infoList == null) return;
            QBadgeView qBadgeView = new QBadgeView(this);
            QBadgeView qBadgeView2 = new QBadgeView(this);
            int passIngNum = infoList.getPassIngNum();
            if (passIngNum != 0) {
                qBadgeView.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(0)).setBadgeNumber(passIngNum);
            }
            int unPassNum = infoList.getUnPassNum();
            if (unPassNum != 0) {
                qBadgeView2.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(1)).setBadgeNumber(unPassNum);
            }
        }
        if(nNum==1){
            mListTitle = new ArrayList<>();
            mListTitle.add("待审核");

            mTabLayout.setTabMode(TabLayout.MODE_FIXED);

            mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(0)));


            mListFragment = new ArrayList<>();

            mListFragment.add(PatrolRecordFragment.newInstance(-1));

            mAdapter = new FragmentAdapter(getSupportFragmentManager(), this, mListFragment, mListTitle);
            //设置适配器
            mViewPager.setAdapter(mAdapter);
            mViewPager.setOffscreenPageLimit(1);
            //tab加载viewPager
            mTabLayout.setupWithViewPager(mViewPager);
            if (infoList == null) return;
            QBadgeView qBadgeView = new QBadgeView(this);
            QBadgeView qBadgeView2 = new QBadgeView(this);
            int passIngNum = infoList.getPassIngNum();
            if (passIngNum != 0) {
                qBadgeView.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(0)).setBadgeNumber(passIngNum);
            }
            int unPassNum = infoList.getUnPassNum();
            if (unPassNum != 0) {
                qBadgeView2.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(1)).setBadgeNumber(unPassNum);
            }
        }
        if(nNum==2){
            mListTitle = new ArrayList<>();
            mListTitle.add("不通过");
            mTabLayout.setTabMode(TabLayout.MODE_FIXED);

            mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(0)));

            mListFragment = new ArrayList<>();

            mListFragment.add(PatrolRecordFragment.newInstance(0));
            mAdapter = new FragmentAdapter(getSupportFragmentManager(), this, mListFragment, mListTitle);
            //设置适配器
            mViewPager.setAdapter(mAdapter);
            mViewPager.setOffscreenPageLimit(1);
            //tab加载viewPager
            mTabLayout.setupWithViewPager(mViewPager);
            if (infoList == null) return;
            QBadgeView qBadgeView = new QBadgeView(this);
            QBadgeView qBadgeView2 = new QBadgeView(this);
            int passIngNum = infoList.getPassIngNum();
            if (passIngNum != 0) {
                qBadgeView.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(0)).setBadgeNumber(passIngNum);
            }
            int unPassNum = infoList.getUnPassNum();
            if (unPassNum != 0) {
                qBadgeView2.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(1)).setBadgeNumber(unPassNum);
            }
        }
        if(nNum==3){
            mListTitle = new ArrayList<>();
            mListTitle.add("已通过");

            mTabLayout.setTabMode(TabLayout.MODE_FIXED);

            mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(0)));

            mListFragment = new ArrayList<>();

            mListFragment.add(PatrolRecordFragment.newInstance(1));
            mAdapter = new FragmentAdapter(getSupportFragmentManager(), this, mListFragment, mListTitle);
            //设置适配器
            mViewPager.setAdapter(mAdapter);
            mViewPager.setOffscreenPageLimit(1);
            //tab加载viewPager
            mTabLayout.setupWithViewPager(mViewPager);
            if (infoList == null) return;
            QBadgeView qBadgeView = new QBadgeView(this);
            QBadgeView qBadgeView2 = new QBadgeView(this);
            int passIngNum = infoList.getPassIngNum();
            if (passIngNum != 0) {
                qBadgeView.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(0)).setBadgeNumber(passIngNum);
            }
            int unPassNum = infoList.getUnPassNum();
            if (unPassNum != 0) {
                qBadgeView2.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(1)).setBadgeNumber(unPassNum);
            }
        }
        /*mListTitle = new ArrayList<>();
        mListTitle.add("待审核");
        mListTitle.add("不通过");
        mListTitle.add("已通过");

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);

        mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(0)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(1)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mListTitle.get(2)));

        mListFragment = new ArrayList<>();

        mListFragment.add(PatrolRecordFragment.newInstance(-1));
        mListFragment.add(PatrolRecordFragment.newInstance(0));
        mListFragment.add(PatrolRecordFragment.newInstance(1));
        mAdapter = new FragmentAdapter(getSupportFragmentManager(), this, mListFragment, mListTitle);

        //设置适配器
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(3);
        //tab加载viewPager
        mTabLayout.setupWithViewPager(mViewPager);
        if (infoList == null) return;
        QBadgeView qBadgeView = new QBadgeView(this);
        QBadgeView qBadgeView2 = new QBadgeView(this);
        int passIngNum = infoList.getPassIngNum();
        if (passIngNum != 0) {
            qBadgeView.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(0)).setBadgeNumber(passIngNum);
        }
        int unPassNum = infoList.getUnPassNum();
        if (unPassNum != 0) {
            qBadgeView2.bindTarget(((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(1)).setBadgeNumber(unPassNum);
        }*/
    }

}
