package com.danding.yang800;

import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.danding.yang800.Adapter.AbstractWheelTextAdapter;
import com.danding.yang800.Adapter.ArrayWheelAdapter;
import com.danding.yang800.Entity.AddressInfo;
import com.danding.yang800.Presenter.EditMarketActivityPresenter;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.View.WheelView;
import com.danding.yang800.listener.OnWheelChangedListener;
import com.yanzhenjie.sofia.Sofia;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditMarketActivity extends BaseActivity implements
        OnWheelChangedListener {
    @BindView(R.id.editMarket_getTimeLayout)
    LinearLayout editMarket_getTimeLayout;
    @BindView(R.id.editMarket_backTv)
    TextView editMarket_backTv;
    @BindView(R.id.editMarket_getTimeTv)
    TextView editMarket_getTimeTv;
    @BindView(R.id.editMarket_useStartTimeTv)
    TextView editMarket_useStartTimeTv;
    @BindView(R.id.editMarket_useEndTimeTv)
    TextView editMarket_useEndTimeTv;
    @BindView(R.id.editMarket_useTimeLayout)
    LinearLayout editMarket_useTimeLayout;
    @BindView(R.id.editMarket_saveTv)
    TextView editMarket_saveTv;
    @BindView(R.id.editMarket_useConditionLayout)
    LinearLayout editMarket_useConditionLayout;
    @BindView(R.id.editMarket_useConditionTv)
    TextView editMarket_useConditionTv;
    @BindView(R.id.editMarket_feeEdit)
    EditText editMarket_feeEdit;
    @BindView(R.id.editMarket_useConditionEdit)
    EditText editMarket_useConditionEdit;
    @BindView(R.id.editMarket_quantityEdit)
    EditText editMarket_quantityEdit;
    @BindView(R.id.editMarket_nameEdit)
    EditText editMarket_nameEdit;
    @BindView(R.id.editMarket_getStartTimeTv)
    TextView editMarket_getStartTimeTv;
    private PopupWindow popupWindow;
    private WheelView mViewYear;
    private WheelView mViewMonth;
    private WheelView mViewDate;
    int yearArr[] = {2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026};
    int monthArr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    int year;
    int month;
    String date;
    String dateData[];
    boolean end = false;
    private int curYear;
    private int curMonth;
    int curDate;
    EditMarketActivityPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_edit_market);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        presenter = new EditMarketActivityPresenter(this);
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        curDate = c.get(Calendar.DATE);
        editMarket_getStartTimeTv.setText(curYear + "-" + curMonth + "-" + curDate);
    }


    @OnClick({R.id.editMarket_backTv, R.id.editMarket_getTimeTv, R.id.editMarket_useStartTimeTv
            , R.id.editMarket_useEndTimeTv, R.id.editMarket_saveTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.editMarket_backTv:
                finish();
                break;
            case R.id.editMarket_getTimeTv:
                AbstractWheelTextAdapter.DEFAULT_TEXT_SIZE = 18;
                showWindow(1);
                break;
            case R.id.editMarket_useStartTimeTv:
                AbstractWheelTextAdapter.DEFAULT_TEXT_SIZE = 18;
                showWindow(2);
                break;
            case R.id.editMarket_useEndTimeTv:
                AbstractWheelTextAdapter.DEFAULT_TEXT_SIZE = 18;
                showWindow(3);
                break;
            case R.id.editMarket_saveTv:
//                boolean a = StringUtil.isDateOneBigger(editMarket_useStartTimeTv.getText().toString(), editMarket_useEndTimeTv.getText().toString());
                presenter.create(sp.getString("storeId", ""), editMarket_nameEdit.getText().toString().trim(), "0", "0", editMarket_feeEdit.getText().toString().trim(),
                        editMarket_useConditionEdit.getText().toString().trim(), editMarket_quantityEdit.getText().toString().trim(),
                        StringUtil.getcurrentTime(), editMarket_getTimeTv.getText().toString(), editMarket_useStartTimeTv.getText()
                                .toString(), editMarket_useEndTimeTv.getText().toString());
                break;
        }
    }

    private void showWindow(final int type) {
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.popupwindows_address, null);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
        mViewYear = (WheelView) contentView.findViewById(R.id.id_province);
        mViewMonth = (WheelView) contentView.findViewById(R.id.id_city);
        mViewDate = (WheelView) contentView.findViewById(R.id.id_district);
        TextView choiceaddress_tvsure = (TextView) contentView
                .findViewById(R.id.choiceaddress_tvsure);
        TextView choiceaddress_tv = (TextView) contentView
                .findViewById(R.id.choiceaddress_tv);
        choiceaddress_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        choiceaddress_tvsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 1) {
                    editMarket_getTimeTv.setText(year + "-" + month + "-" + date);
                } else if (type == 2) {
                    editMarket_useStartTimeTv.setText(year + "-" + month + "-" + date);
                } else if (type == 3) {
                    editMarket_useEndTimeTv.setText(year + "-" + month + "-" + date);
                }
                popupWindow.dismiss();
            }
        });
        setUpListener(curYear, curMonth, curDate);
    }

    private void setUpListener(int curYear, int curMonth, int curDate) {
        mViewYear.addChangingListener(this);
        mViewMonth.addChangingListener(this);
        mViewDate.addChangingListener(this);
        initYear(curYear);
        updateMonth(curMonth);
        initDate(curDate);
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewYear) {
            int pCurrent = mViewYear.getCurrentItem();
            year = yearArr[pCurrent];
            updateDate();
        } else if (wheel == mViewMonth) {
            //  ToastUtil.show(this, arraycityList.size() + "s");
            int pCurrent = mViewMonth.getCurrentItem();
            month = monthArr[pCurrent];
            if (mViewDate.getCurrentItem() + 1 == dateData.length) {
                end = true;
            } else {
                end = false;
            }
            updateDate();
        } else if (wheel == mViewDate) {
            int pCurrent = mViewDate.getCurrentItem();
            date = pCurrent + 1 + "";
            LogUtil.d(date);
        }
    }

    private void initYear(int curYear) {
        int j = 999;
        String yearData[] = new String[yearArr.length];
        for (int i = 0; i < yearArr.length; i++) {
            yearData[i] = yearArr[i] + "";
            if (curYear == yearArr[i]) {
                j = i;
            }
        }
        mViewYear.setViewAdapter(new ArrayWheelAdapter<>(
                EditMarketActivity.this, yearData));
        mViewYear.setCyclic(true);
        mViewYear.setCurrentItem(j);
        year = yearArr[j];
    }

    private void initDate(int curDate) {
        int j = 999;
        int dateLenth = GeneralUtil.getDay(year, month);
        if (dateData == null) {
            dateData = new String[dateLenth];
        } else {
            dateData = null;
            dateData = new String[dateLenth];
        }
        for (int i = 0; i < dateLenth; i++) {
            dateData[i] = String.format("%02d", i + 1).toString();
            if (curDate == i + 1) {
                j = i;
            }
        }
        mViewDate.setViewAdapter(new ArrayWheelAdapter<>(this, dateData));
        if (end) {
            mViewDate.setCurrentItem(dateData.length - 1);
        }
        mViewDate.setCyclic(true);
        mViewDate.setCurrentItem(j);
        int pCurrent = mViewDate.getCurrentItem();
        date = pCurrent + 1 + "";
    }

    private void updateMonth(int curMonth) {
        int j = 999;
        String monthData[] = new String[monthArr.length];
        for (int i = 0; i < monthArr.length; i++) {
            monthData[i] = String.format("%02d", monthArr[i]).toString();
            if (curMonth == monthArr[i]) {
                j = i;
            }
        }
        mViewMonth.setViewAdapter(new ArrayWheelAdapter<>(
                EditMarketActivity.this, monthData));
        mViewMonth.setCyclic(true);
        mViewMonth.setCurrentItem(j);
        int pCurrent = mViewMonth.getCurrentItem();
        month = monthArr[pCurrent];
    }

    private void updateDate() {
        int dateLenth = GeneralUtil.getDay(year, month);
        if (dateData == null) {
            dateData = new String[dateLenth];
        } else {
            dateData = null;
            dateData = new String[dateLenth];
        }
        for (int i = 0; i < dateLenth; i++) {
            dateData[i] = String.format("%02d", i + 1).toString();
        }
        mViewDate.setViewAdapter(new ArrayWheelAdapter<>(this, dateData));
        if (end) {
            mViewDate.setCurrentItem(dateData.length - 1);
        }
        mViewDate.setCyclic(true);
        int pCurrent = mViewDate.getCurrentItem();
        date = pCurrent + 1 + "";
    }


}
