package cn.net.caoyao.cpzshandset.ui.dialog;

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

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.City;
import cn.net.caoyao.cpzshandset.service.bll.CityBll;
import cn.net.caoyao.cpzshandset.ui.adapter.ChoiceSaleAddressAdapter;

public class ChoiceAddressDialog extends Dialog implements android.view.View.OnClickListener, OnItemSelectedListener {
	private ChoiceCity choiceCity;
	private View btn_dialog_choice_address_cancle, btn_dialog_choice_address_sure;
	private City currentCity;
	private Spinner sp_province, sp_city, sp_area;
	private List<City> provinces = new ArrayList<>();
	private List<City> citys = new ArrayList<>();
	private List<City> areas = new ArrayList<>();
	private ChoiceSaleAddressAdapter provinceAdapter;
	private ChoiceSaleAddressAdapter cityAdapter;
	private ChoiceSaleAddressAdapter areaAdapter;
	private CityBll cityBll;
	private City countryCity = new City("全国", 0);
	private boolean hasCountry = true;
	private boolean mustChoiceArea = false;

	public ChoiceAddressDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public ChoiceAddressDialog(Context context, int theme) {
		super(context, R.style.MyNoBgDialog);
	}

	public ChoiceAddressDialog(Context context, ChoiceCity choiceCity) {
		super(context, R.style.MyNoBgDialog);
		this.choiceCity = choiceCity;
		cityBll = new CityBll(context);
	}

	public void setCountryStatus(boolean hasCountry, boolean mustChoiceArea) {
		this.hasCountry = hasCountry;
		this.mustChoiceArea = mustChoiceArea;
		/*
		 * if (!hasCountry) { provinceAdapter.setCountryArea(hasCountry);
		 * cityAdapter.setCountryArea(hasCountry);
		 * areaAdapter.setCountryArea(hasCountry);
		 * provinceAdapter.refreshData(provinces);
		 * cityAdapter.refreshData(citys); areaAdapter.refreshData(areas); }
		 */

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initDialogStyle();
		setContentView(R.layout.dialog_choice_sales_area);
		initView();
		setListener();
		initData();
	}

	private void initData() {
		provinces = cityBll.queryProvince();

		provinceAdapter = new ChoiceSaleAddressAdapter(provinces, 1, hasCountry);
		cityAdapter = new ChoiceSaleAddressAdapter(citys, 2, hasCountry);
		areaAdapter = new ChoiceSaleAddressAdapter(areas, 3, hasCountry);
		sp_province.setAdapter(provinceAdapter);
		sp_city.setAdapter(cityAdapter);
		sp_area.setAdapter(areaAdapter);
		refreshNoCountryProvince(0);
		refreshNoCountryCity(0);
	}

	public void refreshProvinceAdapter(List<City> provinces) {
		this.provinces = provinces;
		provinceAdapter.refreshData(provinces);
		sp_province.setSelection(0);
	}

	private void setListener() {
		btn_dialog_choice_address_cancle.setOnClickListener(this);
		btn_dialog_choice_address_sure.setOnClickListener(this);
		sp_province.setOnItemSelectedListener(this);
		sp_city.setOnItemSelectedListener(this);
		sp_area.setOnItemSelectedListener(this);
	}

	private void initView() {
		btn_dialog_choice_address_cancle = findViewById(R.id.btn_dialog_choice_address_cancle);
		btn_dialog_choice_address_sure = findViewById(R.id.btn_dialog_choice_address_sure);
		sp_province = (Spinner) findViewById(R.id.sp_province);
		sp_city = (Spinner) findViewById(R.id.sp_city);
		sp_area = (Spinner) findViewById(R.id.sp_area);
	}

	private void initDialogStyle() {
		Window win = this.getWindow();
		win.setBackgroundDrawableResource(R.color.transparent);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);
		win.setGravity(Gravity.CENTER);
		this.setCanceledOnTouchOutside(false);
	}

	@Override
	public void dismiss() {
		try {
			if (this != null && this.isShowing()) {
				super.dismiss();
			}
		} catch (Exception e) {
		}
	}

	@Override
	public void show() {
//	if (provinces.size()!=0){
        super.show();
//    }
		// initData();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_dialog_choice_address_cancle:
			dismiss();
			break;

		case R.id.btn_dialog_choice_address_sure:
			choiceCity.setCity(currentCity);
			dismiss();
			break;
		}
	}

	public interface ChoiceCity {
		void setCity(City city);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		switch (parent.getId()) {
		case R.id.sp_province:
			if (hasCountry) {
				refreshHasCountryProvince(position);
			} else {
				refreshNoCountryProvince(position);
			}
			break;
		case R.id.sp_city:
			if (hasCountry) {
				refreshHasCountryCity(position);
			} else {
				refreshNoCountryCity(position);
			}

			break;
		case R.id.sp_area:
			if (hasCountry) {
				refreshHasCountryArea(position);
			} else {
				refreshNoCountryArea(position);
			}

			break;
		}

	}

	private void refreshNoCountryArea(int position) {
		currentCity = areas.get(position);
	}

	private void refreshHasCountryArea(int position) {
		if (position > 0) {
			currentCity = areas.get(position - 1);
		} else {
			int cityPos = sp_city.getSelectedItemPosition();
			if (cityPos > 0) {
				currentCity = citys.get(cityPos - 1);
			}
		}
	}

	private void refreshNoCountryCity(int position) {
		try {
            currentCity = citys.get(position);
            areas = cityBll.queryCityByCity(currentCity);
            if (areas != null && areas.size() > 0) {
                currentCity = areas.get(0);
            }
            areaAdapter.refreshData(areas);
            sp_area.setSelection(0);
        }catch (Exception e){
		    e.printStackTrace();
        }
	}

	private void refreshHasCountryCity(int position) {
		if (position > 0) {
			currentCity = citys.get(position - 1);
			areas = cityBll.queryCityByCity(currentCity);
			areaAdapter.refreshData(areas);
			sp_area.setSelection(0);
		} else {
			int provincePos = sp_province.getSelectedItemPosition();
			if (provincePos > 0) {
				currentCity = provinces.get(sp_province.getSelectedItemPosition() - 1);
			}
			areas = new ArrayList<>();
			areaAdapter.refreshData(areas);
			sp_area.setSelection(0);
		}
	}

	private void refreshNoCountryProvince(int position) {
	    try {
            currentCity = provinces.get(position);
            citys = cityBll.queryCityByProvince(currentCity);
            cityAdapter.refreshData(citys);
            areas = new ArrayList<>();
            if (citys != null && citys.size() > 0) {
                areas = cityBll.queryCityByCity(citys.get(0));
            }

            if (areas != null && areas.size() > 0) {
                currentCity = areas.get(0);
            }
            areaAdapter.refreshData(areas);
            sp_city.setSelection(0);
            sp_area.setSelection(0);
        }catch (Exception e){
	        e.printStackTrace();
        }

	}

	private void refreshHasCountryProvince(int position) {
		if (position > 0) {
			currentCity = provinces.get(position - 1);
			citys = cityBll.queryCityByProvince(currentCity);
			cityAdapter.refreshData(citys);
			areas = new ArrayList<>();
			areaAdapter.refreshData(areas);
			sp_city.setSelection(0);
			sp_area.setSelection(0);
		} else {
			currentCity = countryCity;
			citys = new ArrayList<>();
			areas = new ArrayList<>();
			cityAdapter.refreshData(citys);
			areaAdapter.refreshData(areas);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub

	}

}
