package com.epson.mobilephone.common.guide;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.Locale;

import epson.print.R;

/**
 * 新手引导页
 */
public class NoticeGuideActivity extends AppCompatActivity implements GuideWebviewFragment.MyClickListener {

    public static int DIALOG_MODE_OFF = 20001;
    public static int DIALOG_MODE_ON = 20000;
    public static int DIALOG_MODE_ONLY_BUTTON = 20002;
    public static String KEY_BOOT_GROUP = "BOOT_GROUP";
    public static String KEY_BOOT_MODE = "BOOT_MODE";
    public static String KEY_DIALOG_BUTTON_NO = "DIALOG_NO";
    public static String KEY_DIALOG_BUTTON_YES = "DIALOG_YES";
    public static String KEY_DIALOG_MODE = "DIALOG_MODE";
    public static String KEY_DIALOG_TITLE = "DIALOG_TITLE";
    public static String KEY_GUIDE_VER = "GUIDE_VER";
    public static String KEY_HTML_PATH = "HTML_PATH";
    public static int MODE_AUTO = 10001;
    public static int MODE_MANUAL = 10000;
    public String SP_KEY_GUIDE_VER = "GuideVersion";
    private String boot_group;
    private int boot_mode;
    private int dialog_mode;
    private ArrayList<String> htmlpath = new ArrayList<>();
    private MyFragmentStatePagerAdapter mAdapter;
    private Button mCancel;
    private String mDialogButtonNo = "";
    private String mDialogButtonYes = "";
    private TextView mDialogText;
    private String mDialogTextMessage = "";
    private View mGrayLine_Hor;
    private LinearLayout mGuideDialogView;
    private Button mOK;
    private LinearLayout mPageDisplayView;
    private MyViewPager mViewPager;
    private int version;

    protected void onCreate(Bundle bundle) {
        String str;
        super.onCreate(bundle);
        setContentView(R.layout.activity_notice_guide);

        Intent intent = getIntent();
        if (intent != null) {
            htmlpath = intent.getStringArrayListExtra(KEY_HTML_PATH);
            if (htmlpath.size() == 0) {
                finish();
            }
            boot_mode = intent.getIntExtra(KEY_BOOT_MODE, 0);
            boot_group = intent.getStringExtra(KEY_BOOT_GROUP);
            dialog_mode = intent.getIntExtra(KEY_DIALOG_MODE, 0);
            version = intent.getIntExtra(KEY_GUIDE_VER, 0);
            mDialogTextMessage = intent.getStringExtra(KEY_DIALOG_TITLE);
            mDialogButtonYes = intent.getStringExtra(KEY_DIALOG_BUTTON_YES);
            mDialogButtonNo = intent.getStringExtra(KEY_DIALOG_BUTTON_NO);
            if (boot_group != null) {
                SP_KEY_GUIDE_VER += "-" + boot_group;
            }
        }
        if (boot_mode == MODE_AUTO) {
            if (version <= PreferenceManager.getDefaultSharedPreferences(this).getInt(SP_KEY_GUIDE_VER, 0)) {
                finish();
            }
        }
        final MyPageAdapter myPageAdapter = new MyPageAdapter();
        mAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager(), htmlpath);
        mViewPager = (MyViewPager) findViewById(R.id.guideViewPager);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrollStateChanged(int i) {
            }

            public void onPageScrolled(int i, float f, int i2) {
            }

            public void onPageSelected(int i) {
                myPageAdapter.changePage(i);
            }
        });
        mPageDisplayView = (LinearLayout) findViewById(R.id.pageDisplayViewGroup);
        myPageAdapter.setPageSize(mPageDisplayView, mAdapter.getCount());
        myPageAdapter.changePage(mViewPager.getCurrentItem());
        mGuideDialogView = (LinearLayout) findViewById(R.id.guide_dialog_layout);
        mGrayLine_Hor = findViewById(R.id.guide_dialog_horizon_gray_line);
        mDialogText = (TextView) findViewById(R.id.guide_dialog_message);
        int i = dialog_mode;
        if (i == DIALOG_MODE_ON || i == DIALOG_MODE_ONLY_BUTTON) {
            mGuideDialogView.setVisibility(View.VISIBLE);
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mGuideDialogView.getLayoutParams();
            marginLayoutParams.width = (int) Utils.dptopx(Utils.getWebViewDP(this, getApplicationContext()) + Utils.LAYOUT_MEARGIN, getApplicationContext());
            mGuideDialogView.setLayoutParams(marginLayoutParams);
            mOK = (Button) findViewById(R.id.ok_button);
            mCancel = (Button) findViewById(R.id.cancel_btn);
            String str2 = mDialogButtonYes;
            if (str2 != null) {
                mOK.setText(str2);
            }
            String str3 = mDialogButtonNo;
            if (str3 != null) {
                mCancel.setText(str3);
            }
            mOK.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    setResult(-1);
                    onClickClose();
                }
            });
            mCancel.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    setResult(0);
                    onClickClose();
                }
            });
            if (dialog_mode == DIALOG_MODE_ON && (str = mDialogTextMessage) != null) {
                mDialogText.setText(str);
            }
            if (dialog_mode == DIALOG_MODE_ONLY_BUTTON) {
                mGrayLine_Hor.setVisibility(View.GONE);
                mDialogText.setVisibility(View.GONE);
                mOK.setBackground(getResources().getDrawable(R.drawable.rectangle_left_button));
                mCancel.setBackground(getResources().getDrawable(R.drawable.rectangle_right_button));
            }
        }
    }

    public void onBackPressed() {
        onClickClose();
    }

    public void onClickClose() {
        if (boot_mode == MODE_AUTO) {
            PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(SP_KEY_GUIDE_VER, version).apply();
        }
        finish();
    }

    public void onConfigurationChanged(Configuration configuration) {
        switch (configuration.orientation) {
            case 1:
                if (dialog_mode == DIALOG_MODE_ON) {
                    mGuideDialogView.setVisibility(View.VISIBLE);
                    break;
                }
                break;
            case 2:
                if (dialog_mode == DIALOG_MODE_ON) {
                    mGuideDialogView.setVisibility(View.GONE);
                    break;
                }
                break;
        }
        super.onConfigurationChanged(configuration);
    }

    public static class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
        private ArrayList<String> HTML_PAGE;
        private int PAGE_LENGTH;

        private MyFragmentStatePagerAdapter(FragmentManager fragmentManager, ArrayList<String> arrayList) {
            super(fragmentManager);
            HTML_PAGE = new ArrayList<>();
            PAGE_LENGTH = 0;
            HTML_PAGE = arrayList;
            PAGE_LENGTH = arrayList.size();
        }

        public Fragment getItem(int i) {
            return GuideWebviewFragment.newInstance(String.format(Locale.US, HTML_PAGE.get(i), new Object[]{Integer.valueOf(i + 1)}));
        }

        public int getCount() {
            return PAGE_LENGTH;
        }
    }

    class MyPageAdapter {
        private ImageView[] mPagePosition;

        private MyPageAdapter() {
        }

        private void setPageSize(@NonNull ViewGroup viewGroup, int i) {
            mPagePosition = new ImageView[i];
            int i2 = 0;
            while (i2 < i) {
                ImageView imageView = new ImageView(getApplicationContext());
                imageView.setImageResource(R.drawable.cycle02);
                int i3 = i2 + 1;
                viewGroup.addView(imageView, i3);
                mPagePosition[i2] = imageView;
                i2 = i3;
            }
        }

        private void changePage(int i) {
            int i2 = 0;
            while (true) {
                ImageView[] imageViewArr = mPagePosition;
                if (i2 < imageViewArr.length) {
                    if (imageViewArr[i2] != null) {
                        if (i == i2) {
                            imageViewArr[i2].setImageResource(R.drawable.cycle03);
                        } else {
                            imageViewArr[i2].setImageResource(R.drawable.cycle02);
                        }
                    }
                    i2++;
                } else {
                    return;
                }
            }
        }
    }
}
