package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerWithdrawalComponent;
import com.sheep.gamegroup.di.modules.WithdrawalModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GridViewEntity;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.model.entity.WithdrawalList;
import com.sheep.gamegroup.presenter.WithdrawalContract;
import com.sheep.gamegroup.presenter.WithdrawalPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.WithdrawalAdapter;
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 java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 提现
 * Created by ljy on 2018/3/18.
 */

public class WithdrawalAct extends BaseActivity implements WithdrawalContract.View {
    @BindView(R.id.withdrawal_greidview)
    GridView withdrawalGreidview;
    @BindView(R.id.withdrawal_sure)
    TextView withdrawalSure;
    @BindView(R.id.weixin_name_tv)
    TextView weixin_name_tv;
    @BindView(R.id.withdrawal_click_bindname_tv)
    TextView withdrawal_click_bindname_tv;

    Activity activity;
    @Inject
    WithdrawalPresenter presenter;

    private List<WithdrawalList> wlLists = new ArrayList<>();

    private WithdrawalAdapter adapter;
    private int select;
    private String amount;
    private float balanceAmount;//余额

    private  UserEntity userEntity;
    @Override
    protected int getLayoutId() {
        return R.layout.withdrawal_layout;
    }

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils
                .getInstance()
                .setShowOrHide(this, true)
                .setTitle(activity, "提现")
                .setTitleFinish(activity);
        DaggerWithdrawalComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .withdrawalModule(new WithdrawalModule(this))
                .build()
                .inject(this);
        adapter = new WithdrawalAdapter(activity, wlLists);
        withdrawalGreidview.setVerticalSpacing(25);
        withdrawalGreidview.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        withdrawalGreidview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                select = position;
                for(int i=0;i<wlLists.size();i++){
                    if(position == i){
                        wlLists.get(i).getGridViewEntity().setSelectState(true);
                    }else {
                        wlLists.get(i).getGridViewEntity().setSelectState(false);

                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
        showProgress();
        presenter.getCanWithdrawAmount();
        presenter.getWithdrawAmountOption();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        mInitData();
    }

    public void mInitData() {
        userEntity = DataUtil.getInstance().getUserEntity();
        if(userEntity != null){
            ViewUtil.setText(weixin_name_tv, userEntity.getWx_nickname());
        }
        setBalanceAmount();
    }

    @OnClick({R.id.withdrawal_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.withdrawal_sure:
                doWithdrawal();
                break;
        }
    }

    private void doWithdrawal() {
        UMConfigUtils.Event.SHEEP_TIXIAN_COMMIT.onEvent();
        if(ListUtil.hasIndex(wlLists, select)) {
            amount = wlLists.get(select).getGridViewEntity().getValue();
            if (TextUtils.isEmpty(amount)) {
                G.showToast("请选择金额");
                return;
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("amount", Float.valueOf(amount));
            jsonObject.put("type", 1);
            showProgress();
            presenter.goWithdrawal(jsonObject);
        } else {
            G.showToast(R.string.loading_data);
        }
    }


    @Override
    public void failData(Object o) {
        hideProgress();
        if(o instanceof BaseMessage)
            G.showToast((BaseMessage) o);
    }

    @Override
    public void updateView(Object o) {
        hideProgress();
        CommonUtil.getInstance().updateUserInfo(null);
        presenter.getCanWithdrawAmount();
        Jump2View.getInstance().goWithdrawalResultview(activity, NumberFormatUtils.parseFloat(amount));
    }

    @Override
    public void failBalanceData(BaseMessage o) {
        hideProgress();
        G.showToast(o.getMsg()+"");
    }

    @Override
    public void updateBalanceView(BaseMessage baseMessage) {

        hideProgress();
        balanceAmount = baseMessage.getData(Float.class);
        setBalanceAmount();
    }

    private void setBalanceAmount(){

        String sr = "可提现金额 <font color='#ff2d4b'><big>" + NumberFormatUtils.retainMost2(balanceAmount) + "</big></font> 元";
        withdrawal_click_bindname_tv.setText(Html.fromHtml(sr));
    }

    @Override
    public void failWithdrawAmountOptionData(BaseMessage o) {
        G.showToast(o.getMsg()+"");
    }

    @Override
    public void updateWithdrawAmountOptionView(BaseMessage o) {
        try{
            List<WithdrawalList> withdrawalLists = JSONArray.parseArray(JSONArray.toJSONString(o.getData()), WithdrawalList.class);
            wlLists.clear();
            for(int i=0;i< withdrawalLists.size(); i++){
                WithdrawalList withdrawalList = withdrawalLists.get(i);
                if(withdrawalList.getType() != 2){
                    GridViewEntity gridViewEntity = new GridViewEntity();
                    if(i == 0){
                        gridViewEntity.setSelectState(true);
                    }else {
                        gridViewEntity.setSelectState(false);
                    }
                    gridViewEntity.setValue(withdrawalList.getAmount()+"");
                    withdrawalList.setGridViewEntity(gridViewEntity);

                    wlLists.add(withdrawalList);
                }
            }
            adapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
