package com.android.chmo.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.MyWalletResponse;
import com.android.chmo.http.service.UserService;
import com.android.chmo.model.WalletRecord;
import com.android.chmo.ui.activity.me.RechargeActivity;
import com.android.chmo.ui.adpater.MyWalletAdapter;
import com.android.chmo.ui.dialog.PickMonthDialog;
import com.bigkoo.pickerview.configure.PickerOptions;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyWalletActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener, PickMonthDialog.OnPickYearMonthListener, OnTimeSelectListener {
    @BindView(R.id.tv_coin)
    TextView           tvCoin;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView       recyclerView;

    private MyWalletAdapter    adapter;
    private List<WalletRecord> data;
    private int                page = 1;
    private int                year;
    private int                month;
    private TimePickerView     timePicker;

    @Override
    protected int getContentResId() {
        return R.layout.activity_my_wallet;
    }

    @Override
    public void initView() {
        refreshLayout.setRefreshHeader(new WaterDropHeader(getContext()));
        refreshLayout.setPrimaryColorsId(R.color.pink);
        ClassicsFooter footer = new ClassicsFooter(getContext());
        footer.setTextSizeTitle(12);
        refreshLayout.setRefreshFooter(footer);
        refreshLayout.setNoMoreData(true);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        data = new ArrayList<>();
        adapter = new MyWalletAdapter(data, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;

        PickerOptions pickerOptions = new PickerOptions(PickerOptions.TYPE_PICKER_TIME);
        pickerOptions.context = this;
        pickerOptions.textContentConfirm = "确定";
        pickerOptions.textContentCancel = "取消";
        pickerOptions.textColorConfirm = this.getResources().getColor(R.color.pink);
        pickerOptions.textColorCancel = this.getResources().getColor(R.color.gray);
        pickerOptions.type = new boolean[]{true, true, false, false, false, false};
        Calendar start = Calendar.getInstance();
        start.set(Calendar.YEAR, start.get(Calendar.YEAR) - 5);
        start.set(Calendar.MONTH, 0);
        pickerOptions.startDate = start;
        pickerOptions.endDate = Calendar.getInstance();
        pickerOptions.textSizeContent = 16;
        pickerOptions.timeSelectListener = this;
        timePicker = new TimePickerView(pickerOptions);
        timePicker.setDate(Calendar.getInstance());

        getData();
    }

    void getData() {
        UserService.myWallet(year, month, page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
                MyWalletResponse response = new Gson().fromJson(result, MyWalletResponse.class);
                if ("success".equals(response.getMsg())) {
                    tvCoin.setText(String.valueOf(response.getAllcoin()));
                    if (page == 1) {
                        data.clear();
                    }
                    data.addAll(response.getData().getData());
                    adapter.notifyDataSetChanged();
                    if (response.getData().getCount() == data.size() || response.getData().getCount() == 0) {
                        refreshLayout.setNoMoreData(true);
                    }
                }
            }

            @Override
            public void onFailure(String error) {

            }
        });
    }

    @Override
    protected int getStatusBarStyle() {
        return STATUS_BAR_STYLE_LIGHT_CONTENT;
    }

    @OnClick({R.id.btn_recharge, R.id.iv_calendar})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recharge:
                openPage(RechargeActivity.class);
                break;
            case R.id.iv_calendar:
                timePicker.show();
                break;
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        page++;
        getData();
    }

    @Override
    public void onPickYearMonth(int year, int month) {
        this.year = year;
        this.month = month;
        refreshLayout.autoRefresh();
    }

    @Override
    public void onTimeSelect(Date date, View v) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        this.year = calendar.get(Calendar.YEAR);
        this.month = calendar.get(Calendar.MONTH) + 1;
        refreshLayout.autoRefresh();
    }
}
