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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sheep.gamegroup.event.BigEvent;
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.DataUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.activity.ActMain;
import com.sheep.gamegroup.view.adapter.TitleFragmentListAdapter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

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

public class FgtMainAudit extends BaseFragment {
    @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;
    private Activity activity;

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

    public static FgtMainAudit newInstance(int index) {
        FgtMainAudit fgtMainAudit = new FgtMainAudit();
        Bundle bundle = new Bundle();
        DataUtil.putObject(bundle, index);
        fgtMainAudit.setArguments(bundle);
        return fgtMainAudit;
    }

    public FgtMainAudit() {
    }
    private Integer curPosition = 0;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            Integer position = DataUtil.getObject(bundle, Integer.class);
            if(position != null){
                curPosition = position;
            }
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_audit_layout;
    }

    @Override
    public void onViewCreated() {
        activity = getActivity();
        EventBus.getDefault().register(this);
        if(activity instanceof ActMain){
            TitleBarUtils.getInstance()
                    .setTitle(getActivity(), "审核中心")
                    .setTitleListen(getActivity(), 0, null);
        } else {
            TitleBarUtils.getInstance()
                    .setTitle(getActivity(), "审核中心")
                    .setTitleFinish(getActivity());
        }

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

                ViewUtil.showOrienteeringDetails(activity);
            }
        });
        initView();

    }

    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() {
        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) {
                curPosition = i;

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        indicator.setupWithViewPager(pager);
        CommonUtil.getInstance().reflex(indicator, activity);
        pager.setCurrentItem(curPosition);
    }
    public static final int POSITION_ACTIVITY = 0;
    public static final int POSITION_SUCCESS = 1;
    public static final int POSITION_FAILED = 2;
    protected void initView() {
        mAdapter = new TitleFragmentListAdapter(getChildFragmentManager());
        mAdapter.add(FgtAudit.newInstance(POSITION_ACTIVITY + 1), tabNames[POSITION_ACTIVITY]);
        mAdapter.add(FgtAudit.newInstance(POSITION_SUCCESS + 1), tabNames[POSITION_SUCCESS]);
        mAdapter.add(FgtAudit.newInstance(POSITION_FAILED + 1), tabNames[POSITION_FAILED]);
        FgtAudit fgtAudit = (FgtAudit) mAdapter.getItem(curPosition);
        if(fgtAudit != null){
            fgtAudit.setFirstPage(true);
        }

        pager.setAdapter(mAdapter);
        new_tab();
    }

    @Override
    public void onResume() {
        super.onResume();
        try {
            initData();
        } catch (Exception e) {
            e.printStackTrace();
            G.showToast(e.getMessage());
        }
    }

    public void setCurrentItem(int item, boolean smoothScroll) {
        if(pager != null)
            pager.setCurrentItem(item, smoothScroll);
    }
    @Subscribe
    public void onEventMainThread(BigEvent event) {
        switch (event.getEventTypes()) {
            case REFRESH_AUDITACTIVITY_DATA:
                try {
                    initData();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

