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

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.admin.admin.AdminMemberListActivity;
import kr.co.hanwha.hifive.admin.admin.AdminMenuActivity;
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. 3. 27..
 */

public class TestDesignActivity extends BaseActivity {
    private LinearLayout mLayoutContainer;
    private Spinner mSpinner;
    private BackgroundColorSpinnerAdapter mAdapter;

    private LinearLayout mLayoutTask;
    private TextView mTextViewElevationTask;
    private float mElevationTask = 2;
    private LinearLayout mLayoutUnit;
    private TextView mTextViewElevationUnit;
    private float mElevationUnit = 2;
    private LinearLayout mLayoutIssueTop;
    private LinearLayout mLayoutIssueBottom;
    private TextView mTextViewElevationIssue;
    private float mElevationIssue = 3;

    private ArrayList<SpinnerData> mListSpinner;

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

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

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

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

        mLayoutContainer = (LinearLayout) findViewById(R.id.container_linearLayout);

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

        mLayoutTask = (LinearLayout) findViewById(R.id.task_linearLayout);
        mTextViewElevationTask = (TextView) findViewById(R.id.elevationTask_textView);
        findViewById(R.id.minusTask_textView).setOnClickListener(onClickListener);
        findViewById(R.id.plusTask_textView).setOnClickListener(onClickListener);

        mLayoutUnit = (LinearLayout) findViewById(R.id.unit_linearLayout);
        mTextViewElevationUnit = (TextView) findViewById(R.id.elevationUnit_textView);
        findViewById(R.id.minusUnit_textView).setOnClickListener(onClickListener);
        findViewById(R.id.plusUnit_textView).setOnClickListener(onClickListener);

        mLayoutIssueTop = (LinearLayout) findViewById(R.id.issueTop_linearLayout);
        mLayoutIssueBottom = (LinearLayout) findViewById(R.id.issueBottom_linearLayout);
        mTextViewElevationIssue = (TextView) findViewById(R.id.elevationIssue_textView);
        findViewById(R.id.minusIssue_textView).setOnClickListener(onClickListener);
        findViewById(R.id.plusIssue_textView).setOnClickListener(onClickListener);

        findViewById(R.id.font_button).setOnClickListener(onClickListener);


        //Initialize spinner data
        mListSpinner = new ArrayList<>();
        mListSpinner.add(new SpinnerData("Default", R.color.hf_background));
        mListSpinner.add(new SpinnerData("White", R.color.white));

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

        //Initialize Task Data
        setElevationTask(mElevationTask);

        //Initialize Unit Data
        int screenWidth = HFCommonUtil.getScreenWidth(this);
        int dp10 = HFCommonUtil.getPixelFromDimension(this, R.dimen.dimen_dp10);
        int cellWidth = (screenWidth - dp10 * 6) / 2;
        LinearLayout.LayoutParams cellParam = new LinearLayout.LayoutParams(cellWidth, cellWidth);
        cellParam.topMargin = dp10 * 2;
        cellParam.bottomMargin = dp10 * 2;
        mLayoutUnit.setLayoutParams(cellParam);
        setElevationUnit(mElevationUnit);

        //Initialize Issue
        setElevationIssue(mElevationIssue);
    }

    /**************************************************
     * Support methods
     **************************************************/
    public void setBackgroundColor(int color) {
        mLayoutContainer.setBackgroundColor(color);
    }

    private void setElevationTask(float elevationDp) {
        mLayoutTask.setElevation(HFCommonUtil.getPixelFromDp(this, elevationDp));
        mTextViewElevationTask.setText(String.format("%.01f", elevationDp));
    }

    private void setElevationUnit(float elevationDp) {
        mLayoutUnit.setElevation(HFCommonUtil.getPixelFromDp(this, elevationDp));
        mTextViewElevationUnit.setText(String.format("%.01f", elevationDp));
    }

    private void setElevationIssue(float elevationDp) {
        mLayoutIssueTop.setElevation(HFCommonUtil.getPixelFromDp(this, elevationDp));
        mLayoutIssueBottom.setElevation(HFCommonUtil.getPixelFromDp(this, elevationDp));
        mTextViewElevationIssue.setText(String.format("%.01f", elevationDp));
    }

    /**************************************************
     * Support methods
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.minusTask_textView:
                    mElevationTask -= 0.1;
                    if (mElevationTask < 0) mElevationTask = 0;
                    setElevationTask(mElevationTask);
                    break;
                case R.id.plusTask_textView:
                    mElevationTask += 0.1;
                    setElevationTask(mElevationTask);
                    break;
                case R.id.minusUnit_textView:
                    mElevationUnit -= 0.1;
                    if (mElevationUnit < 0) mElevationUnit = 0;
                    setElevationUnit(mElevationUnit);
                    break;
                case R.id.plusUnit_textView:
                    mElevationUnit += 0.1;
                    setElevationUnit(mElevationUnit);
                    break;
                case R.id.minusIssue_textView:
                    mElevationIssue -= 0.1;
                    if (mElevationIssue < 0) mElevationIssue = 0;
                    setElevationIssue(mElevationIssue);
                    break;
                case R.id.plusIssue_textView:
                    mElevationIssue += 0.1;
                    setElevationIssue(mElevationIssue);
                    break;
                case R.id.font_button:
                    Intent intent = new Intent(TestDesignActivity.this, TestFontActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    };

    /**************************************************
     * 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(TestDesignActivity.this, (int) mListSpinner.get(position).getValue());
            setBackgroundColor(color);
        }

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

        }
    };

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

        public BackgroundColorSpinnerAdapter(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;
        }
    }

}
