package com.wd.workoffice.ui.activity.bat.work;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ProPriceBean;
import com.wd.workoffice.bean.ProfitBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkProfitAdapter;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkFinancialProfitActivity extends WorkToolBarActivity {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.tv_dep_name)
    TextView tvDepName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.ll_time)
    LinearLayout llTime;
    @BindView(R.id.tv_month_profit)
    TextView tvMonthProfit;
    @BindView(R.id.tv_cost)
    TextView tvCost;
    @BindView(R.id.tv_sale)
    TextView tvSale;
    private Map<String, Object> param;
    private List<ProfitBean.PageBean.RecordsBean> dataList;
    private WorkProfitAdapter dataAdapter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        tvDepName.setText(UserKeeper.getInstance().getUserDepName());
        dataList = new ArrayList<>();
        dataAdapter = new WorkProfitAdapter(R.layout.item_profit, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        param = WorkUtils.simpleParam();
        param.put("launchDeptId", UserKeeper.getInstance().getUserDepId());
//        getData();
    }

    private void getData() {
        RtfUtils.getRtf().receiveOrderInfo(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                ProfitBean profitBean = JSON.parseObject(data.getData().toString(), ProfitBean.class);
                List<ProfitBean.PageBean.RecordsBean> getList = profitBean.getPage().getRecords();
                dataList.clear();
                dataList.addAll(getList);
                dataAdapter.notifyDataSetChanged();
                dataAdapter.loadMoreComplete();
                tvCost.setText(MathUtils.converData(profitBean.getInnerPrice(),3));
                tvSale.setText(MathUtils.converData(profitBean.getOuterPrice(),3));
                tvMonthProfit.setText(MathUtils.converData(profitBean.getProfitPrice(),3));

            }
        });
    }


    @Override
    protected void initEvent() {
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(WorkFinancialProfitDetailActivity.class,"info",JSON.toJSONString(dataList.get(position)));
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_profit;
    }


    @OnClick(R.id.ll_time)
    public void onViewClicked() {
        PickTimeUtils.showPickTwo(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM"));
                tvTime.setText(time);
                String[] timeArray = time.split("-");
                param.put("year",timeArray[0]);
                param.put("month",timeArray[1]);
                getData();
            }
        }, true).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_rule, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.rule:
                startActivity(WorkFinancialProfitRuleActivity.class);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
