package com.sheep.gamegroup.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.view.fragment.FgtMyMoney1;
import com.sheep.gamegroup.view.fragment.FgtMyMoney2;
import com.sheep.gamegroup.view.fragment.FgtMyMoney3;
import com.sheep.gamegroup.view.adapter.TitleFragmentListAdapter;
import com.kfzs.duanduan.utils.StatusBarUtils;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserAssets;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.afinal.simplecache.ApiKey;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.MY_MONEY_TAB;

/**
 * 我的资产
 * Created by realicing on 2018/5/8.
 */

public class ActMyMoney extends BaseActivity {

    @BindView(R.id.indicator)
    TabLayout indicator;
    @BindView(R.id.pager)
    ViewPager pager;

    @BindView(R.id.my_money_total_money)
    TextView my_money_total_money;
    @BindView(R.id.my_money_task_reward)
    TextView my_money_task_reward;
    @BindView(R.id.my_money_agent_extract)
    TextView my_money_agent_extract;
    @BindView(R.id.my_money_recharge_amount)
    TextView my_money_recharge_amount;
    @BindView(R.id.my_money_available_amount)
    TextView my_money_available_amount;
    @BindView(R.id.my_money_dingxiang_amount)
    TextView my_money_dingxiang_amount;
    @BindView(R.id.my_money_dingxiang_amount_iv)
    ImageView my_money_dingxiang_amount_iv;

    private TitleFragmentListAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTranslucent(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_my_money;
    }
    public static final String[] tabNames = {"充值记录", "提现记录", "收支明细"};
    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setTitle(this,"我的资产")
                .setTitleFinish(this);
        mAdapter = new TitleFragmentListAdapter(getSupportFragmentManager());
        mAdapter.add(new FgtMyMoney1(), tabNames[0]);
        mAdapter.add(new FgtMyMoney2(), tabNames[1]);
        mAdapter.add(new FgtMyMoney3(), tabNames[2]);
        pager.setAdapter(mAdapter);
        new_tab();
    }
    private void new_tab(){
        indicator.addTab(indicator.newTab().setCustomView(tab_icon(tabNames[0], true)));
        indicator.addTab(indicator.newTab().setCustomView(tab_icon(tabNames[1], true)));
        indicator.addTab(indicator.newTab().setCustomView(tab_icon(tabNames[2], false)));
        //Tablayout自定义view绑定ViewPager
        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]);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    private View tab_icon(String name,boolean isShow){
        View newtab =  LayoutInflater.from(this).inflate(R.layout.tab_item,null);
        TextView tv = newtab.findViewById(R.id.tabtext);
        View tab_line = newtab.findViewById(R.id.tab_line);
        tab_line.setVisibility(isShow ? View.VISIBLE: View.INVISIBLE);
        tv.setText(name);
        return newtab;
    }

    public TitleFragmentListAdapter getmAdapter() {
        return mAdapter;
    }

    @Override
    public void initListener() {

    }
    @OnClick({ R.id.my_money_withdraw_tv, R.id.my_money_recharge_tv, R.id.my_money_dingxiang_amount_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_money_withdraw_tv://提现
                Jump2View.getInstance().tryGoWithdrawal(ActMyMoney.this, null);
                break;
            case R.id.my_money_recharge_tv://充值
                Jump2View.getInstance().goRechargeAct(ActMyMoney.this, "我的资产");
                break;
            case R.id.my_money_dingxiang_amount_iv://定向货币详情
                ViewUtil.showOrienteeringDetails(ActMyMoney.this);
                break;
        }
    }
    @Override
    public void initData() {
        showProgress();
        resetData();
        SheepApp.getInstance().getNetComponent().getApiService().getMyAssets()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(this) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                boolean isNewData = DataUtil.getInstance().isNewData(ApiKey.MY_ASSETS);
                                if(isNewData){
                                    CommonUtil.getInstance().updateUserInfo(null);
                                }
                                UserAssets userAssets = baseMessage.getData(UserAssets.class);
                                loadData(userAssets);
                                hideProgress();
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                G.showToast(baseMessage);
                                resetData();
                                hideProgress();
                            }
                        });
    }

    private void loadData(UserAssets userAssets) {
        if(my_money_total_money == null){
            return;
        }
        my_money_total_money.setText(getString(R.string.total_money, userAssets.getSheep_bi_total()));
        my_money_task_reward.setText(getString(R.string.task_reward, userAssets.getTask_reward()));
        my_money_agent_extract.setText(getString(R.string.agent_extract, userAssets.getExtension_extract()));
        my_money_recharge_amount.setText(getString(R.string.recharge_amount, userAssets.getRecharge_amount()));
        my_money_available_amount.setText(getString(R.string.available_amount, NumberFormatUtils.retainMost2(userAssets.getCash_withdrawal())));
        my_money_dingxiang_amount.setText(getString(R.string.dingxiang_amount, NumberFormatUtils.retainMost2(userAssets.getDirectional_currency())));
    }
    private void resetData() {
        if(my_money_total_money == null){
            return;
        }
        my_money_total_money.setText(getString(R.string.total_money, "0.00"));
        my_money_task_reward.setText(getString(R.string.task_reward, "0.00"));
        my_money_agent_extract.setText(getString(R.string.agent_extract, "0.00"));
        my_money_recharge_amount.setText(getString(R.string.recharge_amount, "0.00"));
        my_money_available_amount.setText(getString(R.string.available_amount, "0.00"));
        my_money_dingxiang_amount.setText(getString(R.string.dingxiang_amount, "0.00"));
    }

}
