package kr.co.icontrols.iotplatform;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.appcompat.app.AppCompatActivity;

import kr.co.icontrols.iotplatform.utils.SharedPrefUtil;

public class SmartGuideActivity extends AppCompatActivity implements View.OnClickListener {

    private CheckBox chkDontShow;

    private View indicator1;
    private View indicator2;
    private View indicator3;

    private ViewFlipper mViewFlipper;
    private int m_nPreTouchPosX = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_guide);

        indicator1 = findViewById(R.id.indicator1);
        indicator2 = findViewById(R.id.indicator2);
        indicator3 = findViewById(R.id.indicator3);
        mViewFlipper = findViewById(R.id.viewFlipper);
        chkDontShow = findViewById(R.id.chkDontShow);

        changeHelpPage();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onClick(View v) {
        /*
        final View fv = v;
        fv.setEnabled(false);
        Timer buttonTimer = new Timer();
        buttonTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fv.setEnabled(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {

                        }
                    }
                });
            }
        }, 500);
        */

        switch (v.getId()) {
            case R.id.btnClose:
                SharedPrefUtil.putBoolean(SharedPrefUtil.SHOW_SMART_GUIDE,!chkDontShow.isChecked());
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    private void changeHelpPage(){
        /**
         * ViewFlipper에 도움말 페이지 추가.
         */
        mViewFlipper.removeAllViews();
        int main_img[] = {R.drawable.img_smartguide_01,R.drawable.img_smartguide_02,R.drawable.img_smartguide_03};
        String main_txtTitle[] = getResources().getStringArray(R.array.list_smart_guide_title);
        String main_txtSubTitle[] = getResources().getStringArray(R.array.list_smart_guide_subtitle);
        for(int i = 0;i<main_img.length;i++) {
            View layoutGuide = LayoutInflater.from(this).inflate(R.layout.layout_guide,null);
            ((ImageView)layoutGuide.findViewById(R.id.img)).setImageResource(main_img[i]);
            ((TextView)layoutGuide.findViewById(R.id.txtTitle)).setText(main_txtTitle[i]);
            ((TextView)layoutGuide.findViewById(R.id.txtSubTitle)).setText(main_txtSubTitle[i]);
            mViewFlipper.addView(layoutGuide);
        }
        mViewFlipper.setOnTouchListener(MyTouchListener);
        updateIndicators(mViewFlipper.getDisplayedChild());
    }


    /**
     * 화면에 터치이벤트 추가.
     */
    View.OnTouchListener MyTouchListener = new View.OnTouchListener()
    {
        public boolean onTouch(View v, MotionEvent event)
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                m_nPreTouchPosX = (int)event.getX();
            }
            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                int nTouchPosX = (int)event.getX();
                if (nTouchPosX < m_nPreTouchPosX)
                {
                    if(mViewFlipper.getDisplayedChild() < mViewFlipper.getChildCount()-1)
                        MoveNextView();
                }
                else if (nTouchPosX > m_nPreTouchPosX)
                {
                    if(mViewFlipper.getDisplayedChild()>0)
                        MovewPreviousView();
                }
                m_nPreTouchPosX = nTouchPosX;
            }
            updateIndicators(mViewFlipper.getDisplayedChild());
            return true;
        }

    };

    /**
     * 다음 페이지 이동 함수
     */
    private void MoveNextView()
    {
        mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_right));
        mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_left));
        mViewFlipper.showNext();
    }

    /**
     * 이전 페이지 이동 함수
     */
    private void MovewPreviousView()
    {
        mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_left));
        mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_right));
        mViewFlipper.showPrevious();
    }

    /**
     * 페이지 인디게이터 갱신 함수
     * @param position
     */
    public void updateIndicators(int position) {
        switch (position) {
            case 0:
                indicator1.setBackgroundResource(R.drawable.paging_guide_on);
                indicator2.setBackgroundResource(R.drawable.paging_guide_off);
                indicator3.setBackgroundResource(R.drawable.paging_guide_off);
                break;

            case 1:
                indicator1.setBackgroundResource(R.drawable.paging_guide_off);
                indicator2.setBackgroundResource(R.drawable.paging_guide_on);
                indicator3.setBackgroundResource(R.drawable.paging_guide_off);
                break;

            case 2:
                indicator1.setBackgroundResource(R.drawable.paging_guide_off);
                indicator2.setBackgroundResource(R.drawable.paging_guide_off);
                indicator3.setBackgroundResource(R.drawable.paging_guide_on);
                break;
        }
    }
}