package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kfzs.duanduan.BaseCompatFragment;
import com.kfzs.duanduan.mine.GiftpackListAdapter;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.TaskAuditEntity;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.functions.Action1;

/**
 * Created by ljy on 2018/8/20.
 */

public class FgtMainAudit extends BaseCompatFragment {
    @BindView(R.id.img_baseactivity_title)
    ImageView imgBaseactivityTitle;
    @BindView(R.id.txt_baseactivity_title)
    TextView txtBaseactivityTitle;
    @BindView(R.id.number_tv)
    TextView numberTv;
    @BindView(R.id.audit_total_complete_tv)
    TextView auditTotalCompleteTv;
    @BindView(R.id.audit_total_money_tv)
    TextView auditTotalMoneyTv;
    @BindView(R.id.audit_total_welfare_tv)
    TextView auditTotalWelfareTv;
    @BindView(R.id.indicator)
    TabLayout indicator;
    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.my_money_dingxiang_amount_iv)
    ImageView my_money_dingxiang_amount_iv;
    Unbinder unbinder;
    private Activity activity;

    public static final String[] tabNames = {"审核中", "任务成功", "任务失败"};
    private GiftpackListAdapter mAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.act_audit_layout);
        activity = getActivity();

        unbinder = ButterKnife.bind(this, mContentView);
//        StatusBarUtils.setTranslucent(this);
        TitleBarUtils.getInstance()
                .setTitle(mContentView, "审核中心")
                .setTitleListen(mContentView, 0, null);

        mAdapter = new GiftpackListAdapter(getChildFragmentManager(), activity.getApplicationContext());
        mAdapter.add(FgtAudit.newInstance(1), tabNames[0]);
        mAdapter.add(FgtAudit.newInstance(2), tabNames[1]);
        mAdapter.add(FgtAudit.newInstance(3), tabNames[2]);
        pager.setAdapter(mAdapter);
        new_tab();

        my_money_dingxiang_amount_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ViewUtil.showOrienteeringDetails(activity);
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void initData() {

        CommonUtil.getInstance().staticAudit(new Action1<BaseMessage>() {
            @Override
            public void call(BaseMessage o) {
                try {
                    TaskAuditEntity taskAuditEntity = o.getData(TaskAuditEntity.class);
                    if (taskAuditEntity != null) {
                        String audit_total_audit = String.format(activity.getResources().getString(R.string.audit_total_audit), taskAuditEntity.getAudit_count());
                        String audit_total_complete = String.format(activity.getResources().getString(R.string.audit_total_complete), taskAuditEntity.getFinish_count());
                        String audit_total_money = String.format(activity.getResources().getString(R.string.audit_total_money), taskAuditEntity.getFinish_cash());
                        String audit_total_welfare = String.format(activity.getResources().getString(R.string.audit_total_welfare), taskAuditEntity.getWelfare_award());

                        if (numberTv == null)
                            return;
                        numberTv.setText(audit_total_audit + "");
                        auditTotalCompleteTv.setText(audit_total_complete + "");
                        auditTotalMoneyTv.setText(audit_total_money + "");
                        auditTotalWelfareTv.setText(audit_total_welfare + "");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void new_tab() {
//        indicator.newTab();
        pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(indicator));
        indicator.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager));
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
//                MY_MONEY_TAB.onEvent("tab_name", tabNames[2]);
//                for(int d=0;d<indicator.getTabCount();d++){
//                    TabLayout.Tab tab=indicator.getTabAt(i);
//                    if(i == d){
//
//                        ((TextView) tab.getCustomView().findViewById(R.id.tv_text)).setTextColor(Color.parseColor("#34a6e7"));
//                    }else {
//
//                        ((TextView) tab.getCustomView().findViewById(R.id.tv_text)).setTextColor(Color.parseColor("#333333"));
//                    }
//                }

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        indicator.setupWithViewPager(pager);
        CommonUtil.getInstance().reflex(indicator, activity);
//        for (int i = 0; i < indicator.getTabCount(); i++) {
//            indicator.getTabAt(i).setCustomView(mAdapter.getTabView(i));
//        }

     /*   TabLayout.Tab tab=indicator.getTabAt(2);

            tab.getCustomView().findViewById(R.id.iv_img).setVisibility(View.VISIBLE);*/

    }


    @Override
    public void onResume() {
        super.onResume();

        try {
            initData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

