package kr.co.hanwha.hifive.admin.design;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.SpinnerData;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by sanghoonkwon on 2018. 4. 3..
 */

public class TestFontActivity extends BaseActivity {
    private TextView mTextViewSizeFont;
    private Spinner mSpinner;
    private FontColorSpinnerAdapter mAdapter;
    private TextView mTextViewTest;
    private EditText mEditTextTest;
    private ImageButton mImageButtonBold;

    private ArrayList<SpinnerData> mListSpinner;
    private int mFontSize = 13;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_test_font;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return BaseToolbarType.ONLY_BACK;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.RIGHT;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextViewSizeFont = (TextView) findViewById(R.id.sizeFont_textView);
        findViewById(R.id.minusFont_textView).setOnClickListener(onClickListener);
        findViewById(R.id.plusFont_textView).setOnClickListener(onClickListener);

        mImageButtonBold = (ImageButton) findViewById(R.id.bold_imageButton);
        mImageButtonBold.setOnClickListener(onClickListener);

        mAdapter = new FontColorSpinnerAdapter(this);
        mSpinner = (Spinner) findViewById(R.id.spinner);
        mSpinner.setAdapter(mAdapter);
        mSpinner.setOnItemSelectedListener(onItemSelectedListener);

        //Initialize spinner data
        mListSpinner = new ArrayList<>();
        mListSpinner.add(new SpinnerData("GREY01", R.color.hf_grey01));
        mListSpinner.add(new SpinnerData("GREY02", R.color.hf_grey02));
        mListSpinner.add(new SpinnerData("GREY03", R.color.hf_grey03));
        mListSpinner.add(new SpinnerData("GREY04", R.color.hf_grey04));
        mListSpinner.add(new SpinnerData("GREY05", R.color.hf_grey05));
        mListSpinner.add(new SpinnerData("GREY06", R.color.hf_grey06));

        mAdapter.addItems(mListSpinner);
        mAdapter.notifyDataSetChanged();

        mTextViewTest = (TextView) findViewById(R.id.test_textView);
        mEditTextTest = (EditText) findViewById(R.id.test_editText);
        mEditTextTest.addTextChangedListener(textWatcher);

        setFontSize(mFontSize);
        setTextBold();
        setText(mEditTextTest.getText().toString());
    }

    /**************************************************
     * Support methods
     **************************************************/
    public void setFontColor(int color) {
        mTextViewTest.setTextColor(color);
    }

    private void setFontSize(int sizeSp) {
        mTextViewSizeFont.setText(Integer.toString(sizeSp));
//        mTextViewTest.setTextSize(HFCommonUtil.getPixelFromSp(this, sizeSp));
        mTextViewTest.setTextSize(sizeSp);
    }

    private void setText(String text) {
        mTextViewTest.setText(text);
    }

    private void setTextBold() {
        Typeface typeface;
        if (mImageButtonBold.isSelected()) {
            typeface = Typeface.create(mTextViewTest.getTypeface(), Typeface.BOLD);
        } else {
            typeface = Typeface.create(mTextViewTest.getTypeface(), Typeface.NORMAL);
        }
        mTextViewTest.setTypeface(typeface);
    }

    /**************************************************
     * Support methods
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.minusFont_textView:
                    mFontSize -= 1;
                    if (mFontSize < 1) mFontSize = 1;
                    setFontSize(mFontSize);
                    break;
                case R.id.plusFont_textView:
                    mFontSize += 1;
                    setFontSize(mFontSize);
                    break;
                case R.id.bold_imageButton:
                    mImageButtonBold.setSelected(!mImageButtonBold.isSelected());
                    setTextBold();
                    break;
            }
        }
    };

    /**************************************************
     * TextWatcher
     **************************************************/
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            setText(s.toString());
        }
    };

    /**************************************************
     * AdapterView.OnItemSelectedListener
     **************************************************/
    AdapterView.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (mAdapter == null) return;

            int color = HFCommonUtil.getColor(TestFontActivity.this, (int) mListSpinner.get(position).getValue());
            setFontColor(color);
        }

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

        }
    };

    /**************************************************
     * BaseAdapter
     **************************************************/
    public class FontColorSpinnerAdapter extends BaseAdapter {
        private Context mContext = null;
        private LayoutInflater mInflater = null;
        private ArrayList<SpinnerData> mListData = new ArrayList<>();

        public FontColorSpinnerAdapter(Context context) {
            this.mContext = context;
            this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void clear() {
            mListData.clear();
        }

        public void addItem(SpinnerData item) {
            mListData.add(item);
        }

        public void addItems(ArrayList<SpinnerData> list) {
            if (mListData == null) mListData = new ArrayList<>();
            else mListData.clear();

            mListData.addAll(list);
        }

        @Override
        public int getCount() {
            return mListData.size();
        }

        @Override
        public Object getItem(int position) {
            return mListData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getDropDownView(int position, View view, ViewGroup parent) {
            if (view == null || !view.getTag().toString().equals("DROPDOWN")) {
                view = mInflater.inflate(R.layout.item_admin_default_spinner, parent, false);
                view.setTag("DROPDOWN");
            }

            TextView textViewTitle = (TextView) view.findViewById(R.id.title_textView);
            textViewTitle.setText(mListData.get(position).getKey());
            int color = HFCommonUtil.getColor(mContext, (int) mListData.get(position).getValue());
            textViewTitle.setTextColor(color);
            return view;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null || !view.getTag().toString().equals("NON_DROPDOWN")) {
                view = mInflater.inflate(R.layout.item_admin_title_spinner, parent, false);
                view.setTag("NON_DROPDOWN");
            }
            TextView textViewTitle = (TextView) view.findViewById(R.id.title_textView);
            textViewTitle.setText(mListData.get(position).getKey());
            return view;
        }
    }
}
