package com.casic.dcms.ui;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.casic.dcms.R;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.databinding.ActivityStreetStatisticsBinding;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author a203
 * @description 街道统计
 * @package com.casic.dcms.ui
 * @date 2022/2/15 09:46
 * @email 290677893@qq.com
 */
public class StreetStatisticsActivity extends AndroidxBaseActivity<ActivityStreetStatisticsBinding> {

    private static final String TAG = "StreetActivity";
    private final Context context = StreetStatisticsActivity.this;
    private final List<String> spinnerEntries = new ArrayList<>();
    private String selectedStreet = "";

    @Override
    protected void setupTopBarLayout() {
        //TODO 这是模拟数据
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < i; j++) {
                str.append("x");
            }
            spinnerEntries.add(str.append("街道").toString());
            str.delete(0, str.length());
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, spinnerEntries);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        int dimension = (int) getResources().getDimension(R.dimen.titleViewHeight);
        viewBinding.titleBarLayout.titleSpinner.setDropDownVerticalOffset(dimension);
        viewBinding.titleBarLayout.titleSpinner.setAdapter(arrayAdapter);
        //默认选中第一条
        selectedStreet = spinnerEntries.get(0);
        viewBinding.titleBarLayout.titleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                //spinner 选中后显示字体的颜色设置
                TextView tv = (TextView) view;
                tv.setTextColor(ContextCompat.getColor(context, R.color.white));
                selectedStreet = spinnerEntries.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    protected void initData() {
        //默认显示当前月的分析数据
        viewBinding.titleBarLayout.dateView.setText(TimeOrDateUtil.timestampToYearMonth(System.currentTimeMillis()));

        Log.d(TAG, "selectedStreet: " + selectedStreet);
    }

    @Override
    protected void initEvent() {
        viewBinding.titleBarLayout.rightImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TimePickerDialog.Builder()
                        .setThemeColor(ContextCompat.getColor(context, R.color.mainThemeColor))
                        .setTitleStringId("选择日期")
                        .setWheelItemTextSize(16)
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis() - Constant.FIVE_YEARS)
                        .setMaxMillseconds(System.currentTimeMillis())
                        .setType(Type.YEAR_MONTH)
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millSeconds) {
                                String date = TimeOrDateUtil.timestampToYearMonth(millSeconds);
                                viewBinding.titleBarLayout.dateView.setText(date);
                            }
                        }).build().show(getSupportFragmentManager(), "year_month");
            }
        });
    }
}
